■ WindowsFormsHost 클래스의 PropertyMap 속성을 사용하여 WPF 속성을 호스팅된 Windows Forms 컨트롤의 해당 속성에 매핑하는 방법을 보여준다.
▶ MainWindow.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="800" Height="600" Title="TestProject" FontFamily="나눔고딕코딩" FontSize="16"> <DockPanel Margin="10" LastChildFill="True"> <WindowsFormsHost Name="windowsFormsHost" DockPanel.Dock="Left" FontSize="20" /> </DockPanel> </Window> |
▶ MainWindow.xaml.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 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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 |
using System; using System.IO; using System.Reflection; using System.Windows; using System.Windows.Forms.Integration; using System.Windows.Media; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); Loaded += Window_Loaded; this.windowsFormsHost.SizeChanged += windowsFormsHost_SizeChanged; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 윈도우 로드시 처리하기 - Window_Loaded(sender, e) /// <summary> /// 윈도우 로드시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void Window_Loaded(object sender, RoutedEventArgs e) { System.Windows.Forms.Application.EnableVisualStyles(); System.Windows.Forms.CheckBox checkBox = new System.Windows.Forms.CheckBox(); checkBox.Dock = System.Windows.Forms.DockStyle.Fill; checkBox.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; checkBox.Text = "Windows Forms checkbox"; checkBox.CheckedChanged += checkBox_CheckedChanged; this.windowsFormsHost.Child = checkBox; ReplaceFlowDirectionPropertyMapping(); RemoveCursorPropertyMapping(); AddClipPropertyMapping(); ExtendBackgroundPropertyMapping(); this.windowsFormsHost.FlowDirection = FlowDirection.LeftToRight; this.windowsFormsHost.Clip = new RectangleGeometry(); this.windowsFormsHost.Background = new ImageBrush(); } #endregion #region 윈도우즈 폼 호스트 크기 변경시 처리하기 - windowsFormsHost_SizeChanged(sender, e) /// <summary> /// 윈도우즈 폼 호스트 크기 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void windowsFormsHost_SizeChanged(object sender, SizeChangedEventArgs e) { ProcessClipPropertyTranslator(this.windowsFormsHost, "Clip", null); } #endregion #region 체크 박스 체크 변경시 처리하기 - checkBox_CheckedChanged(sender, e) /// <summary> /// 체크 박스 체크 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void checkBox_CheckedChanged(object sender, EventArgs e) { System.Windows.Forms.CheckBox checkBox = sender as System.Windows.Forms.CheckBox; if(checkBox.CheckState == System.Windows.Forms.CheckState.Checked) { this.windowsFormsHost.FlowDirection = FlowDirection.RightToLeft; } else { this.windowsFormsHost.FlowDirection = FlowDirection.LeftToRight; } } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 클리핑 영역 생성하기 - CreateClippingRegion() /// <summary> /// 클리핑 영역 생성하기 /// </summary> /// <returns>클리핑 영역</returns> private System.Drawing.Region CreateClippingRegion() { System.Drawing.Drawing2D.GraphicsPath graphicsPath = new System.Drawing.Drawing2D.GraphicsPath(); graphicsPath.StartFigure(); graphicsPath.AddEllipse ( new System.Drawing.Rectangle ( 0, 0, (int)this.windowsFormsHost.ActualWidth, (int)this.windowsFormsHost.ActualHeight ) ); graphicsPath.CloseFigure(); return new System.Drawing.Region(graphicsPath); } #endregion #region Clip 속성 번역자 처리하기 - ProcessClipPropertyTranslator(hostObject, propertyName, value) /// <summary> /// Clip 속성 번역자 처리하기 /// </summary> /// <param name="hostObject">호스트 객체</param> /// <param name="propertyName">속성명</param> /// <param name="value">값</param> private void ProcessClipPropertyTranslator(object hostObject, string propertyName, object value) { WindowsFormsHost windowsFormsHost = hostObject as WindowsFormsHost; System.Windows.Forms.CheckBox checkBox = windowsFormsHost.Child as System.Windows.Forms.CheckBox; if(checkBox != null) { checkBox.Region = CreateClippingRegion(); } } #endregion #region FlowDirection 속성 번역자 처리하기 - ProcessFlowDirectionPropertyTranslator(hostObject, propertyName, value) /// <summary> /// FlowDirection 속성 번역자 처리하기 /// </summary> /// <param name="hostObject">호스트 객체</param> /// <param name="propertyName">속성명</param> /// <param name="value">값</param> private void ProcessFlowDirectionPropertyTranslator(object hostObject, string propertyName, object value) { WindowsFormsHost windowsFormsHost = hostObject as WindowsFormsHost; FlowDirection flowDirection = (FlowDirection)value; System.Windows.Forms.CheckBox checkBox = windowsFormsHost.Child as System.Windows.Forms.CheckBox; checkBox.RightToLeft = (flowDirection == FlowDirection.RightToLeft) ? System.Windows.Forms.RightToLeft.Yes : System.Windows.Forms.RightToLeft.No; } #endregion #region Background 속성 번역자 처리하기 - ProcessBackgroundPropertyTranslator(hostObject, propertyName, value) /// <summary> /// Background 속성 번역자 처리하기 /// </summary> /// <param name="hostObject">호스트 객체</param> /// <param name="propertyName">속성명</param> /// <param name="value">값</param> private void ProcessBackgroundPropertyTranslator(object hostObject, string propertyName, object value) { WindowsFormsHost windowsFormsHost = hostObject as WindowsFormsHost; System.Windows.Forms.CheckBox checkBox = windowsFormsHost.Child as System.Windows.Forms.CheckBox; ImageBrush imageBrush = value as ImageBrush; if(imageBrush != null) { string assemblyFilePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); string imageFilePath = Path.Combine(assemblyFilePath, "IMAGE", "sample.jpg"); checkBox.BackgroundImage = new System.Drawing.Bitmap(imageFilePath); } } #endregion #region Clip 속성 매핑 추가하기 - AddClipPropertyMapping() /// <summary> /// Clip 속성 매핑 추가하기 /// </summary> private void AddClipPropertyMapping() { this.windowsFormsHost.PropertyMap.Add("Clip", new PropertyTranslator(ProcessClipPropertyTranslator)); } #endregion #region 커서 속성 매핑 제거하기 - RemoveCursorPropertyMapping() /// <summary> /// 커서 속성 매핑 제거하기 /// </summary> private void RemoveCursorPropertyMapping() { this.windowsFormsHost.PropertyMap.Remove("Cursor"); } #endregion #region FlowDirection 속성 매핑 변경하기 - ReplaceFlowDirectionPropertyMapping() /// <summary> /// FlowDirection 속성 매핑 변경하기 /// </summary> private void ReplaceFlowDirectionPropertyMapping() { this.windowsFormsHost.PropertyMap.Remove("FlowDirection"); this.windowsFormsHost.PropertyMap.Add("FlowDirection", new PropertyTranslator(ProcessFlowDirectionPropertyTranslator)); } #endregion #region Background 속성 매핑 확장하기 - ExtendBackgroundPropertyMapping() /// <summary> /// Background 속성 매핑 확장하기 /// </summary> private void ExtendBackgroundPropertyMapping() { if(this.windowsFormsHost.PropertyMap["Background"] != null) { this.windowsFormsHost.PropertyMap["Background"] += new PropertyTranslator(ProcessBackgroundPropertyTranslator); } } #endregion } } |