■ Window 클래스에서 다중 모니터 중 하나의 모니터 화면 왼쪽에 윈도우를 위치시키는 방법을 보여준다.
▶ WindowPlacementHelper.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 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 |
using System; using System.Runtime.InteropServices; using System.Windows; using System.Windows.Interop; namespace TestProject; /// <summary> /// 윈도우 배치 헬퍼 /// </summary> public class WindowPlacementHelper { //////////////////////////////////////////////////////////////////////////////////////////////////// Structure ////////////////////////////////////////////////////////////////////////////////////////// Private #region CWPRETSTRUCT - CWPRETSTRUCT /// <summary> /// CWPRETSTRUCT /// </summary> [StructLayout(LayoutKind.Sequential)] private struct CWPRETSTRUCT { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Public #region Field /// <summary> /// 결과 핸들 /// </summary> public IntPtr ResultHandle; /// <summary> /// LONG 매개 변수 /// </summary> public IntPtr LongParameter; /// <summary> /// WORD 매개 변수 /// </summary> public IntPtr WordParameter; /// <summary> /// 메시지 /// </summary> public uint Message; /// <summary> /// 윈도우 핸들 /// </summary> public IntPtr WindowHandle; #endregion } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Delegate ////////////////////////////////////////////////////////////////////////////////////////// Private #region 후킹 프로시저 처리하기 - HookProcedure(code, wordParameter, longParameter) /// <summary> /// 후킹 프로시저 처리하기 /// </summary> /// <param name="code">코드</param> /// <param name="wordParameter">WORD 매개 변수</param> /// <param name="longParameter">LONG 매개 변수</param> /// <returns>처리 결과</returns> private delegate IntPtr HookProcedure(int code, IntPtr wordParameter, IntPtr longParameter); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Import ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 윈도우즈 후킹 설정하기 (확장) - SetWindowsHookEx(hookID, hookProcedure, instanceHandle, threadID) /// <summary> /// 윈도우즈 후킹 설정하기 (확장) /// </summary> /// <param name="hookID">후킹 ID</param> /// <param name="hookProcedure">후킹 프로시저</param> /// <param name="instanceHandle">인스턴스 핸들</param> /// <param name="threadID">쓰레드 ID</param> /// <returns>처리 결과</returns> [DllImport("user32")] private static extern IntPtr SetWindowsHookEx(int hookID, HookProcedure hookProcedure, IntPtr instanceHandle, uint threadID); #endregion #region 윈도우즈 후킹 해제하기 (확장) - UnhookWindowsHookEx(instanceHandle) /// <summary> /// 윈도우즈 후킹 해제하기 (확장) /// </summary> /// <param name="instanceHandle">인스턴스 핸들</param> /// <returns>처리 결과</returns> [DllImport("user32")] private static extern bool UnhookWindowsHookEx(IntPtr instanceHandle); #endregion #region 다음 후킹 호출하기 (확장) - CallNextHookEx(hookID, code, wordParameter, longParameter) /// <summary> /// 다음 후킹 호출하기 (확장) /// </summary> /// <param name="hookID">후킹 ID</param> /// <param name="code">코드</param> /// <param name="wordParameter">WORD 매개 변수</param> /// <param name="longParameter">LONG 매개 변수</param> /// <returns>처리 결과</returns> [DllImport("user32")] private static extern IntPtr CallNextHookEx(IntPtr hookID, int code, IntPtr wordParameter, IntPtr longParameter); #endregion #region 현재 쓰레드 ID 구하기 - GetCurrentThreadId() /// <summary> /// 현재 쓰레드 ID 구하기 /// </summary> /// <returns>현재 쓰레드 ID</returns> [DllImport("kernel32")] private static extern uint GetCurrentThreadId(); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// WH_CALLWNDPROCRET /// </summary> private const int WH_CALLWNDPROCRET = 12; /// <summary> /// WM_MOVE /// </summary> private const int WM_MOVE = 0x0003; /// <summary> /// WM_MOVING /// </summary> private const int WM_MOVING = 0x0216; /// <summary> /// WM_EXITSIZEMOVE /// </summary> private const int WM_EXITSIZEMOVE = 0x0232; /// <summary> /// 후킹 ID /// </summary> private IntPtr hookID = IntPtr.Zero; /// <summary> /// 후킹 프로시저 /// </summary> private HookProcedure hookProcedure; /// <summary> /// 이동 여부 /// </summary> private bool isMoving = false; /// <summary> /// 윈도우 /// </summary> private Window window; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - WindowPlacementHelper(window) /// <summary> /// 생성자 /// </summary> /// <param name="window">윈도우</param> public WindowPlacementHelper(Window window) { this.window = window; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 후킹하기 - Hook() /// <summary> /// 후킹하기 /// </summary> public void Hook() { this.hookProcedure = new HookProcedure(HookCallback); this.hookID = SetWindowsHookEx(WH_CALLWNDPROCRET, this.hookProcedure, IntPtr.Zero, GetCurrentThreadId()); } #endregion #region 후킹 해제하기 - Unhook() /// <summary> /// 후킹 해제하기 /// </summary> public void Unhook() { if(this.hookID != IntPtr.Zero) { UnhookWindowsHookEx(this.hookID); } } #endregion #region 윈도우 고정하기 - PinWindow(window) /// <summary> /// 윈도우 고정하기 /// </summary> /// <param name="window">윈도우</param> public void PinWindow(Window window) { nint windowHandle = new WindowInteropHelper(window).Handle; System.Windows.Forms.Screen currentScreen = System.Windows.Forms.Screen.FromHandle(windowHandle); System.Drawing.Rectangle workAreaRectangle = currentScreen.WorkingArea; window.Width = 400; window.Height = workAreaRectangle.Height + 7; window.Left = workAreaRectangle.Right - window.Width + 7; window.Top = workAreaRectangle.Top; PresentationSource presentationSource = PresentationSource.FromVisual(window); if(presentationSource != null) { double dpiX = presentationSource.CompositionTarget.TransformToDevice.M11; double dpiY = presentationSource.CompositionTarget.TransformToDevice.M22; window.Left /= dpiX; window.Top /= dpiY; } window.WindowState = WindowState.Normal; } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Private #region 후킹 콜백 처리하기 - HookCallback(code, wordParameter, longParameter) /// <summary> /// 후킹 콜백 처리하기 /// </summary> /// <param name="code">코드</param> /// <param name="wordParameter">WORD 매개 변수</param> /// <param name="longParameter">LONG 매개 변수</param> /// <returns>처리 결과</returns> private IntPtr HookCallback(int code, IntPtr wordParameter, IntPtr longParameter) { if(code >= 0) { CWPRETSTRUCT cwpretstruct = (CWPRETSTRUCT)Marshal.PtrToStructure(longParameter, typeof(CWPRETSTRUCT)); IntPtr windowHandle = cwpretstruct.WindowHandle; uint message = cwpretstruct.Message; if(windowHandle == new WindowInteropHelper(this.window).Handle) { switch(message) { case WM_MOVING : this.isMoving = true; break; case WM_EXITSIZEMOVE : if(this.isMoving) { this.isMoving = false; this.window.Dispatcher.Invoke(() => { PinWindow(this.window); }); } break; } } } return CallNextHookEx(hookID, code, wordParameter, longParameter); } #endregion } |
▶ MainWindow.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<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" WindowStyle="SingleBorderWindow" ResizeMode="CanMinimize" Title="TestProject" FontFamily="맑은고딕" FontSize="16"> </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 |
using System; using System.Windows; namespace TestProject; /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 윈도우 배치 헬퍼 /// </summary> private WindowPlacementHelper helper = null; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); Loaded += Window_Loaded; Closed += Window_Closed; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 윈도우 로드시 처리하기 - Window_Loaded(sender, e) /// <summary> /// 윈도우 로드시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void Window_Loaded(object sender, RoutedEventArgs e) { if(this.helper == null) { this.helper = new WindowPlacementHelper(this); this.helper.Hook(); this.helper.PinWindow(this); } } #endregion #region 윈도우를 닫은 경우 처리하기 - Window_Closed(sender, e) /// <summary> /// 윈도우를 닫은 경우 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void Window_Closed(object sender, EventArgs e) { this.helper.Unhook(); this.helper = null; } #endregion } |