17
votes

Is it possible to have a window from another 3rd party application shown inside our WPF Window? Preferably in a container control?

I'm guessing there might be some Win32 API that allows us to do that.

1

1 Answers

10
votes

I did that some time ago for Winforms, but the method was not to bright, so as long as anyone else doesn't have any idea, here's what I did. The code was pretty much this:

Process p = Process.Start(@"application.exe");

p.WaitForInputIdle();
IntPtr appWin = p.MainWindowHandle;

SetParent(appWin, parent);
SetWindowLong(appWin, GWL_STYLE, WS_VISIBLE);
System.Threading.Thread.Sleep(100);
MoveWindow(appWin, 0, 0, ClientRectangle.Width, ClientRectangle.Height, true);

(where SetParent, SetWindowLong and MoveWindow are the win32 API functions called via p/invoke) The sleep was needed as a hack, because without it the call to MoveWindow would have no effect.

For WPF you will need a handle to a window/control that will be the parrent of your 3rd party window and the easiest way to get such a handle is to use a HwndHost container.

I don't think there is a prettier way to achieve this in WPF. Also, note that I've only tested this in winforms, not in WPF, but it should work also in WPF, as long as it has a valid win32 HWND of the parent.