What is the procedure to host the WPF content in Win32 window ?

Platform: WPF| Category: WPF in Win32

This outlines the basic procedure to host WPF content in a Win32 window. The key to hosting WPF content on a Win32 window is the ‘HwndSource’ class. This class wraps the WPF content in a Win32 window, allowing it to be incorporated into your user interface (UI) as a child window. The following approach combines the Win32 and WPF in a single application.
A. Implement your WPF content as a managed class.
B. Implement a Win32 application with C++ / CLI. If you are starting with an existing application and unmanaged C++ code, you can usually enable it to call managed code by changing your project settings to include the /clr compiler flag.
C. Set the threading model to single-threaded apartment (STA).
D. Handle the WM_CREATE notification in your window procedure and do the following :
1.Create a new HwndSource object with the parent window as it’s parent parameter.
2.Create an instance of your WPF content class.
3.Assign a reference to the WPF content object to the RootVisual property of the HwndSource.
4.Get the HWND for the content. The Handle property of the HwndSource object contains the windowhandle (HWND). To get an HWND that you can use in the unmanaged part of your application, cast Handle.ToPointer() to an HWND.
E. Implement a managed class that contains a static field to hold a reference to your WPF content. This class allows you to get a reference to the WPF content from your Win32 code.
F. Assign the WPF content to the static field.
G. Receive notifications from the WPF content by attaching a handler to one or more of the WPF events.
H. Communicate with the WPF content by using the reference that is stored in the static field to set the properties and so on.

Note:
You can also use Extensible Application Markup Language (XAML) to implement your WPF content. However, you will have to compile it separately as a dynamic-link library (DLL) and reference that DLL from your Win32 application. The remainder of the procedure is similar to that outlined above.

Share with

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.