Walkthrough: Hosting an ActiveX Control in Windows Presentation Foundation

To enable improved interaction with browsers, you can use Microsoft ActiveX controls in your WPF-based application. This walkthrough demonstrates how you can 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.

For a complete code listing of the tasks shown in this walkthrough, see Hosting an ActiveX Control in Windows Presentation Foundation Sample.

When you have completed this walkthrough, you will understand how to use Microsoft ActiveX controls in your WPF-based application.

NoteNote:

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.

To complete this walkthrough, you will need:

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

  • Development Tools for .NET Framework 3.0, which enable you to create a WPF application project. For information on installing these tools, see Installation Instructions for the Windows SDK.

To create and set up the project

  1. Create a Windows Application (WPF) project named HostingAxInWpf.

  2. Add a Windows Control Library project to the application project, and name the project WmpAxLib.

  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.

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.

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.

    The default location for this file is %programfiles%\Reference Assemblies\Microsoft\Framework\v3.0\WindowsFormsIntegration.dll.

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

  4. Open Window1.xaml, and replace the generated code with the following code.

    <Window x:Class="HostingAxInWpf.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="HostingAxInWpf"
    	Loaded="WindowLoaded" 
        >
    	
        <Grid Name="grid1">
            
        </Grid>
    	
    </Window>
    
    
    <Window x:Class="Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="HostingAxInWpf"
    	Loaded="WindowLoaded" 
        >
    	
        <Grid Name="grid1">
            
        </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.

    This code creates an instance of the WindowsFormsHost control and adds an instance of the AxWindowsMediaPlayer control as its child.

    private void WindowLoaded(object sender, RoutedEventArgs e) 
    {
        // Create the interop host control.
        System.Windows.Forms.Integration.WindowsFormsHost host =
            new System.Windows.Forms.Integration.WindowsFormsHost();
    
        // Create the ActiveX control.
        AxWMPLib.AxWindowsMediaPlayer axWmp = new AxWMPLib.AxWindowsMediaPlayer();
    
        // Assign the ActiveX control as the host control's child.
        host.Child = axWmp;
    
        // Add the interop host control to the Grid
        // control's collection of child controls.
        this.grid1.Children.Add(host);
    
        // 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.

Community Additions

ADD
Show: