Link to home
Start Free TrialLog in
Avatar of mridey
mridey

asked on

How to insert a .NET control into a non-.NET NativeWindow ?

I'm writing an Office Add-in in C#.
Using Automation, I get the Handle of the main window of the application in which the Add-in is running (in my case, PowerPoint).
I can then navigate the Child windows, hook into message loops and can create additional Windows using the NativeWindow class

But

I would like to be able to create a .NET control and position it into the application. The problem is that I have non-.NET handles and when I try to call Control.FromHandle(handle), it returns null. Then I can't assign a parent to my .NET control so it's not displayed.

So How do I assign a .NET control to a non-.NET parent window ?

Thanks

Marc
Avatar of mridey
mridey

ASKER

I found a solution on the dotnet247 forum.
After I create the .Net control, I call SetParent Win32 using:

\\\
[DllImport("USER32.DLL")]
private static extern IntPtr SetParent(
IntPtr hWndChild, // handle to window
IntPtr hWndNewParent // new parent window
);
///

That work.

If you know anything nicier, I'll still award the points.
I don't even think there is another way to do that, although I could be wrong. Might just ask to get your points refunded in the community TA :)
ASKER CERTIFIED SOLUTION
Avatar of Computer101
Computer101
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial