user1523271
user1523271

Reputation: 1063

Using CustomTaskPane.Window to distinguish between panes

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

Upvotes: 0

Views: 307

Answers (2)

user1523271
user1523271

Reputation: 1063

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?

Upvotes: 1

Eugene Astafiev
Eugene Astafiev

Reputation: 49395

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.

Upvotes: 0

Related Questions