■ ElementHost 컨트롤의 PropertyMap 속성을 사용하여 Windows Forms 속성을 호스팅된 WPF 엘리먼트의 해당 속성에 매핑하는 방법을 보여준다.
▶ 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 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 |
using System; using System.Drawing; using System.IO; using System.Reflection; using System.Windows; using System.Windows.Forms; using System.Windows.Forms.Integration; using System.Windows.Media; using System.Windows.Media.Imaging; namespace TestProject { /// <summary> /// 메인 폼 /// </summary> public partial class MainForm : Form { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainForm() /// <summary> /// 생성자 /// </summary> public MainForm() { InitializeComponent(); Load += Form_Load; Resize += Form_Resize; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 폼 로드시 처리하기 - Form_Load(sender, e) /// <summary> /// 폼 로드시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void Form_Load(object sender, EventArgs e) { System.Windows.Controls.Button button = new System.Windows.Controls.Button(); button.Content = "Windows Presentation Foundation Button"; this.elementHost.Child = button; AddMarginPropertyMapping(); RemoveCursorPropertyMapping(); AddRegionPropertyMapping(); ExtendBackColorPropertyMapping(); this.elementHost.Margin = new Padding(30, 30, 30, 30); this.elementHost.Region = new Region(); this.elementHost.BackColor = System.Drawing.Color.AliceBlue; } #endregion #region 폼 크기 변경시 처리하기 - Form_Resize(sender, e) /// <summary> /// 폼 크기 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void Form_Resize(object sender, EventArgs e) { ProcessRegionPropertyTranslator(this.elementHost, "Region", null); } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 마진 속성 번역자 처리하기 - ProcessMarginPropertyTranslator(hostObject, propertyName, value) /// <summary> /// 마진 속성 번역자 처리하기 /// </summary> /// <param name="hostObject">호스트 객체</param> /// <param name="propertyName">속성명</param> /// <param name="value">값</param> private void ProcessMarginPropertyTranslator(object hostObject, string propertyName, object value) { ElementHost elementHost = hostObject as ElementHost; Padding padding = (Padding)value; System.Windows.Controls.Button button = elementHost.Child as System.Windows.Controls.Button; Thickness thickness = new Thickness(padding.Left, padding.Top, padding.Right, padding.Bottom); button.Margin = thickness; } #endregion #region 영역 속성 번역자 처리하기 - ProcessRegionPropertyTranslator(hostObject, propertyName, value) /// <summary> /// 영역 속성 번역자 처리하기 /// </summary> /// <param name="hostObject">호스트 객체</param> /// <param name="propertyName">속성명</param> /// <param name="value">값</param> private void ProcessRegionPropertyTranslator(object hostObject, string propertyName, object value) { ElementHost elementHost = hostObject as ElementHost; System.Windows.Controls.Button button = elementHost.Child as System.Windows.Controls.Button; button.Clip = new EllipseGeometry ( new Rect ( 0, 0, this.elementHost.Width - (button.Margin.Left + button.Margin.Right ), this.elementHost.Height - (button.Margin.Top + button.Margin.Bottom) ) ); } #endregion #region 배경색 속성 번역자 처리하기 - ProcessBackColorPropertyTranslator(hostObject, propertyName, value) /// <summary> /// 배경색 속성 번역자 처리하기 /// </summary> /// <param name="hostObject">호스트 객체</param> /// <param name="propertyName">속성명</param> /// <param name="value">값</param> private void ProcessBackColorPropertyTranslator(object hostObject, string propertyName, object value) { string assemblyFilePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); string imageFilePath = Path.Combine(assemblyFilePath, "IMAGE", "sample.jpg"); ImageBrush imageBrush = new ImageBrush(new BitmapImage(new Uri(@"file:///" + imageFilePath))); ElementHost elementHost = hostObject as ElementHost; System.Windows.Controls.Button button = elementHost.Child as System.Windows.Controls.Button; button.Background = imageBrush; } #endregion #region 마진 속성 매핑 추가하기 - AddMarginPropertyMapping() /// <summary> /// 마진 속성 매핑 추가하기 /// </summary> private void AddMarginPropertyMapping() { this.elementHost.PropertyMap.Add("Margin", new PropertyTranslator(ProcessMarginPropertyTranslator)); } #endregion #region 커서 속성 매핑 제거하기 - RemoveCursorPropertyMapping() /// <summary> /// 커서 속성 매핑 제거하기 /// </summary> private void RemoveCursorPropertyMapping() { this.elementHost.PropertyMap.Remove("Cursor"); } #endregion #region 영역 속성 매핑 추가하기 - AddRegionPropertyMapping() /// <summary> /// 영역 속성 매핑 추가하기 /// </summary> private void AddRegionPropertyMapping() { this.elementHost.PropertyMap.Add("Region", new PropertyTranslator(ProcessRegionPropertyTranslator)); } #endregion #region 배경색 속성 매핑 확장하기 - ExtendBackColorPropertyMapping() /// <summary> /// 배경색 속성 매핑 확장하기 /// </summary> private void ExtendBackColorPropertyMapping() { if(this.elementHost.PropertyMap["BackColor"] != null) { this.elementHost.PropertyMap["BackColor"] += new PropertyTranslator(ProcessBackColorPropertyTranslator); } } #endregion } } |