Using CustomTaskPane.Window to distinguish between panes

时光总嘲笑我的痴心妄想 提交于 2020-01-16 09:34:06

问题


C# Add-In for Excel.

I want to use the CustomTaskPane.Window property in order to know if a pane belongs to the active Workbook. This is my code:

var activeWnd = Globals.ThisAddIn.Application.ActiveWindow;
var pane = this.CustomTaskPanes.Add(myUSerCtrl, "title", activeWnd);
IntPtr panePtr = Marshal.GetIUnknownForObject(pane.Window);
IntPtr activeWndPtr= Marshal.GetIUnknownForObject(activeWnd);    
bool sameWindows = panePtr.Equals(activeWndPtr);

When I inspect sameWindows it is false. And the pointer values are different, too Is there a problem with my code, or these properties are not reliable? Shouldn't both pointers point to the same window? How else can I compare them?

Thanks


回答1:


As a result, you get pointers to the IUnknown interface which doesn't make any sense there.

Instead, I'd recommend casting the window to the IOLEWindow interface and then call the GetWindow method. Both explorer and inspector windows implement the specified interface. Then you may compare window handles.




回答2:


How about this:

if((Microsoft.Office.Interop.Excel.Window)myPane.Window).Hwnd     
== 
Globals.ThisAddIn.Application.ActiveWindow.Hwnd)

Will it work OK? I tried and it works but I am wondering if it is the right thing to do?



来源:https://stackoverflow.com/questions/56877357/using-customtaskpane-window-to-distinguish-between-panes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!