How to embed Window from another application in our WPF window as user control?

后端 未结 1 1668
渐次进展
渐次进展 2020-12-30 02:54

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

相关标签:
1条回答
  • 2020-12-30 03:09

    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.

    0 讨论(0)
提交回复
热议问题