Address: 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 got strated in this walkthrough include:
When you have completed this walkthrough, you will understand how to use Microsoft ActiveX controls in your WPF-based application.
Prerequisites
You need the following components to complete this Walkthrough:
Creating the project
To create and set up the project
Create a WPF Application project named hostingaxinwpf.
Add a Windows Forms control library project to the solution, and name the project wmpaxlib.
In the WmpAxLib project, add a reference to the Windows Media Player assembly, which is named wmp. dll.
Open the Toolbox.
Right-click in the Toolbox, and then click Choose Items.
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.
In Solution Explorer, right-click the UserControl1 file, and then click Rename.
Change the name to WmpAxControl. vb or WmpAxControl. cs, depending on the language.
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
Open WmpAxControl. vb or WmpAxControl. cs in the Windows Forms Designer.
From the Toolbox, add the Windows Media Player control to the design surface.
In the Properties window, set the value of the Windows Media Player control's Dock property to Fill.
Build the WmpAxLib control library project.
Hosting the ActiveX control on a WPF page
To host the ActiveX Control
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.
Add a reference to the WindowsFormsIntegration assembly, which is named WindowsFormsIntegration. dll.
Add a reference to the Windows Forms assembly, which is named System. Windows. Forms. dll.
Open MainWindow. xaml in the WPF Designer.
Name the Grid element grid1.
Xamlcopy<Grid Name="grid1"></Grid>
In Design view or XAML view, select the Window element.
In the Properties window, click the Events tab.
Double-click the Loaded event.
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 # vbcopyprivate 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";}
Press F5 to build and run the application.
See also
Referenceelementhostwindowsformshostconceptswalkthrough: hosting a Windows Forms Composite Control in wpfwalkthrough: hosting a WPF Composite Control in Windows formsother resourceswpf designer