■ WPF 복합 컨트롤을 만들고 ElementHost 컨트롤을 사용하여 Windows Forms 컨트롤 및 폼에서 호스트하는 방법을 보여준다. 두 개의 자식 컨트롤이 포함된 WPF UserControl을 구현한다. UserControl은 3차원 원뿔을 표시한다. 3D 개체를 렌더링하는 것은 Windows Forms보다 WPF를 사용하는 것이 훨씬 쉽다. 따라서 WPF UserControl 클래스를 호스트하여 Windows Forms에서 3D 그래픽을 만드는 것이 좋다.
[TestLibrary 프로젝트]
▶ CustomControl.xaml
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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
<UserControl x:Class="TestLibrary.CustomControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <Grid> <Label HorizontalAlignment="Center" Foreground="Red" TextBlock.TextAlignment="Center" FontSize="20" Content="Model : Cone" /> <Viewport3D> <Viewport3D.Camera> <PerspectiveCamera Position="0 0 -3" LookDirection="0 0 1" UpDirection="0 1 0" NearPlaneDistance="1" FarPlaneDistance="20" FieldOfView="45" /> </Viewport3D.Camera> <Viewport3D.Children> <ModelVisual3D> <ModelVisual3D.Content> <Model3DGroup > <Model3DGroup.Children> <DirectionalLight Direction="3 -4 5" Color="#ffffffff" /> <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> |
[TestProject 프로젝트]
▶ MainForm.cs
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 |
using System; using System.Windows.Forms; using System.Windows.Forms.Integration; using TestLibrary; namespace TestProject { /// <summary> /// 메인 폼 /// </summary> public partial class MainForm : Form { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainForm() /// <summary> /// 생성자 /// </summary> public MainForm() { InitializeComponent(); Load += Form_Load; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 폼 로드시 처리하기 - Form_Load(sender, e) /// <summary> /// 폼 로드시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void Form_Load(object sender, EventArgs e) { CustomControl customControl = new CustomControl(); ElementHost elementHost = new ElementHost(); elementHost.Dock = DockStyle.Fill; elementHost.Child = customControl; Controls.Add(elementHost); } #endregion } } |