逐步解說:在 WPF 中裝載 ActiveX 控制項

若要改善與瀏覽器的互動,您可以在 WPF 型應用程式中使用 Microsoft ActiveX 控制項。 本逐步解說示範如何在 WPF 頁面上將 Microsoft Windows 媒體播放機裝載為控制項。

這個逐步解說中所述的工作包括:

  • 建立專案。

  • 建立 ActiveX 控制項。

  • 在 WPF 頁面上裝載 ActiveX 控制項。

當您完成本逐步解說時,您將瞭解如何在 WPF 型應用程式中使用 Microsoft ActiveX 控制項。

必要條件

您需要下列元件才能完成這個逐步解說:

  • Microsoft Windows 媒體播放機安裝在已安裝 Visual Studio 的電腦上。

  • Visual Studio 2010。

建立專案

建立並設定專案

  1. 建立名為 HostingAxInWpf 的 WPF 應用程式專案。

  2. 將 Windows Forms 控制項程式庫專案新增至方案,並將專案 WmpAxLib 命名為 。

  3. 在 WmpAxLib 專案中,將參考新增至名為 wmp.dll 的 Windows 媒體播放機 元件。

  4. 開啟 [ 工具箱 ]。

  5. 以滑鼠右鍵按一下 [工具箱 ],然後按一下 [ 選擇專案 ]。

  6. 按一下 [ COM 元件] 索引標籤,選取 Windows 媒體播放機 控制項,然後按一下 [ 確定 ]。

    Windows 媒體播放機控制項會新增至 [工具箱 ]。

  7. 在方案總管中,以滑鼠右鍵按一下 UserControl1 檔案,然後按一下 [ 重新命名 ]。

  8. 根據語言,將名稱變更為 WmpAxControl.vbWmpAxControl.cs

  9. 如果系統提示您重新命名所有參考,請按一下 [ ]。

建立 ActiveX 控制項

當控制項加入設計介面時,Visual Studio 會自動為 Microsoft ActiveX 控制項產生 AxHost 包裝函式類別。 下列程式會建立名為 AxInterop.WMPLib.dll 的 Managed 元件。

建立 ActiveX 控制項

  1. 在 Windows Forms 設計工具中開啟 WmpAxControl.vb 或 WmpAxControl.cs。

  2. 從 [ 工具箱 ] 中,將Windows 媒體播放機控制項新增至設計介面。

  3. 在屬性視窗中,將 Windows 媒體播放機 控制項的 Dock 屬性值設定為 Fill

  4. 建置 WmpAxLib 控制項程式庫專案。

在 WPF 頁面上裝載 ActiveX 控制項

裝載 ActiveX 控制項

  1. 在 HostingAxInWpf 專案中,新增所產生 ActiveX 互通性元件的參考。

    此元件名為 AxInterop.WMPLib.dll,當您匯入Windows 媒體播放機控制項時,已新增至 WmpAxLib 專案的 Debug 資料夾。

  2. 新增名為 WindowsFormsIntegration.dll 之 WindowsFormsIntegration 組件的參考。

  3. 新增名為 System.Windows.Forms.dll 之 Windows Forms 元件的參考。

  4. 在 WPF 設計工具中開啟 MainWindow.xaml。

  5. 將專案 grid1 命名為 Grid

    <Grid Name="grid1">
        
    </Grid>
    
  6. 在 [設計檢視] 或 [XAML 檢視] 中,選取 專案 Window

  7. 在 [屬性] 視窗中,按一下 [事件] 索引標籤。

  8. 按兩下 Loaded 事件。

  9. 插入下列程式碼來處理 Loaded 事件。

    此程式碼會建立 控制項的實例, WindowsFormsHost 並將 控制項的 AxWindowsMediaPlayer 實例加入為其子系。

    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 以建置並執行應用程式。

另請參閱