Reputation: 1940
I'm creating a custom task panel for an Excel Add-In using VS 2010. I want the task pane to always be visible to the user, so it can't be closed, moved, or resized.
Is there a way to disable those capabilities in the task pane's title bar? Perhaps by disabling the close box and the down arrow button in the upper right corner?
Thanks
Upvotes: 2
Views: 3730
Reputation: 1940
To stop a custom task pane from being closed:
When a user presses the close button, the result is that the custom pane's Visible property is set to false. This causes the custom pane's VisibleChanged event to fire. Within that event's handler, you can forcibly reopen the custom task pane.
private static void OnVisibleChanged(object sender, EventArgs e)
{
var customTaskPane = sender as CustomTaskPane;
if (customTaskPane != null)
{
Dispatcher.CurrentDispatcher.BeginInvoke(new Action(() => { customTaskPane.Visible = true; }));
}
}
Note that I'm using the Dispatcher's BeginInvoke method. This is because if you try to directly set the custom pane's Visible property to true, without using the Dispatcher, an exception is thrown since that isn't allowed within the event handler. Using the BeginInvoke method causes the assignment to execute asynchronously, thus working around that limitation.
To stop a custom task pane from being resized:
To accomplish this, you need to attach a SizeChanged event handler to the custom task pane's UserControl. For example:
var sampleHostControl = new WpfHostControl(Globals.AddIn.SamplePaneWpfControl);
_samplePane = this.CustomTaskPanes.Add(sampleHostControl, "Sample");
sampleHostControl.SizeChanged += new EventHandler(OnHostControlSizeChanged);
Inside the event handler, you can reset the _samplePane's height.
private const int PaneHeight = 52;
private void OnHostControlSizeChanged(object sender, EventArgs e)
{
if (_samplePane != null && _samplePane.Height != PaneHeight)
{
System.Windows.Forms.SendKeys.Send("{ESC}");
_samplePane.Height = PaneHeight;
}
}
The use of SendKeys.Send comes from this msdn forum post. A Microsoft Moderator indicates that SendKeys.Send stops the re-sizing operation. The Height comparison in the if-statement ensures that we only prevent vertical height changes; if a user wants to horizontally expand or shrink Excel, we shouldn't prevent that.
Upvotes: 6
Reputation: 15391
You can specify the Docking of your task pane and lock it so that its position cannot be modified by the user this way:
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
var taskPaneContainer = new TaskPaneContainer();
var taskPane = this.CustomTaskPanes.Add(taskPaneContainer, "My Task Pane");
taskPane.DockPosition = MsoCTPDockPosition.msoCTPDockPositionRight;
taskPane.DockPositionRestrict = MsoCTPDockPositionRestrict.msoCTPDockPositionRestrictNoChange;
taskPane.Visible = true;
}
On the other hand, as far as I know, it is not possible to directly prevent a user from making the TaskPane invisible. Your best bet is probably to add a button in the Ribbon to make the TaskPane visible again.
Upvotes: 5