逐步解說:將 ActiveX 控制項裝載在 WPF 中Walkthrough: Hosting an ActiveX Control in WPF

若要啟用與瀏覽器的改進互動,您可以在應用程式中使用 Microsoft ActiveX 控制項 WPFWPFTo enable improved interaction with browsers, you can use Microsoft ActiveX controls in your WPFWPF-based application. 本逐步解說會示範如何將 Microsoft Windows Media Player 裝載為頁面上的控制項 WPFWPFThis walkthrough demonstrates how you can host the Microsoft Windows Media Player as a control on a WPFWPF page.

這個逐步解說中所述的工作包括:Tasks illustrated in this walkthrough include:

  • 建立專案。Creating the project.

  • 建立 ActiveX 控制項。Creating the ActiveX control.

  • 將 ActiveX 控制項裝載在 WPF 頁面上。Hosting the ActiveX control on a WPF Page.

當您完成此逐步解說時,您將瞭解如何在您的應用程式中使用 Microsoft ActiveX 控制項 WPFWPFWhen you have completed this walkthrough, you will understand how to use Microsoft ActiveX controls in your WPFWPF-based application.

必要條件Prerequisites

您需要下列元件才能完成這個逐步解說:You need the following components to complete this walkthrough:

  • 在安裝 Visual Studio 的電腦上安裝 Microsoft Windows Media Player。Microsoft Windows Media Player installed on the computer where Visual Studio is installed.

  • Visual Studio 2010。Visual Studio 2010.

建立專案Creating the Project

建立並設定專案To create and set up the project

  1. 建立名為的 WPF 應用程式專案 HostingAxInWpfCreate a WPF Application project named HostingAxInWpf.

  2. 將 Windows Forms 控制項程式庫專案新增至方案,並為專案命名 WmpAxLibAdd a Windows Forms Control Library project to the solution, and name the project WmpAxLib.

  3. 在 WmpAxLib 專案中,加入名為 wmp.dll 之 Windows Media Player 元件的參考。In the WmpAxLib project, add a reference to the 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. 按一下 [ COM 元件 ] 索引標籤,選取 Windows Media Player 控制項,然後按一下 [確定]Click the COM Components tab, select the Windows Media Player control, and then click OK.

    Windows Media Player 控制項會加入至 [ 工具箱]。The Windows Media Player control is added to the Toolbox.

  7. 在方案總管中,以滑鼠右鍵按一下 UserControl1 檔案,然後按一下 [ 重新命名]。In Solution Explorer, right-click the UserControl1 file, and then click Rename.

  8. 將名稱變更為 WmpAxControl.vbWmpAxControl.cs ,視語言而定。Change the name to WmpAxControl.vb or WmpAxControl.cs, depending on the language.

  9. 如果系統提示您重新命名所有參考,請按一下 [是]If you are prompted to rename all references, click Yes.

建立 ActiveX 控制項Creating the ActiveX Control

AxHost當控制項加入至設計介面時,Visual Studio 會自動產生 Microsoft ActiveX 控制項的包裝函式類別。Visual Studio automatically generates an AxHost wrapper class for a Microsoft ActiveX control when the control is added to a design surface. 下列程式會建立名為 AxInterop.WMPLib.dll 的 managed 元件。The following procedure creates a managed assembly named AxInterop.WMPLib.dll.

若要建立 ActiveX 控制項To create the ActiveX control

  1. 在 Windows Form 設計工具中開啟 WmpAxControl .vb 或 WmpAxControl。Open WmpAxControl.vb or WmpAxControl.cs in the Windows Forms Designer.

  2. 在 [ 工具箱] 中,將 Windows Media Player 控制項加入至設計介面。From the Toolbox, add the Windows Media Player control to the design surface.

  3. 在 [屬性視窗中,將 Windows Media Player 控制項的屬性值設定 DockFillIn the Properties window, set the value of the Windows Media Player control's Dock property to Fill.

  4. 建立 WmpAxLib 控制項程式庫專案。Build the WmpAxLib control library project.

將 ActiveX 控制項裝載在 WPF 頁面上Hosting the ActiveX Control on a WPF Page

裝載 ActiveX 控制項To host the ActiveX control

  1. 在 HostingAxInWpf 專案中,加入所產生的 ActiveX 互通性元件的參考。In the HostingAxInWpf project, add a reference to the generated ActiveX interoperability assembly.

    這個元件命名為 AxInterop.WMPLib.dll,並在您匯入 Windows Media Player 控制項時,加入至 WmpAxLib 專案的 Debug 資料夾中。This assembly is named AxInterop.WMPLib.dll and was added to the Debug folder of the WmpAxLib project when you imported the Windows Media Player control.

  2. 加入 WindowsFormsIntegration 元件的參考,名為 WindowsFormsIntegration.dll。Add a reference to the WindowsFormsIntegration assembly, which is named WindowsFormsIntegration.dll.

  3. 將參考新增至名為 System.Windows.Forms.dll 的 Windows Forms 元件。Add a reference to the Windows Forms assembly, which is named System.Windows.Forms.dll.

  4. 在 WPF 設計工具中開啟 MainWindow。Open MainWindow.xaml in the WPF Designer.

  5. 命名 Grid 元素 grid1Name the Grid element grid1.

    <Grid Name="grid1">
        
    </Grid>
    
  6. 在設計檢視或 XAML 視圖中,選取 Window 元素。In Design view or XAML view, select the Window element.

  7. 在 [屬性視窗中,按一下 [ 事件 ] 索引標籤。In the Properties window, click the Events tab.

  8. 按兩下 Loaded 事件。Double-click the Loaded event.

  9. 插入下列程式碼來處理 Loaded 事件。Insert the following code to handle the Loaded event.

    此程式碼會建立控制項的實例 WindowsFormsHost ,並將控制項的實例加入為其子系 AxWindowsMediaPlayerThis code creates an instance of the WindowsFormsHost control and adds an instance of the AxWindowsMediaPlayer control as its child.

    private void Window_Loaded(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.
        WmpAxLib.AxWindowsMediaPlayer axWmp = new WmpAxLib.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\tada.wav";
    }
    
    Private Sub Window_Loaded(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\tada.wav"
    
    End Sub
    
  10. 按 F5 以建置並執行應用程式。Press F5 to build and run the application.

另請參閱See also