Live Chat Icon For mobile
Live Chat Icon

WPF FAQ - WPF in Win32

Find answers for the most frequently asked questions
Expand All Collapse All

You can choose to keep your Windows Forms application primarily intact and migrate portions of your application to WPF where it makes sense. You can do this by identifying those areas of your application that are a good fit for WPF features and convert only those areas to WPF. For example, you may want to convert only a few of your forms to WPF and keep the rest of the application based on Windows Forms. Using this method, you could simply popup instances of WPF pages or windows from your Windows Forms application. Additionally, you may want to actually swap-out Windows Forms controls for WPF controls on a form resulting in a hybrid form when the controls co-exist in peaceful harmony.

Permalink

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.

Permalink

This can be done with the help of the following code :

[C#[

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.Integration;
namespace WpfUserControlHost
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            // Create the ElementHost control for hosting the
            // WPF UserControl.
            ElementHost host = new ElementHost();
            host.Dock = DockStyle.Fill;

            // Create the WPF UserControl.
            HostingWpfUserControlInWf.UserControl1 uc =
                new HostingWpfUserControlInWf.UserControl1();
            // Assign the WPF UserControl to the ElementHost control’s
            // Child property.
            host.Child = uc;
            // Add the ElementHost control to the form’s
            // collection of child controls.
            this.Controls.Add(host);
        }
    }
}

Permalink

First add references to the WPF namespaces (PresentationCore, PresentationFramework, UIAutomationProvider, UIAutomationTypes, and WindowsBase). Next create an instance of the ’ElementHost’ control and the control you wish to embed in the Windows Forms application and then hook that control up to the ElementHost control. Then simply add the ElementHost control to your Forms control collection :

[C#]

        ElementHost host = new ElementHost();
System.Windows.Controls.ListBox wpfListBox = 
  new System.Windows.Controls.ListBox();

for (int i = 0; i < 10; i++)
{
  wpfListBox.Items.Add('Item ' + i.ToString());
}

host.Dock = DockStyle.Fill;
host.Controls.Add(wpfListBox);
this.panel1.Controls.Add(host);

However, if you want to use XAML to describe the WPF control that you want to use in the Windows Forms application, you need to add an ’Avalon’ UserControl item to your project. This will create a UserControl1.xaml file and a UserControl1.xaml.cs file. You can then modify the UserControl1.xaml file to contain whatever XAML you wish to have to describe your control. Then you could simply create an instance of this control and add it to the ElementHost control as in the above example :

[C#]

ElementHost host = new ElementHost();
UserControl1 uc1 = new UserControl1();
host.Controls.Add(uc1);
host.Dock = DockStyle.Fill;
this.panel1.Controls.Add(host);

In addition, you will have to modify the project file because the Windows Application will not know what to do with the XAML file. You will have to open the project file (.csproj, .vbproj, etc.) in an editor like Notepad and then scroll to the bottom. You will see the following line:

[XAML]

        <Import Project='$(MSBuildBinPath)\Microsoft.CSharp.targets' />
        You will need to copy this line and paste it just below the above line and then change 'CSharp' to 'WinFX' so that the two lines look like:
        <Import Project='$(MSBuildBinPath)\Microsoft.CSharp.targets' />
        <Import Project='$(MSBuildBinPath)\Microsoft.WinFx.targets' />

Now save this file and reload the project using VS and run the application.

Permalink

Share with

Share on twitter
Share on facebook
Share on linkedin

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

Please submit your question and answer.