■ WPF는 응용 프로그램을 만들기 위한 풍부한 환경을 제공한다. 그러나 Win32 코드에 상당한 투자가 있는 경우 WPF 응용 프로그램에서 해당 코드 중 일부를 완전히 다시 작성하는 것보다 재사용하는 것이 더 효과적일 수 있다. WPF는 WPF 페이지에서 Win32 창을 호스팅하기 위한 간단한 메커니즘을 제공한다.
▶ ControlHost.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 |
using System; using System.Runtime.InteropServices; using System.Windows.Interop; namespace TestProject { /// <summary> /// 컨트롤 호스트 /// </summary> public class ControlHost : HwndHost { //////////////////////////////////////////////////////////////////////////////////////////////////// Import ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 윈도우 생성하기 (확장) - CreateWindowEx(extendedStyle, className, windowName, style, x, y, width, height, parentWindowHandle, menuHandle, instanceHandle, parameter) /// <summary> /// 윈도우 생성하기 (확장) /// </summary> /// <param name="extendedStyle">확장 스타일</param> /// <param name="className">클래스명</param> /// <param name="windowName">윈도우명</param> /// <param name="style">스타일</param> /// <param name="x">X</param> /// <param name="y">Y</param> /// <param name="width">너비</param> /// <param name="height">높이</param> /// <param name="parentWindowHandle">부모 윈도우 핸들</param> /// <param name="menuHandle">메뉴 핸들</param> /// <param name="instanceHandle">인스턴스 핸들</param> /// <param name="parameter">매개 변수</param> /// <returns>윈도우 핸들</returns> [DllImport("user32", EntryPoint = "CreateWindowEx", CharSet = CharSet.Unicode)] private static extern IntPtr CreateWindowEx ( int extendedStyle, string className, string windowName, int style, int x, int y, int width, int height, IntPtr parentWindowHandle, IntPtr menuHandle, IntPtr instanceHandle, [MarshalAs(UnmanagedType.AsAny)] object parameter ); #endregion #region 윈도우 제거하기 - DestroyWindow(windowHandle) /// <summary> /// 윈도우 제거하기 /// </summary> /// <param name="windowHandle">윈도우 핸들</param> /// <returns>처리 결과</returns> [DllImport("user32", EntryPoint = "DestroyWindow", CharSet = CharSet.Unicode)] private static extern bool DestroyWindow(IntPtr windowHandle); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// WS_CHILD /// </summary> private const int WS_CHILD = 0x40000000; /// <summary> /// WS_VISIBLE /// </summary> private const int WS_VISIBLE = 0x10000000; /// <summary> /// LBS_NOTIFY /// </summary> private const int LBS_NOTIFY = 0x00000001; /// <summary> /// HOST_ID /// </summary> private const int HOST_ID = 0x00000002; /// <summary> /// LISTBOX_ID /// </summary> private const int LISTBOX_ID = 0x00000001; /// <summary> /// WS_VSCROLL /// </summary> private const int WS_VSCROLL = 0x00200000; /// <summary> /// WS_BORDER /// </summary> private const int WS_BORDER = 0x00800000; /// <summary> /// 호스트 너비 /// </summary> private readonly int hostWidth; /// <summary> /// 호스트 높이 /// </summary> private readonly int hostHeight; /// <summary> /// 호스트 윈도우 핸들 /// </summary> private IntPtr hostWindowHandle; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 리스트 박스 윈도우 핸들 - ListBoxWindowHandle /// <summary> /// 리스트 박스 윈도우 핸들 /// </summary> public IntPtr ListBoxWindowHandle { get; private set; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - ControlHost(width, height) /// <summary> /// 생성자 /// </summary> /// <param name="width">너비</param> /// <param name="height">높이</param> public ControlHost(double width, double height) { this.hostWidth = (int)width; this.hostHeight = (int)height; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Protected #region 윈도우 코어 만들기 - BuildWindowCore(parentHandleRef) /// <summary> /// 윈도우 코어 만들기 /// </summary> /// <param name="parentHandleRef">부모 핸들 참조</param> /// <returns>핸들 참조</returns> protected override HandleRef BuildWindowCore(HandleRef parentHandleRef) { ListBoxWindowHandle = IntPtr.Zero; this.hostWindowHandle = IntPtr.Zero; this.hostWindowHandle = CreateWindowEx ( 0, "static", "", WS_CHILD | WS_VISIBLE, 0, 0, this.hostWidth, this.hostHeight, parentHandleRef.Handle, (IntPtr)HOST_ID, IntPtr.Zero, 0 ); ListBoxWindowHandle = CreateWindowEx ( 0, "listbox", "", WS_CHILD | WS_VISIBLE | LBS_NOTIFY | WS_VSCROLL | WS_BORDER, 0, 0, this.hostWidth, this.hostHeight, this.hostWindowHandle, (IntPtr)LISTBOX_ID, IntPtr.Zero, 0 ); return new HandleRef(this, this.hostWindowHandle); } #endregion #region 윈도우 프로시저 처리하기 - WndProc(windowHandle, message, wordParameter, longParameter, handled) /// <summary> /// 윈도우 프로시저 처리하기 /// </summary> /// <param name="windowHandle">윈도우 핸들</param> /// <param name="message">메시지</param> /// <param name="wordParameter">WORD 매개 변수</param> /// <param name="longParameter">LONG 매개 변수</param> /// <param name="handled">처리 여부</param> /// <returns>핸들</returns> protected override IntPtr WndProc(IntPtr windowHandle, int message, IntPtr wordParameter, IntPtr longParameter, ref bool handled) { handled = false; return IntPtr.Zero; } #endregion #region 윈도우 코어 제거하기 - DestroyWindowCore(windowHandleRef) /// <summary> /// 윈도우 코어 제거하기 /// </summary> /// <param name="windowHandleRef">윈도우 핸들 참조</param> protected override void DestroyWindowCore(HandleRef windowHandleRef) { DestroyWindow(windowHandleRef.Handle); } #endregion } } |
▶ MainWindow.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 |
<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" Background="LightGreen"> <Border Name="hostBorder" DockPanel.Dock="Right" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="10" Width="200" Height="250" BorderThickness="3" BorderBrush="LightGray" /> <StackPanel> <Label HorizontalAlignment="Center" Margin="0 10 0 0" FontWeight="Bold"> Control the Control </Label> <TextBlock Margin="10 10 10 10"> Selected Text : <TextBlock Name="selectedTextBlock" /> </TextBlock> <TextBlock Margin="10 10 10 10"> Number of Items : <TextBlock Name="itemCountTextBlock" /> </TextBlock> <Line HorizontalAlignment="Center" Margin="0 20 0 0" X1="0" X2="500" StrokeThickness="2" Stroke="LightYellow" /> <Label HorizontalAlignment="Center" Margin="10 10 10 10"> Append an Item to the List </Label> <StackPanel Orientation="Horizontal"> <Label HorizontalAlignment="Left" Margin="10 10 10 10"> Item Text </Label> <TextBox Name="appendTextBox" HorizontalAlignment="Left" Margin="10 10 10 10" Width="200" VerticalContentAlignment="Center" /> </StackPanel> <Button Name="appendButton" HorizontalAlignment="Left" Margin="10 10 10 10" Width="100" Height="30"> Append </Button> <Line HorizontalAlignment="Center" Margin="0 20 0 0" X1="0" X2="500" StrokeThickness="2" Stroke="LightYellow" /> <Label HorizontalAlignment="Center" Margin="10 10 10 10"> Delete the Selected Item </Label> <Button Name="deleteButton" HorizontalAlignment="Left" Margin="10 10 10 10" Width="100" Height="30"> Delete </Button> </StackPanel> </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 263 264 265 266 267 |
using System; using System.Runtime.InteropServices; using System.Text; using System.Windows; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Import ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 메시지 보내기 - SendMessage(windowHandle, message, wordParameter, longParameter) /// <summary> /// 메시지 보내기 /// </summary> /// <param name="windowHandle">윈도우 핸들</param> /// <param name="message">메시지</param> /// <param name="wordParameter">WORD 매개 변수</param> /// <param name="longParameter">LONG 매개 변수</param> /// <returns>처리 결과</returns> [DllImport("user32", EntryPoint = "SendMessage", CharSet = CharSet.Unicode)] private static extern int SendMessage(IntPtr windowHandle, int message, IntPtr wordParameter, IntPtr longParameter); #endregion #region 메시지 보내기 - SendMessage(windowHandle, message, wordParameter, longParameter) /// <summary> /// 메시지 보내기 /// </summary> /// <param name="windowHandle">윈도우 핸들</param> /// <param name="message">메시지</param> /// <param name="wordParameter">WORD 매개 변수</param> /// <param name="longParameter">LONG 매개 변수</param> /// <returns>처리 결과</returns> [DllImport("user32", EntryPoint = "SendMessage", CharSet = CharSet.Unicode)] private static extern int SendMessage(IntPtr windowHandle, int message, int wordParameter, [MarshalAs(UnmanagedType.LPWStr)] StringBuilder longParameter); #endregion #region 메시지 보내기 - SendMessage(windowHandle, message, wordParameter, longParameter) /// <summary> /// 메시지 보내기 /// </summary> /// <param name="windowHandle">윈도우 핸들</param> /// <param name="message">메시지</param> /// <param name="wordParameter">WORD 매개 변수</param> /// <param name="longParameter">LONG 매개 변수</param> /// <returns>처리 결과</returns> [DllImport("user32", EntryPoint = "SendMessage", CharSet = CharSet.Unicode)] private static extern IntPtr SendMessage(IntPtr windowHandle, int message, IntPtr wordParameter, string longParameter); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// WM_COMMAND /// </summary> private const int WM_COMMAND = 0x00000111; /// <summary> /// LB_SEL_CHANGE /// </summary> private const int LB_SEL_CHANGE = 0x00000001; /// <summary> /// LB_GET_CUR_SEL /// </summary> private const int LB_GET_CUR_SEL = 0x00000188; /// <summary> /// LB_GET_TEXT_LEN /// </summary> private const int LB_GET_TEXT_LEN = 0x0000018A; /// <summary> /// LB_ADD_STRING /// </summary> private const int LB_ADD_STRING = 0x00000180; /// <summary> /// LB_GET_TEXT /// </summary> private const int LB_GET_TEXT = 0x00000189; /// <summary> /// LB_DELETE_STRING /// </summary> private const int LB_DELETE_STRING = 0x00000182; /// <summary> /// LB_GET_COUNT /// </summary> private const int LB_GET_COUNT = 0x0000018B; /// <summary> /// 리스트 박스 핸들 /// </summary> private IntPtr listBoxHandle; /// <summary> /// 항목 수 /// </summary> private int itemCount; /// <summary> /// 컨트롤 호스트 /// </summary> private ControlHost controlHost; /// <summary> /// 선택 항목 인덱스 /// </summary> private int selectedItemIndex; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); Loaded += Window_Loaded; this.appendButton.Click += appendButton_Click; this.deleteButton.Click += deleteButton_Click; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 윈도우 로드시 처리하기 - Window_Loaded(sender, e) /// <summary> /// 윈도우 로드시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void Window_Loaded(object sender, EventArgs e) { this.controlHost = new ControlHost(this.hostBorder.ActualWidth, this.hostBorder.ActualHeight); this.hostBorder.Child = this.controlHost; this.controlHost.MessageHook += controlHost_MessageHook; this.listBoxHandle = this.controlHost.ListBoxWindowHandle; for(int i = 0; i < 15; i++) { string itemText = "Item" + i; SendMessage(this.listBoxHandle, LB_ADD_STRING, IntPtr.Zero, itemText); } this.itemCount = SendMessage(this.listBoxHandle, LB_GET_COUNT, IntPtr.Zero, IntPtr.Zero); this.itemCountTextBlock.Text = this.itemCount.ToString(); } #endregion #region 컨트롤 호스트 메시지 후킹 처리하기 - controlHost_MessageHook(windowHandle, message, wordParameter, longParameter, handled) /// <summary> /// 컨트롤 호스트 메시지 후킹 처리하기 /// </summary> /// <param name="windowHandle">윈도우 핸들</param> /// <param name="message">메시지</param> /// <param name="wordParameter">WORD 매개 변수</param> /// <param name="longParameter">LONG 매개 변수</param> /// <param name="handled">처리 여부</param> /// <returns>핸들</returns> private IntPtr controlHost_MessageHook(IntPtr windowHandle, int message, IntPtr wordParameter, IntPtr longParameter, ref bool handled) { int textLength; handled = false; if(message == WM_COMMAND) { switch((uint) wordParameter.ToInt32() >> 16 & 0xffff) { case LB_SEL_CHANGE : this.selectedItemIndex = SendMessage(this.controlHost.ListBoxWindowHandle, LB_GET_CUR_SEL, IntPtr.Zero, IntPtr.Zero); textLength = SendMessage(this.controlHost.ListBoxWindowHandle, LB_GET_TEXT_LEN, IntPtr.Zero, IntPtr.Zero); StringBuilder stringBuilder = new StringBuilder(); SendMessage(this.listBoxHandle, LB_GET_TEXT, this.selectedItemIndex, stringBuilder); this.selectedTextBlock.Text = stringBuilder.ToString(); handled = true; break; } } return IntPtr.Zero; } #endregion #region Append 버튼 클릭시 처리하기 - appendButton_Click(sender, e) /// <summary> /// Append 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void appendButton_Click(object sender, EventArgs e) { if(this.appendTextBox.Text != string.Empty) { SendMessage(this.listBoxHandle, LB_ADD_STRING, IntPtr.Zero, this.appendTextBox.Text); } this.itemCount = SendMessage(this.listBoxHandle, LB_GET_COUNT, IntPtr.Zero, IntPtr.Zero); this.itemCountTextBlock.Text = this.itemCount.ToString(); } #endregion #region Delete 버튼 클릭시 처리하기 - deleteButton_Click(sender, e) /// <summary> /// Delete 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void deleteButton_Click(object sender, EventArgs e) { this.selectedItemIndex = SendMessage(this.controlHost.ListBoxWindowHandle, LB_GET_CUR_SEL, IntPtr.Zero, IntPtr.Zero); if(this.selectedItemIndex != -1) { SendMessage(this.listBoxHandle, LB_DELETE_STRING, (IntPtr) this.selectedItemIndex, IntPtr.Zero); } this.itemCount = SendMessage(this.listBoxHandle, LB_GET_COUNT, IntPtr.Zero, IntPtr.Zero); this.itemCountTextBlock.Text = this.itemCount.ToString(); } #endregion } } |