How to run exe application inside wpf application?

0

My question is how to run an application(.exe) inside WPF application. I mean running inside a window of an application, not an external running an application, in this code notepad work but if change .exe file (e.g calc.exe) application open in exte

    private Process _process;

    [DllImport("user32.dll")]
    private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);

    [DllImport("user32.dll", SetLastError = true)]
    private static extern int GetWindowLong(IntPtr hWnd, int nIndex);

    [DllImport("user32")]
    private static extern IntPtr SetParent(IntPtr hWnd, IntPtr hWndParent);

    [DllImport("user32")]
    private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, int uFlags);

    private const int SWP_NOZORDER = 0x0004;
    private const int SWP_NOACTIVATE = 0x0010;
    private const int GWL_STYLE = -16;
    private const int WS_CAPTION = 0x00C00000;
    private const int WS_THICKFRAME = 0x00040000;

    private void LaunchChildProcess()
    {
        _process = Process.Start("notepad.exe");
        _process.WaitForInputIdle();

        var helper = new WindowInteropHelper(this);

        while(_process.MainWindowHandle == IntPtr.Zero)
        {

            System.Threading.Thread.Sleep(10);

            _process.Refresh();

        }

        SetParent(_process.MainWindowHandle, helper.Handle);


        // remove control box
        int style = GetWindowLong(_process.MainWindowHandle, GWL_STYLE);
        style = style & ~WS_CAPTION & ~WS_THICKFRAME;
        SetWindowLong(_process.MainWindowHandle, GWL_STYLE, style);
        // resize embedded application & refresh
        ResizeEmbeddedApp();

}

    private void ResizeEmbeddedApp()
    {
        if (_process == null)
            return;
        SetWindowPos(_process.MainWindowHandle, IntPtr.Zero, 0, 0, (int)ActualWidth, (int)ActualHeight, SWP_NOZORDER | SWP_NOACTIVATE);
    }

    protected override Size MeasureOverride(Size availableSize)
    {
        Size size = base.MeasureOverride(availableSize);
        ResizeEmbeddedApp();
        return size;
    }
c#
wpf
asked on Stack Overflow Aug 30, 2020 by user6860476

1 Answer

1

In general, you can't. Only a small subset of Win32 applications support window reparenting, as you've discovered.

You do have some options:

  1. Only reparent windows of applications that support it.
  2. For applications that don't support it, you can use the DWM to get a live copy of the target window's surface displayed as an overlay on-top of your application with DwmRegisterThumbnail.
    • Despite the name "thumbnail" you can render the copy at any size, including the same pixel dimensions as the source window.
    • Note that the thumbnail is not interactive: users cannot give it focus and mouse/keyboard events will not be forwarded to the target application, but this is something you can build yourself if you post your own window-messages.
  3. There is also DwmSetIconicThumbnail - except it allows you to get the target window in an in-memory HBITMAP.
    • But I don't recommend using this approach as it means you'll have a dependency on GDI via HBITMAP, and GDI does not support things like 30-bit color (aka 10-bit color).
answered on Stack Overflow Aug 30, 2020 by Dai • edited Aug 31, 2020 by Dai

User contributions licensed under CC BY-SA 3.0