Walkthrough: Hosting an ActiveX Control in WPF by Using XAML

To enable improved interaction with Web browsers, you can use Microsoft ActiveX controls in your WPF-based application. This walkthrough demonstrates how you can use Extensible Application Markup Language (XAML) to host the Microsoft Windows Media Player as a control on a WPF page.

Tasks illustrated in this walkthrough include:

  • Creating the project.

  • Creating the ActiveX control.

  • Hosting the ActiveX control on a Windows Presentation Foundation page.

When you have completed this walkthrough, you will understand how to use Extensible Application Markup Language (XAML) to host ActiveX controls in your WPF-based application.

Note   The dialog boxes and menu commands you see might differ from those described in Help, depending on your active settings or edition. To change your settings, choose Import and Export Settings on the Tools menu. For more information, see Working with Settings.

Prerequisites

You need the following components to complete this walkthrough:

  • Microsoft Windows Media Player installed on the computer where Visual Studio is installed.

  • Visual Studio 2008.

Creating the Project

To create and set up the project

  1. Create a WPF Application project named HostingAxInWpfWithXaml.

  2. Add a Windows Forms Control Library project to the application project, and name the project WmpAxLib. For more information, see How to: Create a New Windows Forms Application Project.

  3. In Solution Explorer, add a reference to the Microsoft Windows Media Player assembly, which is named Wmp.dll.

  4. Open the Toolbox.

  5. Right-click in the Toolbox, and then click Choose Items.

  6. Click the COM Components tab, select the Windows Media Player control, and then click OK to accept the selection.

    The Microsoft Windows Media Player control is added to the Toolbox.

  7. In Solution Explorer, right-click the UserControl1 file, and then click Rename.

  8. Change the name to WmpAxControl.cs or WmpAxControl.vb, depending on the language.

  9. If you are prompted to rename all references, click Yes.

Creating the ActiveX Control

Microsoft Visual Studio automatically generates an AxHost wrapper class for a Microsoft ActiveX control when the control is added to a design surface. The following procedure creates a managed assembly named AxInterop.WMPLib.dll.

To create the ActiveX control

  1. In the Windows Forms Designer, open WmpAxControl.

  2. From the Toolbox, add the Microsoft Windows Media Player control to the design surface.

  3. In the Properties window, set the value of the Microsoft Windows Media Player control's Dock property to Fill.

  4. Press F6 to build the control library.

Hosting the ActiveX Control on a Windows Presentation Foundation Page

To host the ActiveX control

  1. In the HostingAxInWpf project, add a reference to the generated ActiveX interoperability assembly.

    This assembly is named AxInterop.WMPLib.dll and was added to the Debug folder of the WmpAxLib project when you imported the Microsoft Windows Media Player control.

  2. Add a reference to the WindowsFormsIntegration assembly, which is named WindowsFormsIntegration.dll.

  3. Add a reference to the Windows Forms assembly, which is named System.Windows.Forms.dll.

  4. Replace the code in Window1.xaml with the following code.

    The ax namespace mapping establishes a reference to the AxInterop.WMPLib assembly, which contains the AxWindowsMediaPlayer control. The AxWindowsMediaPlayer class is created as a child of the WindowsFormsHost control.

    <Window x:Class="HostingAxInWpfWithXaml.Window1"
        xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms" 
        xmlns:ax="clr-namespace:AxWMPLib;assembly=AxInterop.WMPLib" 
        Title="HostingAxInWpfWithXaml"
          Loaded="WindowLoaded"
        >
    
      <Grid Name="grid1">
    
        <WindowsFormsHost Name="wfh">
          <ax:AxWindowsMediaPlayer x:Name="axWmp"/>
        </WindowsFormsHost>
    
      </Grid>
    
    </Window>
    
  5. Open Window1.xaml.cs, and uncomment the definition of the WindowLoaded method.

  6. Insert the following code to handle the Loaded event.

    private void WindowLoaded(object sender, RoutedEventArgs e)
    {   
        // Get the AxHost wrapper from the WindowsFormsHost control.
        AxWMPLib.AxWindowsMediaPlayer axWmp =
            wfh.Child as AxWMPLib.AxWindowsMediaPlayer;
    
        // Play a .wav file with the ActiveX control.
        axWmp.URL = @"C:\WINDOWS\Media\Windows XP Startup.wav";
    }
    
  7. Press F5 to build and run the application.

See Also

Tasks

Walkthrough: Hosting a Windows Forms Control in WPF by Using XAML

Reference

ElementHost

WindowsFormsHost

Concepts

Walkthrough: Hosting a Windows Forms Composite Control in WPF

Walkthrough: Hosting a WPF Control in Windows Forms

Other Resources

WPF Designer

Migration and Interoperability How-to Topics