演练:在 Windows Presentation Foundation 中承载 ActiveX 控件

更新:2007 年 11 月

若要改进与浏览器的交互,可以在基于 WPF 的应用程序中使用 Microsoft ActiveX 控件。本演练演示如何在 WPF 页上将 Microsoft Windows Media Player 作为控件来承载。

本演练涉及以下任务:

  • 创建项目。

  • 创建 ActiveX 控件。

  • 在 Windows Presentation Foundation 页上承载 ActiveX 控件。

有关本演练中所显示任务的完整代码清单,请参见在 Windows Presentation Foundation 中承载 ActiveX 控件的示例

在完成本演练之后,您将了解如何在基于 WPF 的应用程序中使用 Microsoft ActiveX 控件。

注意 显示的对话框和菜单命令可能与“帮助”中所述的有所不同,具体取决于当前的设置或版本。若要更改设置,请在“工具”菜单上选择“导入和导出设置”。有关更多信息,请参见 Visual Studio 设置

先决条件

您需要以下组件来完成本演练:

  • 在装有 Visual Studio 的计算机上安装 Microsoft Windows Media Player。

  • Visual Studio 2008.

创建项目

创建和设置项目

  1. 创建名为 HostingAxInWpf 的 WPF 应用程序项目。

  2. 向该应用程序项目中添加一个 Windows 窗体控件库项目,然后将该窗体控件库项目命名为 WmpAxLib。有关更多信息,请参见“添加新项目”对话框

  3. 在解决方案资源管理器中,添加一个对名为 wmp.dll 的 Microsoft Windows Media Player 程序集的引用。

  4. 打开“工具箱”。

  5. 在“工具箱”中右击,再单击“选择项”。

  6. 单击“COM 组件”选项卡,选择“Windows Media Player”控件,再单击“确定”接受选定内容。

    该 Microsoft Windows Media Player 控件即会添加到“工具箱”中。

  7. 在解决方案资源管理器中,右击“UserControl1”文件,再单击“重命名”。

  8. 根据您所使用的语言,将名称更改为 WmpAxControl.cs 或 WmpAxControl.vb。

  9. 如果系统提示您重命名所有的引用,请单击“是”。

创建 ActiveX 控件

在将 Microsoft ActiveX 控件添加到设计图面上时,Microsoft Visual Studio 会自动为该控件生成一个 AxHost 包装类。下面的过程会创建一个名为 AxInterop.WMPLib.dll 的托管程序集。

创建 ActiveX 控件

  1. 在 Windows 窗体设计器中,打开 WmpAxControl。

  2. 从“工具箱”中,将 Microsoft Windows Media Player 控件添加设计图面上。

  3. 在“属性”窗口中,将 Microsoft Windows Media Player 控件的 Dock 属性值设置为 Fill

  4. 按 F6 生成控件库。

在 Windows Presentation Foundation 页上承载 ActiveX 控件

承载 ActiveX 控件

  1. 在 HostingAxInWpf 项目中,添加一个对所生成的 ActiveX 互操作程序集的引用。

    此程序集的名称为 AxInterop.WMPLib.dll。当您导入 Microsoft Windows Media Player 控件时,此程序集会添加到 WmpAxLib 项目的 Debug 文件夹中。

  2. 添加一个对名为 WindowsFormsIntegration.dll 的 WindowsFormsIntegration 程序集的引用。

  3. 添加一个对名为 System.Windows.Forms.dll 的 Windows 窗体程序集的引用。

  4. 打开 Window1.xaml,并将所生成的代码替换为下面的代码。

    <Window x:Class="Window1"
        xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
        Title="HostingAxInWpf"
        Loaded="WindowLoaded" 
        >
    
        <Grid Name="grid1">
    
        </Grid>
    
    </Window>
    
    <Window x:Class="HostingAxInWpf.Window1"
        xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
        Title="HostingAxInWpf"
        Loaded="WindowLoaded" 
        >
    
        <Grid Name="grid1">
    
        </Grid>
    
    </Window>
    
  5. 打开 Window1.xaml.cs,并取消对 WindowLoaded 方法定义的注释。

  6. 插入下面的代码以处理 Loaded 事件。

    此代码会创建一个 WindowsFormsHost 控件实例,并添加一个 AxWindowsMediaPlayer 控件实例来作为其子级。

    Private Sub WindowLoaded(ByVal sender As Object, ByVal e As RoutedEventArgs)
    
        ' Create the interop host control.
        Dim host As New System.Windows.Forms.Integration.WindowsFormsHost()
    
        ' Create the ActiveX control.
        Dim axWmp As 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.
        Me.grid1.Children.Add(host)
    
        ' Play a .wav file with the ActiveX control.
        axWmp.___URL = "C:\WINDOWS\Media\Windows XP Startup.wav"
    
    End Sub
    
    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. 按 F5 生成并运行应用程序。

请参见

任务

在 Windows Presentation Foundation 中承载 ActiveX 控件的示例

概念

演练:在 Windows Presentation Foundation 中承载 Windows 窗体复合控件

演练:在 Windows 窗体中承载 Windows Presentation Foundation 控件

参考

ElementHost

WindowsFormsHost

其他资源

WPF 设计器

迁移和互操作性帮助主题