1

I am in a Win Form application, somewhere in a document I need to call this dialog written in wpf and I want to set the window.owner. The only thing close I found to get the main window of my application is the following.

I want to set the Window.Owner with a Process.GetCurrentProcess().MainWindowHandle, but have no idea how to cast the window handle into a window.

H.B.
  • 166,899
  • 29
  • 327
  • 400

1 Answers1

4

The WindowInteropHelper class allows you to set the owner of a WPF Window using an HWND (as an IntPtr).

In your case, it should be:

WindowInteropHelper wih = new WindowInteropHelper(theWpfWindow);
wih.Owner = Process.GetCurrentProcess().MainWindowHandle;

theWpfWindow.ShowDialog();
Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • Thanks. Why is it so hard to find these kind of details. I have been looking on the web for hours, I could not find anything. – Jean-Marc D. Jul 12 '12 at 18:00
  • @Jean-MarcD. It helps if you know where to look - The interop site is a good resource: http://msdn.microsoft.com/en-us/library/ms742474.aspx – Reed Copsey Jul 12 '12 at 18:01
  • `MainWindowHandle` is not reliable. Please read this: http://stackoverflow.com/q/48288/200443 If there is a popup open, `MainWindowHandle` will return the handle of this popup instead of the real "MainWindow" – Maxence Apr 12 '15 at 11:26