Walkthrough: Hosting an ActiveX Control in WPF

本文详细介绍了如何在基于WPF的应用中使用Microsoft ActiveX控件来集成Windows Media Player,并通过代码示例演示了创建项目、添加控件、配置属性及播放媒体文件的过程。

原文地址:http://msdn.microsoft.com/en-us/library/ms742735.aspx

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 WPF Page.

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

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.

To create and set up the project

  1. Create a WPF Application project named HostingAxInWpf.

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

  3. 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. Click the COM Components tab, select the Windows Media Player control, and then click OK.

    The 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.vb or WmpAxControl.cs, 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. Open WmpAxControl.vb or WmpAxControl.cs in the Windows Forms Designer.

  2. From the Toolbox, add the Windows Media Player control to the design surface.

  3. In the Properties window, set the value of the Windows Media Player control's Dock property to Fill.

  4. Build the WmpAxLib control library project.

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 Windows Media Player control.

  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. Open MainWindow.xaml in the WPF Designer.

  5. Name the Grid element grid1.

    XAML
    <Grid Name="grid1">
    
    </Grid>
    
  6. In Design view or XAML view, select the Window element.

  7. In the Properties window, click the Events tab.

  8. Double-click the Loaded event.

  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.

    C#
    VB
    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. Press F5 to build and run the application.

转载于:https://www.cnblogs.com/jiewei915/archive/2012/12/11/2812566.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值