* Reset branch for WPF changes * Convert BMP to PNG; fix link-out-of-scope err * Add snippets for WPF... 6794 files!!!! * Add missing snippets * update file updated between migration * Fix paths to include * update breadcrumb and toc * fix index links * fix index links * fix index links * fix markdown
4.8 KiB
title, titleSuffix, ms.date, dev_langs, helpviewer_keywords, ms.assetid
| title | titleSuffix | ms.date | dev_langs | helpviewer_keywords | ms.assetid | ||||
|---|---|---|---|---|---|---|---|---|---|
| Host an ActiveX control in WPF | 03/30/2017 |
|
|
1931d292-0dd1-434f-963c-dcda7638d75a |
Walkthrough: Hosting an ActiveX Control in WPF
To enable improved interaction with browsers, you can use Microsoft ActiveX controls in your [!INCLUDETLA2#tla_winclient]-based application. This walkthrough demonstrates how you can host the Microsoft Windows Media Player as a control on a [!INCLUDETLA2#tla_winclient] 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 [!INCLUDETLA2#tla_winclient]-based application.
Prerequisites
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
-
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.vborWmpAxControl.cs, depending on the language. -
If you are prompted to rename all references, click Yes.
Creating the ActiveX Control
Visual Studio automatically generates an xref:System.Windows.Forms.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 xref:System.Windows.Forms.Control.Dock%2A property to xref:System.Windows.Forms.DockStyle.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 xref:System.Windows.Controls.Grid element
grid1.[!code-xamlHostingAxInWpf#1]
-
In Design view or XAML view, select the xref:System.Windows.Window element.
-
In the Properties window, click the Events tab.
-
Double-click the xref:System.Windows.FrameworkElement.Loaded event.
-
Insert the following code to handle the xref:System.Windows.FrameworkElement.Loaded event.
This code creates an instance of the xref:System.Windows.Forms.Integration.WindowsFormsHost control and adds an instance of the
AxWindowsMediaPlayercontrol as its child.[!code-csharpHostingAxInWpf#11] [!code-vbHostingAxInWpf#11]
-
Press F5 to build and run the application.