HwndHost doesn't display content when Desktop Composition is enabled

0

I'm using HwndHost to embed an external application in my WPF window. I noticed on some Windows 7 machines, if an Aero Theme is selected and Desktop Composition is enabled, the external application starts, flickers on the screen for a split second and then it disappears. If I turn off Desktop Composition or use the basic theme, the application is embedded successfully inside the WPF window.

This is the code I use in a class derived from HwndHost:

[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);

[DllImport("USER32.DLL", SetLastError = true)]
private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);

private const int GWL_STYLE = (-16);
private const int WS_CHILD = 0x40000000;
private const int WS_EX_APPWINDOW = 0x00040000;

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

protected override HandleRef BuildWindowCore(HandleRef hwndParent)
{
    while (Process.MainWindowHandle == IntPtr.Zero)
    {
        Process.Refresh();
        System.Threading.Thread.Sleep(10);
    }

    SetLastError(0);
    var ret = SetWindowLong(Process.MainWindowHandle, GWL_STYLE, WS_CHILD);
    int e1 = Marshal.GetLastWin32Error();
    SetParent(Process.MainWindowHandle, hwndParent.Handle);
    int e2 = Marshal.GetLastWin32Error();
    ShowWindow(Process.MainWindowHandle, 0);
    int e3 = Marshal.GetLastWin32Error();
    return new HandleRef(this, Process.MainWindowHandle);
}

I don't get any windows errors when the issue occurs. The process starts from another window which injects it to my class. I've checked with task manager and the process runs but it's not visible inside my WPF window. Any thoughts?

wpf
hwndhost
asked on Stack Overflow Jul 25, 2017 by muku

1 Answer

0

Look at the system addin namespace for how you can use the native HWIND_PTR as a control. You don't need to use all of the library to do the work.

https://docs.microsoft.com/en-us/dotnet/framework/wpf/app-development/wpf-add-ins-overview

answered on Stack Overflow Jul 27, 2017 by Dave

User contributions licensed under CC BY-SA 3.0