WPF 4.5 – Part 8: Solving Airspace Problems
Join the DZone community and get the full member experience.
Join For Free
integrating a win 32 component is already possible in wpf 4.0, even in wpf 3.5 but there is the well-know issue called
airspace problems
.
each render technology belongs to only one airspace only. so when you
place win32 components in your wpf application they behave as a black hole
for input and render transformations 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 given.
this post is a part of a series 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 well.
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 :
<uniformgrid columns="2" x:name="grid"> <webbrowser x:name="_webbrowser" source="http://www.jonathanantoine.com" margin="5" opacity="0.6"> <webbrowser.rendertransform> <scaletransform scalex="0.5" scaley="0.5" /> </webbrowser.rendertransform> </webbrowser> <rectangle margin="5"> <rectangle.fill> <visualbrush visual="{binding elementname=_webbrowser}" /> </rectangle.fill> </rectangle> </uniformgrid> |
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:
public mainwindow() { initializecomponent(); _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.
Published at DZone with permission of Jon Antoine, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments