연습: WPF에서 ActiveX 컨트롤 호스팅

브라우저와의 상호 작용을 개선하기 위해 WPF 기반 애플리케이션에서 Microsoft ActiveX 컨트롤을 사용할 수 있습니다. 이 연습에서는 Microsoft Windows Media Player를 WPF 페이지의 컨트롤로 호스트하는 방법을 보여 줍니다.

이 연습에서 설명하는 작업은 다음과 같습니다.

  • 프로젝트 만들기.

  • ActiveX 컨트롤 만들기.

  • WPF 페이지에서 ActiveX 컨트롤 호스팅.

이 연습을 완료하면 WPF 기반 애플리케이션에서 Microsoft ActiveX 컨트롤을 사용하는 방법을 이해할 수 있습니다.

필수 구성 요소

이 연습을 완료하려면 다음과 같은 구성 요소가 필요합니다.

  • Visual Studio가 설치된 컴퓨터에 설치된 Microsoft Windows Media Player.

  • Visual Studio 2010

프로젝트 만들기

프로젝트를 만들고 설정하려면

  1. HostingAxInWpf라는 WPF 애플리케이션 프로젝트를 만듭니다.

  2. 솔루션에 Windows Forms 컨트롤 라이브러리 프로젝트를 추가하고 프로젝트 이름을 WmpAxLib로 지정합니다.

  3. WmpAxLib 프로젝트에서 wmp.dll이라는 Windows Media Player 어셈블리에 대한 참조를 추가합니다.

  4. 도구 상자를 엽니다.

  5. 도구 상자를 마우스 오른쪽 단추로 클릭한 다음, 항목 선택을 클릭합니다.

  6. COM 구성 요소 탭을 클릭하고 Windows Media Player 컨트롤을 선택한 다음, 확인을 클릭합니다.

    Windows Media Player 컨트롤이 도구 상자에 추가됩니다.

  7. 솔루션 탐색기에서 UserControl1 파일을 마우스 오른쪽 단추로 클릭한 다음, 이름 바꾸기를 클릭합니다.

  8. 언어에 따라 이름을 WmpAxControl.vb 또는 WmpAxControl.cs로 변경합니다.

  9. 모든 참조의 이름을 바꿀 것인지 묻는 메시지가 표시되면 를 클릭합니다.

ActiveX 컨트롤 만들기

Visual Studio는 컨트롤이 디자인 화면에 추가될 때 Microsoft ActiveX 컨트롤에 대한 AxHost 래퍼 클래스를 자동으로 생성합니다. 다음 절차에서는 AxInterop.WMPLib.dll이라는 관리되는 어셈블리를 만듭니다.

ActiveX 컨트롤을 만들려면

  1. Windows Forms 디자이너에서 WmpAxControl.vb 또는 WmpAxControl.cs를 엽니다.

  2. 도구 상자에서 디자인 화면에 Windows Media Player 컨트롤을 추가합니다.

  3. 속성 창에서 Windows Media Player 컨트롤의 Dock 속성 값을 Fill로 설정합니다.

  4. WmpAxLib 컨트롤 라이브러리 프로젝트를 빌드합니다.

WPF 페이지에서 ActiveX 컨트롤 호스팅

ActiveX 컨트롤을 호스트하려면

  1. HostingAxInWpf 프로젝트에서 생성된 ActiveX 상호 운용성 어셈블리에 대한 참조를 추가합니다.

    이 어셈블리의 이름은 AxInterop.WMPLib.dll이며 Windows Media Player 컨트롤을 가져올 때 WmpAxLib 프로젝트의 Debug 폴더에 추가되었습니다.

  2. WindowsFormsIntegration.dll이라는 WindowsFormsIntegration 어셈블리에 대한 참조를 추가합니다.

  3. System.Windows.Forms.dll이라는 Windows Forms 어셈블리에 대한 참조를 추가합니다.

  4. WPF 디자이너에서 MainWindow.xaml을 엽니다.

  5. Grid 요소의 이름을 grid1로 지정합니다.

    <Grid Name="grid1">
        
    </Grid>
    
  6. 디자인 뷰 또는 XAML 뷰에서 Window 요소를 선택합니다.

  7. 속성 창에서 이벤트 탭을 클릭합니다.

  8. Loaded 이벤트를 두 번 클릭합니다.

  9. Loaded 이벤트를 처리하려면 다음 코드를 삽입합니다.

    이 코드는 WindowsFormsHost 컨트롤의 인스턴스를 만들고 AxWindowsMediaPlayer 컨트롤의 인스턴스를 자식으로 추가합니다.

    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.
        WmpAxLib.AxWindowsMediaPlayer axWmp = new WmpAxLib.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";
    }
    
    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
    
  10. F5를 눌러 애플리케이션을 빌드 및 실행합니다.

참고 항목