-
-
Notifications
You must be signed in to change notification settings - Fork 294
Expand file tree
/
Copy pathExcelCustomTaskPane.cs
More file actions
189 lines (160 loc) · 8.1 KB
/
ExcelCustomTaskPane.cs
File metadata and controls
189 lines (160 loc) · 8.1 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#if !COM_GENERATED
// Copyright (c) Govert van Drimmelen. All rights reserved.
// Excel-DNA is licensed under the zlib license. See LICENSE.txt for details.
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using ExcelDna.ComInterop.ComRegistration;
using ExcelDna.Logging;
namespace ExcelDna.Integration.CustomUI
{
public static class CustomTaskPaneFactory
{
private static ExcelCustomTaskPaneAddIn _addin;
// We keep a list of CustomTaksPanes, so that we can clean up when the add-in is removed or reopened.
// But we don't want to artificially extend the lifetime of CTPs if the add-in is not keeping a reference.
// So we use WeakReferences to not interfere with lifetime, but have a chance to clean up for live ones.
private static readonly List<WeakReference> _customTaskPanes = new List<WeakReference>();
public static CustomTaskPane CreateCustomTaskPane(Type userControlType, string title)
{
return CreateCustomTaskPane(userControlType, title, Type.Missing);
}
public static CustomTaskPane CreateCustomTaskPane(Type userControlType, string title, Guid clsIdCTPAddIn, string progIdCTPAddIn)
{
return CreateCustomTaskPane(userControlType, title, Type.Missing, clsIdCTPAddIn, progIdCTPAddIn);
}
public static CustomTaskPane CreateCustomTaskPane(Type userControlType, string title, object parent)
{
object userControl = Activator.CreateInstance(userControlType);
return CreateCustomTaskPane(userControl, title, parent);
}
public static CustomTaskPane CreateCustomTaskPane(Type userControlType, string title, object parent, Guid clsIdCTPAddIn, string progIdCTPAddIn)
{
object userControl = Activator.CreateInstance(userControlType);
return CreateCustomTaskPane(userControl, title, parent, clsIdCTPAddIn, progIdCTPAddIn);
}
public static CustomTaskPane CreateCustomTaskPane(object userControl, string title)
{
return CreateCustomTaskPane(userControl, title, Type.Missing);
}
public static CustomTaskPane CreateCustomTaskPane(object userControl, string title, Guid clsIdCTPAddIn, string progIdCTPAddIn)
{
return CreateCustomTaskPane(userControl, title, Type.Missing, clsIdCTPAddIn, progIdCTPAddIn);
}
public static CustomTaskPane CreateCustomTaskPane(object userControl, string title, object parent)
{
return CreateCustomTaskPane((progId) => CreateCustomTaskPane(progId, title, parent), userControl);
}
public static CustomTaskPane CreateCustomTaskPane(object userControl, string title, object parent, Guid clsIdCTPAddIn, string progIdCTPAddIn)
{
return CreateCustomTaskPane((progId) => CreateCustomTaskPane(progId, title, parent, clsIdCTPAddIn, progIdCTPAddIn), userControl);
}
// UserControl as already registered. Just create via factory and add-in.
public static CustomTaskPane CreateCustomTaskPane(string controlProgId, string title)
{
return CreateCustomTaskPane(controlProgId, title, Type.Missing);
}
// UserControl as already registered. Just create via factory and add-in.
public static CustomTaskPane CreateCustomTaskPane(string controlProgId, string title, Guid clsIdCTPAddIn, string progIdCTPAddIn)
{
return CreateCustomTaskPane(controlProgId, title, Type.Missing, clsIdCTPAddIn, progIdCTPAddIn);
}
public static CustomTaskPane CreateCustomTaskPane(string controlProgId, string title, object parent)
{
return CreateCustomTaskPane(GetCTPFactory(null, null), controlProgId, title, parent);
}
public static CustomTaskPane CreateCustomTaskPane(string controlProgId, string title, object parent, Guid clsIdCTPAddIn, string progIdCTPAddIn)
{
return CreateCustomTaskPane(GetCTPFactory(clsIdCTPAddIn, progIdCTPAddIn), controlProgId, title, parent);
}
private static CustomTaskPane CreateCustomTaskPane(ICTPFactory factory, string controlProgId, string title, object parent)
{
CustomTaskPane newCTP = factory.CreateCTP(controlProgId, title, parent);
_customTaskPanes.Add(new WeakReference(newCTP)); // TODO: Only removed when add-in is unloaded...???
return newCTP;
}
private static CustomTaskPane CreateCustomTaskPane(Func<string, CustomTaskPane> ctpActivator, object userControl)
{
// I could use the ProgId and ClsId of the UserControl type here.
// But then the registration has to be persistent or coordinated, which I dislike.
// It's already a problem for the RTD servers.
// Users that want persistent and consistent names, can sort out registration themselves, or use the ExcelComClass support.
// Then the CTP is created through the CreateCustomTaskPane("My.ProgId",...) overloads.
// So when passed the type, I always synthesize a progid.
// We pick a new Guid as ClassId for this add-in...
Guid clsId = Guid.NewGuid();
// ...and make the ProgId from this Guid - max 39 chars.
// Change from CtpSrv.xxx to CtpSrv_xxx to avoid McAfee bug that blocks registry writes with a "." anywhere
string progId = "CtpSrv_" + clsId.ToString("N");
// Instantiate and then register UserControl
try
{
using (new SingletonClassFactoryRegistration(userControl, clsId))
using (new ProgIdRegistration(progId, clsId))
using (new ClsIdRegistration(clsId, progId))
{
return ctpActivator(progId);
}
}
catch (UnauthorizedAccessException secex)
{
Logger.Initialization.Error(secex,
"The CTP Helper could not be registered.\r\nThis may be due to restricted permissions on the user's HKCU\\Software\\Classes key.");
return null;
}
}
private static ICTPFactory GetCTPFactory(Guid? clsIdCTPAddIn, string progIdCTPAddIn)
{
if (_addin == null)
{
// Register and create addin
_addin = new ExcelCustomTaskPaneAddIn { DnaLibrary = DnaLibrary.CurrentLibrary };
if (clsIdCTPAddIn.HasValue && !string.IsNullOrWhiteSpace(progIdCTPAddIn))
ExcelComAddInHelper.LoadComAddIn(_addin, clsIdCTPAddIn.Value, progIdCTPAddIn);
else
ExcelComAddInHelper.LoadComAddIn(_addin);
}
return _addin.Factory;
}
internal static void UnloadCustomTaskPanes()
{
foreach (WeakReference ctpWr in _customTaskPanes)
{
CustomTaskPane ctp = ctpWr.Target as CustomTaskPane;
if (ctp != null)
{
ctp.Delete();
Marshal.FinalReleaseComObject(ctp);
}
}
_customTaskPanes.Clear();
}
internal static void DetachAddIn()
{
_addin = null;
}
}
internal class ExcelCustomTaskPaneAddIn : ExcelComAddIn, ICustomTaskPaneConsumer
{
public ICTPFactory Factory;
// For CustomTaskPane stuff
// TODO: ActiveX -> WPF without Windows.Forms....?
public void CTPFactoryAvailable(ICTPFactory CTPFactoryInst)
{
Factory = CTPFactoryInst;
}
public override void OnDisconnection(Extensibility.ext_DisconnectMode RemoveMode, ref Array custom)
{
// Unloading custom task panes prevents the crash on Excel 2010 64-bit.
CustomTaskPaneFactory.UnloadCustomTaskPanes();
CustomTaskPaneFactory.DetachAddIn();
if (Factory != null)
{
Marshal.ReleaseComObject(Factory);
Factory = null;
}
base.OnDisconnection(RemoveMode, ref custom);
}
}
}
#endif