17

I want to use WPF windows in a legacy win32 application. I'd like to behave them in a similar way, like the WPF window always being displayed on top of the win32 window.

For this I'd like to set the owner of the WPF window to the win32 windows, but I got no idea how to achieve this.

Any help here?

Since the answer is hidden behind some link, here the code that did the trick:

System::Windows::Interop::WindowInteropHelper^ helper = gcnew System::Windows::Interop::WindowInteropHelper(myWpfChildWindow);
helper->Owner = (System::IntPtr)myMainWindowHWND;
Sam
  • 28,421
  • 49
  • 167
  • 247

2 Answers2

14

This article shows how to get the handles for both as well as how to make the WPF window become a transparent overlay for the win32 window.

http://dedjo.blogspot.com/2007/04/transparent-wpf-control-over-unmanaged.html

This article uses a WindowInteropHelper to accomplish similar functionality.

http://blogs.msdn.com/wpfsdk/archive/2007/04/03/centering-wpf-windows-with-wpf-and-non-wpf-owner-windows.aspx

MSDN page on WindowInteropHelper:

http://msdn.microsoft.com/en-us/library/system.windows.interop.windowinterophelper.aspx

Hope that helps, Ed

mesbahuk
  • 272
  • 1
  • 2
  • 11
Ed Gonzalez
  • 1,882
  • 13
  • 18
  • 1
    Yes, the 2nd article did the trick, for short the following lines: System::Windows::Interop::WindowInteropHelper^ helper = gcnew System::Windows::Interop::WindowInteropHelper(myWpfChildWindow); helper->Owner = (System::IntPtr)myMainWindowHWND; – Sam Apr 08 '10 at 13:35
  • This works on .NET 4 but not .NET 3.5. On .NET 3.5, changing the Owner property of WindowInteropHelper has no effect. Does anybody know why, and a workaround? – Udi Bar-On Apr 11 '13 at 21:47
  • 2
    Found out why: in .NET 3.5, setting Owner within the window's event handler is too late. You have to set it in the code that creates the window, right after construction. .NET 4.0 seems more lenient, allowing you to set a window's owner from within OnSourceInitialized(). – Udi Bar-On Apr 12 '13 at 10:18
1

How about SetParent()? I know works when making a WPF window an MDI Child of a Windows Form.

Alex K.
  • 171,639
  • 30
  • 264
  • 288