다음을 통해 공유


연습: Windows Forms에서 3D WPF 복합 컨트롤 호스팅

이 연습에서는 WPF 복합 컨트롤을 만들고 ElementHost 컨트롤을 사용하여 Windows Forms 컨트롤 및 양식에서 호스트하는 방법을 보여 줍니다.

이 연습에서는 두 개의 하위 컨트롤이 포함된 WPF UserControl을 구현합니다. UserControl은 3D(3차원) 원뿔을 표시합니다. WPF에서는 3D 개체를 렌더링하는 것이 Windows Forms를 사용하는 것보다 훨씬 쉽습니다. 따라서 WPF UserControl 클래스를 호스트하여 Windows Forms에서 3D 그래픽을 만드는 것이 좋습니다.

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

필수 구성 요소

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

  • Visual Studio 2017

UserControl 만들기

  1. HostingWpfUserControlInWf라는 WPF 사용자 제어 라이브러리 프로젝트를 만듭니다.

  2. UserControl1.xaml을 WPF 디자이너에서 엽니다.

  3. 생성된 코드를 다음 코드로 바꿉니다.

    <UserControl x:Class="HostingWpfUserControlInWf.UserControl1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        >
      
        <Grid>
    
          <!-- Place a Label control at the top of the view. -->
          <Label 
                    HorizontalAlignment="Center" 
                    TextBlock.TextAlignment="Center" 
                    FontSize="20" 
                    Foreground="Red" 
                    Content="Model: Cone"/>
    
          <!-- Viewport3D is the rendering surface. -->
          <Viewport3D Name="myViewport" >
    
            <!-- Add a camera. -->
            <Viewport3D.Camera>
              <PerspectiveCamera 
                            FarPlaneDistance="20" 
                            LookDirection="0,0,1" 
                            UpDirection="0,1,0" 
                            NearPlaneDistance="1" 
                            Position="0,0,-3" 
                            FieldOfView="45" />
            </Viewport3D.Camera>
    
            <!-- Add models. -->
            <Viewport3D.Children>
    
              <ModelVisual3D>
                <ModelVisual3D.Content>
    
                  <Model3DGroup >
                    <Model3DGroup.Children>
    
                      <!-- Lights, MeshGeometry3D and DiffuseMaterial objects are added to the ModelVisual3D. -->
                      <DirectionalLight Color="#FFFFFFFF" Direction="3,-4,5" />
    
                      <!-- Define a red cone. -->
                      <GeometryModel3D>
    
                        <GeometryModel3D.Geometry>
                          <MeshGeometry3D 
        Positions="0.293893 -0.5 0.404509  0.475528 -0.5 0.154509  0 0.5 0  0.475528 -0.5 0.154509  0 0.5 0  0 0.5 0  0.475528 -0.5 0.154509  0.475528 -0.5 -0.154509  0 0.5 0  0.475528 -0.5 -0.154509  0 0.5 0  0 0.5 0  0.475528 -0.5 -0.154509  0.293893 -0.5 -0.404509  0 0.5 0  0.293893 -0.5 -0.404509  0 0.5 0  0 0.5 0  0.293893 -0.5 -0.404509  0 -0.5 -0.5  0 0.5 0  0 -0.5 -0.5  0 0.5 0  0 0.5 0  0 -0.5 -0.5  -0.293893 -0.5 -0.404509  0 0.5 0  -0.293893 -0.5 -0.404509  0 0.5 0  0 0.5 0  -0.293893 -0.5 -0.404509  -0.475528 -0.5 -0.154509  0 0.5 0  -0.475528 -0.5 -0.154509  0 0.5 0  0 0.5 0  -0.475528 -0.5 -0.154509  -0.475528 -0.5 0.154509  0 0.5 0  -0.475528 -0.5 0.154509  0 0.5 0  0 0.5 0  -0.475528 -0.5 0.154509  -0.293892 -0.5 0.404509  0 0.5 0  -0.293892 -0.5 0.404509  0 0.5 0  0 0.5 0  -0.293892 -0.5 0.404509  0 -0.5 0.5  0 0.5 0  0 -0.5 0.5  0 0.5 0  0 0.5 0  0 -0.5 0.5  0.293893 -0.5 0.404509  0 0.5 0  0.293893 -0.5 0.404509  0 0.5 0  0 0.5 0  " 
        Normals="0.7236065,0.4472139,0.5257313  0.2763934,0.4472138,0.8506507  0.5308242,0.4294462,0.7306172  0.2763934,0.4472138,0.8506507  0,0.4294458,0.9030925  0.5308242,0.4294462,0.7306172  0.2763934,0.4472138,0.8506507  -0.2763934,0.4472138,0.8506507  0,0.4294458,0.9030925  -0.2763934,0.4472138,0.8506507  -0.5308242,0.4294462,0.7306172  0,0.4294458,0.9030925  -0.2763934,0.4472138,0.8506507  -0.7236065,0.4472139,0.5257313  -0.5308242,0.4294462,0.7306172  -0.7236065,0.4472139,0.5257313  -0.858892,0.429446,0.279071  -0.5308242,0.4294462,0.7306172  -0.7236065,0.4472139,0.5257313  -0.8944269,0.4472139,0  -0.858892,0.429446,0.279071  -0.8944269,0.4472139,0  -0.858892,0.429446,-0.279071  -0.858892,0.429446,0.279071  -0.8944269,0.4472139,0  -0.7236065,0.4472139,-0.5257313  -0.858892,0.429446,-0.279071  -0.7236065,0.4472139,-0.5257313  -0.5308242,0.4294462,-0.7306172  -0.858892,0.429446,-0.279071  -0.7236065,0.4472139,-0.5257313  -0.2763934,0.4472138,-0.8506507  -0.5308242,0.4294462,-0.7306172  -0.2763934,0.4472138,-0.8506507  0,0.4294458,-0.9030925  -0.5308242,0.4294462,-0.7306172  -0.2763934,0.4472138,-0.8506507  0.2763934,0.4472138,-0.8506507  0,0.4294458,-0.9030925  0.2763934,0.4472138,-0.8506507  0.5308249,0.4294459,-0.7306169  0,0.4294458,-0.9030925  0.2763934,0.4472138,-0.8506507  0.7236068,0.4472141,-0.5257306  0.5308249,0.4294459,-0.7306169  0.7236068,0.4472141,-0.5257306  0.8588922,0.4294461,-0.27907  0.5308249,0.4294459,-0.7306169  0.7236068,0.4472141,-0.5257306  0.8944269,0.4472139,0  0.8588922,0.4294461,-0.27907  0.8944269,0.4472139,0  0.858892,0.429446,0.279071  0.8588922,0.4294461,-0.27907  0.8944269,0.4472139,0  0.7236065,0.4472139,0.5257313  0.858892,0.429446,0.279071  0.7236065,0.4472139,0.5257313  0.5308242,0.4294462,0.7306172  0.858892,0.429446,0.279071  "                   TriangleIndices="0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 " />
                        </GeometryModel3D.Geometry>
    
                        <GeometryModel3D.Material>
                          <DiffuseMaterial>
                            <DiffuseMaterial.Brush>
                              <SolidColorBrush 
                                Color="Red" 
                                Opacity="1.0"/>
                            </DiffuseMaterial.Brush>
                          </DiffuseMaterial>
                        </GeometryModel3D.Material>
    
                      </GeometryModel3D>
    
                    </Model3DGroup.Children>
                  </Model3DGroup>
    
                </ModelVisual3D.Content>
    
              </ModelVisual3D>
    
            </Viewport3D.Children>
    
          </Viewport3D>
        </Grid>
      
    </UserControl>
    

    이 코드는 두 개의 자식 컨트롤을 포함하는 System.Windows.Controls.UserControl을 정의합니다. 첫 번째 자식 컨트롤은 System.Windows.Controls.Label 컨트롤이고, 두 번째 컨트롤은 3D 원뿔을 표시하는 Viewport3D 컨트롤입니다.

