我在Excel 2013中添加了一个CustomTaskPane,让用户可以快速搜索照片。如果用户只打开/创建一个工作簿,它就能正常工作。问题是,如果他们打开另一个工作簿或创建一个新工作簿,任务窗格就不会出现在出现的新窗口中。它只是停留在原来的窗口。我知道这种行为是由我只是在打开Excel时初始化面板造成的。我在ActiveWindow事件中添加了一个事件处理程序,以便在打开另一个工作簿时初始化一个新面板。
问题是,我无法知道如何判断CustomTaskPane是否已经存在于窗口中。如果是的话,它就简单地创建了另一个CustomTaskPane,因此现在该窗口中有两个。我编写了下面的代码来释放原始代码并创建一个新的代码,但是它引入了一些延迟(1-5秒),每当用户更改工作簿窗口时都会让他们抓狂。是否有一种方法可以查看窗口中是否已经存在一个CustomTaskPane,以避免处理和重新创建一个新的,以避免堆叠重复的任务窗格?
Microsoft.Office.Tools.CustomTaskPane PartPhotoTaskPane;
Globals.ThisAddIn.Application.WindowActivate += Application_WindowActivate;
void Application_WindowActivate(Excel.Workbook Wb, Excel.Window Wn)
{
if (PartPhotoTaskPane != null)
{
PartPhotoTaskPane.Dispose();
InitalizePartPhotoViewerTaskPane(EPPF);
}
else
{
InitalizePartPhotoViewerTaskPane(EPPF);
}
}
/// <summary>
/// Start up the part photo viewer task pane
/// </summary>
private void InitalizePartPhotoViewerTaskPane(ExcelPartPhotoFunctions _EPPF)
{
//intialize the part search
try
{
PartPhotoTaskPane = Globals.ThisAddIn.CustomTaskPanes.Add(new PartPhotoSearchPane(_EPPF), "Part Information", Globals.ThisAddIn.Application.ActiveWindow);
PartPhotoTaskPane.Visible = Properties.Settings.Default.InfoPaneOpenStatus;
PartPhotoTaskPane.Width = 260;
}
catch (Exception e)
{
MessageBox.Show("Error starting Part Info Toolbar:" + Environment.NewLine +
e.Message + Environment.NewLine + e.StackTrace, "Error!", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}发布于 2014-07-14 08:00:36
使用hwnd (Globals.ThisAddIn.Application.Hwnd)标识Excel窗口。这对于Office2013 (它使用SDI方法)和使用MDI窗口的旧版本的Office都很好。下面是一个可以用于此的类:
public class TaskPaneManager
{
static Dictionary<string, CustomTaskPane> _createdPanes = new Dictionary<string, CustomTaskPane>();
/// <summary>
/// Gets the taskpane by name (if exists for current excel window then returns existing instance, otherwise uses taskPaneCreatorFunc to create one).
/// </summary>
/// <param name="taskPaneId">Some string to identify the taskpane</param>
/// <param name="taskPaneTitle">Display title of the taskpane</param>
/// <param name="taskPaneCreatorFunc">The function that will construct the taskpane if one does not already exist in the current Excel window.</param>
public static CustomTaskPane GetTaskPane(string taskPaneId, string taskPaneTitle, Func<UserControl> taskPaneCreatorFunc)
{
string key = string.Format("{0}({1})", taskPaneId, Globals.ThisAddIn.Application.Hwnd);
if (!_createdPanes.ContainsKey(key))
{
var pane = Globals.ThisAddIn.CustomTaskPanes.Add(taskPaneCreatorFunc(), taskPaneTitle);
_createdPanes[key] = pane;
}
return _createdPanes[key];
}
}这里,我实际上结合了Excel窗口hwnd和一些任意字符串标识符来标识任务窗格。其想法是在同一个加载项中支持多个任务窗格。
下面是一个示例,说明如何从丝带中使用它:
private void button1_Click(object sender, RibbonControlEventArgs e)
{
var taskpane = TaskPaneManager.GetTaskPane("A", "Task pane type A", () => new UserControl1());
taskpane.Visible = !taskpane.Visible;
}
private void button2_Click(object sender, RibbonControlEventArgs e)
{
var taskpane = TaskPaneManager.GetTaskPane("B", "Task pane type B", () => new UserControl2());
taskpane.Visible = !taskpane.Visible;
}如果在Excel中打开多本工作簿,两个Excel窗口都将拥有自己的taspaneA和taskpaneB。
https://stackoverflow.com/questions/19160158
复制相似问题