Instruções passo a passo: hospedando um controle composto do WPF 3D nos Windows FormsWalkthrough: Hosting a 3-D WPF Composite Control in Windows Forms

Este passo a passo demonstra como você pode criar um WPFWPF composto controlar e hospedá-lo em Windows FormsWindows Forms controles e formulários usando o ElementHost controle.This walkthrough demonstrates how you can create a WPFWPF composite control and host it in Windows FormsWindows Forms controls and forms by using the ElementHost control.

Neste passo a passo, você irá implementar um WPFWPF UserControl que contém dois controles filho.In this walkthrough, you will implement a WPFWPF UserControl that contains two child controls. O UserControl exibe um cone tridimensional (3D).The UserControl displays a three-dimensional (3-D) cone. É muito mais fácil renderizar objetos 3D com o WPFWPF que com Windows FormsWindows Forms.Rendering 3-D objects is much easier with the WPFWPF than with Windows FormsWindows Forms. Portanto, faz sentido para hospedar um WPFWPF UserControl classe para criar gráficos 3D em Windows FormsWindows Forms.Therefore, it makes sense to host a WPFWPF UserControl class to create 3-D graphics in Windows FormsWindows Forms.

As tarefas ilustradas neste passo a passo incluem:Tasks illustrated in this walkthrough include:

Para ver uma listagem de código completa das tarefas ilustradas nesta instrução passo a passo, consulte Exemplo de hospedagem de um controle composto 3D do WPF no Windows Forms.For a complete code listing of the tasks illustrated in this walkthrough, see Hosting a 3-D WPF Composite Control in Windows Forms Sample.

Pré-requisitosPrerequisites

Você precisa dos seguintes componentes para concluir esta instrução passo a passo:You need the following components to complete this walkthrough:

  • Visual Studio 2008Visual Studio 2008..

Criando o UserControlCreating the UserControl

Criar o UserControlTo create the UserControl

  1. Criar um projeto de biblioteca de controle de usuário de WPF chamada HostingWpfUserControlInWf.Create a WPF User Control Library project named HostingWpfUserControlInWf.

  2. Abra UserControl1.xaml no WPF DesignerWPF Designer.Open UserControl1.xaml in the WPF DesignerWPF Designer.

  3. Substitua o código gerado pelo código a seguir.Replace the generated code with the following code.

    Esse código define uma System.Windows.Controls.UserControl que contém dois controles filho.This code defines a System.Windows.Controls.UserControl that contains two child controls. O primeiro controle filho é um System.Windows.Controls.Label controle; o segundo é um Viewport3D controle que exibe um cone 3D.The first child control is a System.Windows.Controls.Label control; the second is a Viewport3D control that displays a 3-D cone.

    <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>
    

Criando o projeto de host do Windows FormsCreating the Windows Forms Host Project

Criar o projeto de hostTo create the host project

  1. Adicione um projeto de Aplicativos do Windows chamado WpfUserControlHost à solução.Add a Windows application project named WpfUserControlHost to the solution. Para obter mais informações, consulte Como criar um novo projeto de aplicativo do WPF.For more information, see How to: Create a New WPF Application Project.

  2. No Gerenciador de Soluções, adicione uma referência ao assembly WindowsFormsIntegration, que é chamado de WindowsFormsIntegration.dll.In Solution Explorer, add a reference to the WindowsFormsIntegration assembly, which is named WindowsFormsIntegration.dll.

  3. Adicione referências aos assemblies WPFWPF a seguir:Add references to the following WPFWPF assemblies:

    • PresentationCorePresentationCore

    • PresentationFrameworkPresentationFramework

    • WindowsBaseWindowsBase

  4. Adicione uma referência ao projeto HostingWpfUserControlInWf.Add a reference to the HostingWpfUserControlInWf project.

  5. No Gerenciador de Soluções, defina o projeto WpfUserControlHost como o projeto de inicialização.In Solution Explorer, set the WpfUserControlHost project to be the startup project.

Hospedando o UserControl do Windows Presentation FoundationHosting the Windows Presentation Foundation UserControl

Hospedar o UserControlTo host the UserControl

  1. No Designer de Formulários do Windows, abra Form1.In the Windows Forms Designer, open Form1.

  2. Na janela Propriedades, clique em eventose, em seguida, clique duas vezes o Load evento para criar um manipulador de eventos.In the Properties window, click Events, and then double-click the Load event to create an event handler.

    O Editor de Código abre o manipulador de eventos Form1_Load recém gerado.The Code Editor opens to the newly generated Form1_Load event handler.

  3. Substitua o código em Form1.cs pelo código a seguir.Replace the code in Form1.cs with the following code.

    O Form1_Load manipulador de eventos cria uma instância de UserControl1 e adiciona a forma de ElementHost coleção de controles filho do controle.The Form1_Load event handler creates an instance of UserControl1 and adds itto the ElementHost control's collection of child controls. O ElementHost controle é adicionado à coleção do formulário de controles filho.The ElementHost control is added to the form's collection of child controls.

    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
    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. Pressione F5 para compilar e executar o aplicativo.Press F5 to build and run the application.

Consulte tambémSee Also

ElementHost
WindowsFormsHost
Designer do WPFWPF Designer
Instruções passo a passo: hospedando um controle de composição do WPF nos Windows FormsWalkthrough: Hosting a WPF Composite Control in Windows Forms
Passo a passo: hospedando um controle composto do Windows Forms no WPFWalkthrough: Hosting a Windows Forms Composite Control in WPF
Hospedando um controle composto do WPF nos exemplo do Windows FormsHosting a WPF Composite Control in Windows Forms Sample