WPF 4.5 – Part 8 : No more airspace problems (Integrating WPF with win32 : finally I can use the WebBrowser control !)


Integrating win 32 component is already possible in WPF 4.0, even in WPF 3.5 but there is the well-know issue : airspace problems. Each render technology belongs to only one airspace only. So when you place Win32 components in your WPF application they behave as black hole for input, render transformation are not (well) applied, etc.

With this 4.5 release, the WPF team solves this drawback. In this post more explanation and an example of how it improves developers’ life will be gived.

This post is a part of a serie on WPF 4.5 new features.

What can’t be done in WPF4.0

I’ll only write about the integration of Winform/Win32 controls into WPF because this is the only subject I know wel.
Before WPF 4.5, if you add a Win32 component into a wpf application using the hwndhost control you have some limitations:

  • Resizing is limited because only the container(hwndhost) will be resized, not the contained component itself.
  • Forget about rotation and skewing.
  • The hosted component is a black hole for your application : always at the top (forget the z-order!).
  • Opacity can’t be applied to an hosted Win32 control.
  • VisualBrush do not work with Win32 controls.
  • etc.

This is really annoying when you are used to WPF and this is why hosting a Win32 control into a WPF 4.0 apps is rarely done.

What’s new in WPF 4.5 ?

There is two main properties which are new in WPF 4.5 and related to this feature on the HwndHost class: IsRedirected and CompositionMode.

IsRedirected is a boolean which can be understand as : “do we solve airspace issue and share the screen between Win32 controls and WPF? You may wonder as I am why this is not set to true by default. I have the opinion that enabling it can lead to performance issues and is something you have to do only if needed.

CompositionMode is an enumeration which tells how deep the integration has to be done :

  • None: this is the default behavior and no integration is done: the airspace problems are still here 🙁
  • OutputOnly: The airspace problem are solved but the user (and the input system) can’t interact with the hosted win32 component.
  • Full: the airspace problems are solved and interaction with the win32 component is possible.

This two properties have to be set before any rendering is done otherwise an exception will be thrown.

An example with the WebBrowser control

You may know that the WPF WebBrowser is in fact the Win32 one disguised as a WPF control. It means that every drawbacks that we have seen before are present when you use it on a application.

With WPF 4.5 you can now use it as if it is a standard control. All you have to do is to set the two properties we’ve seen before to the correct values.

So let’s say that I want to display a WebBrowser with an opacity of 0.6 and that I want it to be scaled to the half of it size. Because I am evil, I also want to display a VisualBrus of it inside a Rectangle just next to it. The XAML meeting these requirements will then be :
[xml]<UniformGrid Columns="2" x:Name="grid">
<WebBrowser x:Name="_webBrowser" Source="http://www.jonathanAntoine.com"
Margin="5" Opacity="0.6">
<ScaleTransform ScaleX="0.5" ScaleY="0.5" />
<Rectangle Margin="5">
<VisualBrush Visual="{Binding ElementName=_webBrowser}" />

Then to make it works, I have to activate the redirecting and to set the composition mode of the WebBrowser internal HwndHost. The WPF team thought at this usage and you can find the same property on it. I do it in the Window constructor to be sure that no rendering has be done:
[csharp]public MainWindow()

_webBrowser.CompositionMode = System.Windows.Interop.CompositionMode.Full;
_webBrowser.IsRedirected = true;

That’s all. Here is a comparison of the rendering in WPF 4.0 and 4.5:

This Demo project is available in my DropBox folder after registration.


PS: as pointed out in the .NET 4.5 readme, this feature might not be included in the final release. I hope it will !


11 Responses

  1. Jose Fajardo

    27/09/2011 0 h 52 min

    Just making sure that people are aware that this feature may not make it in for the final version..

    The readme for 4.5 states the following :

    ==================================================================================== HwndHost redirection does not function correctly

    The .NET Framework 4.5 Developer Preview includes an HwndHost redirection feature. However, this feature has several known issues.

    To resolve this issue:

    There is no workaround. We recommend that you do not use HwndHost redirection in this prerelease. The feature is still being evaluated and might not be included in the final release of the .NET Framework 4.5.

    • jmix90

      27/09/2011 0 h 58 min

      Nothing that can't be solve I hope, I'd really like to see this feature released !

  2. Robert4WPF

    27/09/2011 15 h 31 min

    Bah, I missed that in the readme. If they don’t include it, there is no reason for my project to upgrade to 4.5. Very disappointing.

  3. Héctor

    27/09/2011 22 h 49 min

    I have barely used WPF ever, but I think this feature should be added no matter what.

  4. Sergiu

    05/06/2012 19 h 05 min

    this is not the issue anymore, they have completely removed the CompositionMode and IsRedirected properties from the hwndhost 🙁

Comments are closed.