2

I need to host win32 windows in my WPF window, but I need them to act like user controls. Other controls need to be able to appear on top of them, and they should be able to be put into tab controls and such. Is such a thing possible?

Drew DeVault
  • 927
  • 9
  • 22

4 Answers4

3

Not directly. Airspace issues apply here, which prevents you from using the HWND (Win32) window directly like you would other content.

There are various workarounds, such as this AirspaceOverlay control. These function by creating a separate WPF Window without chrome, and "overlaying" it on top of your HWND, moving it as needed.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • Thanks. I don't think this will suit my needs, but I can accept that it's likely it can't be done. – Drew DeVault Jan 28 '13 at 22:09
  • 1
    @DrewDeVault Yeah - they were going to make this work in 4.5, and dropped it. It's a real shame. – Reed Copsey Jan 28 '13 at 22:57
  • 100x thank you for that link to AirspaceOverlay. Saved me many many headaches. Btw. in Net4.5 they added at least the `IsRedirected` property to HwndHost that's supposed to be a great help [reference here](http://www.jonathanantoine.com/2011/09/24/wpf-4-5-%E2%80%93-part-8-no-more-airspace-problems-integrating-wpf-with-win32/). If I understood well, according to that article, with IsRedirected the drawing would be made in compositor and no such tricks would be needed. However, that's 4.5.. Since I worked on 3.5-level now, the transparent-window trick was a life(project) saver. – quetzalcoatl Sep 09 '14 at 22:30
1

In my opinion it can't be done.

You could try to do it with a winform usercontrol and putting it in a windowsformhost control, but even in this this case the windowsformhost it's always on the top, and you can't put other controls over it. The reason for this is known as the AirSpace Issue.

Jonathan
  • 11,809
  • 5
  • 57
  • 91
0

No. At best you can have the native window be hidden and forward graphics and user interaction between it and a WPF control (ala Remote Desktop). But this won't actually be hosting the native window on your WPF window (if the native window interacts with its parent it will be broken).

This is because all WPF content is in a single Win32 window, in a single slot in the Win32 z-order, so other WPF content can't be both below and above this other child control.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
0

Sure, it's possible to embed a Win32 window in a WPF API. For example:

How to embed Window from another application in our WPF window as user control?

Process p = Process.Start(@"application.exe");

p.WaitForInputIdle();
IntPtr appWin = p.MainWindowHandle;

SetParent(appWin, parent);
SetWindowLong(appWin, GWL_STYLE, WS_VISIBLE);
System.Threading.Thread.Sleep(100);
MoveWindow(appWin, 0, 0, ClientRectangle.Width, ClientRectangle.Height, true);

Q: Will your WPF UI be "aware" of this (low-level) Window?
A: No - of course not :)

Q: What do you mean by "without hWndHost"?
If you mean you don't want to use code like the above ... you're probably SOL. At least using WPF...

'Hope that's helps ... at least a little bit...

Community
  • 1
  • 1
paulsm4
  • 114,292
  • 17
  • 138
  • 190