Demonstra Passo a passo: Hospedagem de um controle ActiveX no WPF

Atualização: August 2010

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.

  • Hospedando o controle de ActiveX em uma página do WPF.

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

Pré-requisitos

You need the following components to complete this walkthrough:

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

  • Visual Studio 2010.

Creating the Project

To create and set up the project

  1. Create a WPF Application project named HostingAxInWpf.

  2. Adicionar um projeto de biblioteca de controle de formulários do Windows para a solução e nomeie o projeto WmpAxLib.

  3. No projeto WmpAxLib, adicione uma referência ao assembly do Windows Media Player, que é chamada de Wmp. dll.

  4. Open the Toolbox.

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

  6. Clique o Componentes COM guia, selecione o De Windows Media Player de controle e clique em OK.

    O controle de Windows Media Player é adicionado para o caixa de ferramentas.

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

  8. Altere o nome para WmpAxControl.vb ou WmpAxControl.cs, dependendo do idioma.

  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. Abra o WmpAxControl.vb ou WmpAxControl.cs no Windows Forms Designer.

  2. Do caixa de ferramentas, adicione o controle de Windows Media Player a superfície de design.

  3. Na janela Properties, defina o valor do controle de Windows Media Player Dock propriedade para Fill.

  4. Construa o projeto de biblioteca de controle de WmpAxLib.

Hospedando o controle de ActiveX em uma página do WPF

To host the ActiveX control

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

    Este assembly é denominado AxInterop.WMPLib.dll e foi adicionado para a pasta de depuração do projeto WmpAxLib ao importar o controle de Windows Media Player.

  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. Abra MainWindow. XAML no WPF Designer.

  5. Nome do Grid elemento grid1.

    <Grid Name="grid1">
    
    </Grid>
    
  6. No modo de exibição de Design ou XAML, selecione o Window elemento.

  7. Na janela Propriedades, clique na eventos guia.

  8. Clique duas vezes o Loaded de evento.

  9. 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 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
    
    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.
        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\tada.wav";
    }
    
  10. Pressione F5 para criar e executar o aplicativo.

Consulte também

Referência

ElementHost

WindowsFormsHost

Conceitos

Demonstra Passo a passo: Hospedando um controle Windows Forms composto no WPF

Demonstra Passo a passo: Hospedando um controle composto do WPF no Windows Forms

Outros recursos

WPF Designer

Histórico de alterações

Date

History

Motivo

August 2010

Atualizado para 2010 de Visual Studio.

Comentários do cliente.