업데이트: 2007년 11월
브라우저와의 상호 작용을 개선하기 위해 WPF 기반 응용 프로그램에서 Microsoft ActiveX 컨트롤을 사용할 수 있습니다. 이 연습에서는 Microsoft Windows Media Player를 WPF 페이지의 컨트롤로 호스팅하는 방법을 보여 줍니다.
이 연습에서 수행할 작업은 다음과 같습니다.
프로젝트 만들기
ActiveX 컨트롤 만들기
Windows Presentation Foundation 페이지에서 ActiveX 컨트롤 호스팅
이 연습에 표시된 작업의 전체 코드 목록은 Windows Presentation Foundation에서 ActiveX 컨트롤 호스팅 샘플을 참조하십시오.
이 연습을 마치면 WPF 기반 응용 프로그램에서 Microsoft ActiveX 컨트롤을 사용하는 방법을 이해하게 됩니다.
참고 표시되는 대화 상자와 메뉴 명령은 실제 설정이나 버전에 따라 도움말에서 설명하는 것과 다를 수 있습니다. 설정을 변경하려면 도구 메뉴에서 설정 가져오기 및 내보내기를 선택합니다. 자세한 내용은 Visual Studio 설정을 참조하십시오.
사전 요구 사항
이 연습을 완료하려면 다음 구성 요소가 필요합니다.
Visual Studio가 설치된 컴퓨터에 설치된 Microsoft Windows Media Player
Visual Studio 2008.
프로젝트 만들기
프로젝트를 만들고 설정하려면
HostingAxInWpf라는 WPF 응용 프로그램 프로젝트를 만듭니다.
Windows Forms 컨트롤 라이브러리 프로젝트를 이 응용 프로그램 프로젝트에 추가하고 프로젝트의 이름을 WmpAxLib로 지정합니다. 자세한 내용은 새 프로젝트 추가 대화 상자를 참조하십시오.
솔루션 탐색기에서 wmp.dll이라는 Microsoft Windows Media Player 어셈블리에 대한 참조를 추가합니다.
도구 상자를 엽니다.
도구 상자에서 마우스 오른쪽 단추를 클릭한 다음 항목 선택을 클릭합니다.
COM 구성 요소 탭을 클릭하고 Windows Media Player 컨트롤을 선택한 다음 확인을 클릭하여 선택 내용을 적용합니다.
Microsoft Windows Media Player 컨트롤이 도구 상자에 추가됩니다.
솔루션 탐색기에서 UserControl1 파일을 마우스 오른쪽 단추로 클릭한 다음 이름 바꾸기를 클릭합니다.
언어에 따라 이름을 WmpAxControl.cs 또는 WmpAxControl.vb로 변경합니다.
모든 참조의 이름을 바꾸라는 메시지가 나타나면 예를 클릭합니다.
ActiveX 컨트롤 만들기
컨트롤이 디자인 화면에 추가되면 Microsoft Visual Studio는 Microsoft ActiveX 컨트롤에 대한 AxHost 래퍼 클래스를 자동으로 생성합니다. 다음 절차에서는 AxInterop.WMPLib.dll이라는 관리되는 어셈블리를 만듭니다.
ActiveX 컨트롤을 만들려면
Windows Forms 디자이너에서 WmpAxControl을 엽니다.
도구 상자에서 Microsoft Windows Media Player 컨트롤을 디자인 화면에 추가합니다.
속성 창에서 Microsoft Windows Media Player 컨트롤의 Dock 속성 값을 Fill로 설정합니다.
F6 키를 눌러 컨트롤 라이브러리를 빌드합니다.
Windows Presentation Foundation 페이지에서 ActiveX 컨트롤 호스팅
ActiveX 컨트롤을 호스팅하려면
HostingAxInWpf 프로젝트에서 생성된 ActiveX 상호 운용성 어셈블리에 대한 참조를 추가합니다.
이 어셈블리는 이름이 AxInterop.WMPLib.dll이며 Microsoft Windows Media Player 컨트롤을 가져올 때 WmpAxLib 프로젝트의 Debug 폴더에 추가되었습니다.
WindowsFormsIntegration.dll이라는 WindowsFormsIntegration 어셈블리에 대한 참조를 추가합니다.
System.Windows.Forms.dll이라는 Windows Forms 어셈블리에 대한 참조를 추가합니다.
Window1.xaml을 열고 생성된 코드를 다음 코드로 바꿉니다.
<Window x:Class="Window1" xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml" Title="HostingAxInWpf" Loaded="WindowLoaded" > <Grid Name="grid1"> </Grid> </Window>
<Window x:Class="HostingAxInWpf.Window1" xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml" Title="HostingAxInWpf" Loaded="WindowLoaded" > <Grid Name="grid1"> </Grid> </Window>
Window1.xaml.cs를 열고 WindowLoaded 메서드 정의의 주석 처리를 제거합니다.
다음 코드를 삽입하여 Loaded 이벤트를 처리합니다.
이 코드는 WindowsFormsHost 컨트롤의 인스턴스를 만들고 AxWindowsMediaPlayer 컨트롤의 인스턴스를 자식으로 추가합니다.
Private Sub WindowLoaded(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\Windows XP Startup.wav" End Sub
private void WindowLoaded(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\Windows XP Startup.wav"; }
F5 키를 눌러 응용 프로그램을 빌드 및 실행합니다.
참고 항목
작업
Windows Presentation Foundation에서 ActiveX 컨트롤 호스팅 샘플
개념
연습: Windows Presentation Foundation에서 Windows Forms 합성 컨트롤 호스팅
연습: Windows Forms에서 Windows Presentation Foundation 컨트롤 호스팅