/
MyCustomTaskPane.cs
94 lines (79 loc) · 3.06 KB
/
MyCustomTaskPane.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
namespace ExcelCustomTaskPaneCS
{
using System;
using System.Windows.Forms;
using System.Windows.Forms.Integration;
using ExcelDna.Integration;
using ExcelDna.Integration.CustomUI;
/////////////// Define the backing class for the Ribbon ///////////////////////////
// Would need to be marked with [ComVisible(true)] if in a project that is marked as [assembly:ComVisible(false)] which is the default for VS projects.
public class MyRibbon : ExcelRibbon
{
public void OnShowCTP(IRibbonControl control)
{
CTPManager.ShowCTP();
}
public void OnDeleteCTP(IRibbonControl control)
{
CTPManager.DeleteCTP();
}
}
/////////////// Define the UserControl to display on the CTP ///////////////////////////
// Would need to be marked with [ComVisible(true)] if in a project that is marked as [assembly:ComVisible(false)] which is the default for VS projects.
public class MyUserControl : UserControl
{
// public Label TheLabel;
public TaskPaneContent Content;
public MyUserControl()
{
Content = new TaskPaneContent();
var wpfElementHost = new ElementHost() { Dock = DockStyle.Fill };
wpfElementHost.HostContainer.Children.Add(Content);
Content.MyButton.Click += MyButton_Click;
Controls.Add(wpfElementHost);
}
void MyButton_Click(object sender, System.Windows.RoutedEventArgs e)
{
MessageBox.Show("You clicked the button.");
}
}
/////////////// Helper class to manage CTP ///////////////////////////
internal static class CTPManager
{
static CustomTaskPane ctp;
public static void ShowCTP()
{
if (ctp == null)
{
// Make a new one using ExcelDna.Integration.CustomUI.CustomTaskPaneFactory
ctp = CustomTaskPaneFactory.CreateCustomTaskPane(typeof(MyUserControl), "My Super Task Pane");
ctp.Visible = true;
ctp.DockPosition = MsoCTPDockPosition.msoCTPDockPositionLeft;
ctp.DockPositionStateChange += ctp_DockPositionStateChange;
ctp.VisibleStateChange += ctp_VisibleStateChange;
}
else
{
// Just show it again
ctp.Visible = true;
}
}
public static void DeleteCTP()
{
if (ctp != null)
{
// Could hide instead, by calling ctp.Visible = false;
ctp.Delete();
ctp = null;
}
}
static void ctp_VisibleStateChange(CustomTaskPane CustomTaskPaneInst)
{
MessageBox.Show("Visibility changed to " + CustomTaskPaneInst.Visible);
}
static void ctp_DockPositionStateChange(CustomTaskPane CustomTaskPaneInst)
{
((MyUserControl)ctp.ContentControl).Content.MyLabel.Content = "Moved to " + CustomTaskPaneInst.DockPosition.ToString();
}
}
}