호스트 프로젝트 만들기

  1. 솔루션에 WpfUserControlHost라는 Windows Forms 앱(.NET Framework) 프로젝트를 추가합니다.

  2. 솔루션 탐색기에서 WindowsFormsIntegration.dll이라고 명명된 WindowsFormsIntegration 어셈블리에 대한 참조를 추가합니다.

  3. 다음 WPF 어셈블리에 참조를 추가합니다.

    • PresentationCore

    • PresentationFramework

    • WindowsBase

  4. HostingWpfUserControlInWf 프로젝트에 참조를 추가합니다.

  5. 솔루션 탐색기에서 WpfUserControlHost 프로젝트를 시작 프로젝트로 설정합니다.

UserControl 호스트

  1. Windows Forms 디자이너에서 Form1을 엽니다.

  2. 속성 창에서 이벤트를 클릭한 다음, Load 이벤트를 두 번 클릭하여 이벤트 처리기를 만듭니다.

    코드 편집기에서 새로 생성된 Form1_Load 이벤트 처리기가 열립니다.

  3. Form1.cs의 코드를 다음 코드로 바꿉니다.

    Form1_Load 이벤트 처리기는 UserControl1의 인스턴스를 만들어서 ElementHost 컨트롤의 자식 컨트롤 컬렉션에 추가합니다. ElementHost 컨트롤이 양식의 자식 컨트롤 컬렉션에 추가됩니다.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    
    using System.Windows.Forms.Integration;
    
    namespace WpfUserControlHost
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                // Create the ElementHost control for hosting the
                // WPF UserControl.
                ElementHost host = new ElementHost();
                host.Dock = DockStyle.Fill;
    
                // Create the WPF UserControl.
                HostingWpfUserControlInWf.UserControl1 uc =
                    new HostingWpfUserControlInWf.UserControl1();
    
                // Assign the WPF UserControl to the ElementHost control's
                // Child property.
                host.Child = uc;
    
                // Add the ElementHost control to the form's
                // collection of child controls.
                this.Controls.Add(host);
            }
        }
    }
    
    Imports System.Collections.Generic
    Imports System.ComponentModel
    Imports System.Data
    Imports System.Drawing
    Imports System.Text
    Imports System.Windows.Forms
    
    Imports System.Windows.Forms.Integration
    
    Public Class Form1
        Inherits Form
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            ' Create the ElementHost control for hosting the
            ' WPF UserControl.
            Dim host As New ElementHost()
            host.Dock = DockStyle.Fill
    
            ' Create the WPF UserControl.
            Dim uc As New HostingWpfUserControlInWf.UserControl1()
    
            ' Assign the WPF UserControl to the ElementHost control's
            ' Child property.
            host.Child = uc
    
            ' Add the ElementHost control to the form's
            ' collection of child controls.
            Me.Controls.Add(host)
        End Sub
    
    End Class
    
  4. F5 키를 눌러 애플리케이션을 빌드하고 실행합니다.

참고 항목