Question

I developed WPF in Windows 8 and succeed to hosting win32 windows to WPF using pinvoke user32.dll. But when I build using Windows 7, non WPF application not hosting into Form Panel in WPF. It open another windows like launched that application.

this is my code:

private System.Windows.Forms.Panel _panel;
private Process _process;

public MainWindow()
{
    _panel = new System.Windows.Forms.Panel();
    windowsFormsHost.Child = _panel;
}

private void WindowLoaded(object sender, RoutedEventArgs e)
{
    ProcessStartInfo psi = new ProcessStartInfo(@"D:\unitypcbuild\UnityBuild.exe");
    psi.WindowStyle = ProcessWindowStyle.Minimized;
    _process = Process.Start(psi);
    _process.WaitForInputIdle();
    SetParent(_process.MainWindowHandle, _panel.Handle);
    // resize embedded application & refresh
    ResizeEmbeddedApp();
    this.Activate();
}

private void ResizeEmbeddedApp()
{
    if (_process == null)
    return;

    SetWindowPos(_process.MainWindowHandle, IntPtr.Zero, 0, 0, (int)_panel.ClientSize.Width,    (int)_panel.ClientSize.Height, SWP_NOZORDER | SWP_NOACTIVATE);
    int style = GetWindowLong(_process.MainWindowHandle, GWL_STYLE);
    style = style & ~((int)WS_CAPTION) & ~((int)WS_THICKFRAME); // Removes Caption bar and the sizing border
    SetWindowLong(_process.MainWindowHandle, GWL_STYLE, style);
}

Is there some different method to use user32.dll to hosting win32 window to WPF using WindowFormHost?

Was it helpful?

Solution

I found the way why it's not hosting to windowsFormsHost. It's because _process.MainWindowHandle is 0. So we need to wait until the Process finish and we can insert it to WindowFormHost. _process.WaitForInputIdle(); not efficient to wait Process finish. So I need to make thread sleep while _process.MainWindowHandle is 0.

while (process.MainWindowHandle == IntPtr.Zero)
{
   Thread.Sleep(100);
   process.Refresh();
}

Just like this answer: c# MainWindowHandle always zero

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top