18

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.

Muhammad Hasan Khan
  • 34,648
  • 16
  • 88
  • 131

1 Answers1

11

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.

Andrei Pana
  • 4,484
  • 1
  • 26
  • 27
  • Thanks it works but its a bit hacky. The behavior of target applications is undefined. Some applications are some how registered with OS as non UI application even though they do have a UI i.e. Office and some of them stop behaving normally. I'm unaccepting your answer for a while in a hope to get more answers if there are any. – Muhammad Hasan Khan Jan 06 '11 at 17:41
  • I was wondering in the morning how this worked for you, as I saw you accepted the answer and left no comment. I agree it's quite hacky. I'm curious for myself if there is a better way of doing this; unfortunately when I needed to implement this I've spent so much time and didn't find anything better than this. At that time, this was acceptable for my case, but it's definitely not a nice piece of code and as I mentioned in the answer, I shared it because there was no answer here. – Andrei Pana Jan 06 '11 at 18:39
  • Sadly thats the only option I guess. – Muhammad Hasan Khan Jan 11 '11 at 07:48