Dispatches from Maine

Just another person of little note writing about ordinary things. That I reside in Maine is icing on the cake.

17 September 2005

PDC05: Day Four (Windows Presentation Foundation "Avalon" in Win32/MFC Applications"

The next step was the session which pushed me over the edge and made my brain bubble over with ideas. Nick Kramer presented his excellent session, "Windows Presentation Foundation ("Avalon")in Win32/MFC Applications." His lead off made me cheer, "So, you have seen all this good stuff about WPF and XAML, but you have a big application back at work. You aren't going to be able to go back and rewrite it all, so now what" (paraphrased). He then share the secret of how to move to Avalon without throwing away your investment: HwndSource and HwndHost.

If you have an existing application and you want to be able to add Avalon objects to it, then you make use of HwndSource to place Avalon objects into an existing HWND. A good example of this would be taking an existing dialog, removing the UI code and replacing it with XAML and managed code. This is a great way to add Avalon to an existing application, particular for us here at DeLorme. If you are going to do this, be sure to associate "[System:STAThreadAttribute]" with the "main()" method to ensure it loads the CLR properly at startup. Another caveat is that you cannot have compiled XAML and unmanaged code in the same assembly. You could have uncompiled XAML and then compile it at runtime, you could have two assemblies to separate the two, or you could create the WPF controls by hand rather than with XAML.

// WPF inside an HWND.
HwndSource source = new HwndSource(…);
source.RootVisual = myWpfStuff;
HWND hwnd = source.Handle;

If, on the other hand, you want to write a new Avalon application, but draw your special Win32/MFC controls into it, then you would use HwndHost. It is worth noting that though Avalon supports HRGNs, it does not support layered windows.

0 Comments:

Post a Comment

Links to this post:

Create a Link

<< Home