Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF modal window as tool window in WinForms disappears

I was wondering if anyone can help me with this rather baffling situation. I have a WPF form myForm which I am displaying modally in a WinForm application (without using an ElementHost). Everything works fine if I use the default WindowStyle and it is shown in the taskbar. However now I don't want the form to show in the taskbar or contain the minimize button, therefore I have done the following:

MyForm myForm = new MyForm();
myForm.ShowInTaskbar = false;
myForm.WindowStyle = System.Windows.WindowStyle.ToolWindow;
myForm.WindowStartupLocation =System.Windows.WindowStartupLocation.CenterOwner;
myForm.ShowDialog();

Now, the wpf form displays as expected modally and without the minimize button. If I now select the "parent" winform application in the taskbar, the wpf form disappears and there doesn't seem to be any way of returning to it! I have read this which is similar but not the same (pure WPF application), so I can understand why the main app does not appear in the ALT+TAB menu, but can anyone tell me how I can return to the wpf form?

Thanks in advance.

like image 530
Jeb Avatar asked Jun 11 '12 14:06

Jeb


1 Answers

The use of WindowsInteropHelper allows you to wrap a hosted WPF-form using a Winforms-form and set its parent as a Winforms control, which prevents the WPF form from disappearing (as also pointed out by dowhilefor and Hans Passant)

E.g.:

// Use the interop helper for the specified wpf window hosted by the win32 owner
WindowInteropHelper wih = new WindowInteropHelper(wpfForm);
wih.Owner = this.someWin32FormsControl.Handle;
like image 187
Jeb Avatar answered Oct 20 '22 15:10

Jeb