■ Window 클래스에서 윈도우를 화면 왼쪽에 고정하고 나머지 영역을 작업 영역으로 설정하는 방법을 보여준다.
▶ MainWindow.xaml
1 2 3 4 5 6 7 8 9 10 11 |
<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"> </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 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 311 312 313 314 315 316 317 318 319 320 |
using System; using System.ComponentModel; using System.Runtime.InteropServices; using System.Windows; using System.Windows.Interop; namespace TestProject; /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Structure ////////////////////////////////////////////////////////////////////////////////////////// Private #region 사각형 - RECT /// <summary> /// 사각형 /// </summary> [StructLayout(LayoutKind.Sequential)] private struct RECT { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Public #region Field /// <summary> /// 왼쪽 /// </summary> public int Left; /// <summary> /// 위쭉 /// </summary> public int Top; /// <summary> /// 오른쪽 /// </summary> public int Right; /// <summary> /// 아래쪽 /// </summary> public int Bottom; #endregion } #endregion #region 모니터 정보 - MONITOR_INFORMATION /// <summary> /// 모니터 정보 /// </summary> [StructLayout(LayoutKind.Sequential)] private struct MONITOR_INFORMATION { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Public #region Field /// <summary> /// 크기 /// </summary> public uint Size; /// <summary> /// 모니터 사각형 /// </summary> public RECT MonitorRectangle; /// <summary> /// 작업 영역 사각형 /// </summary> public RECT WorkAreaRectangle; /// <summary> /// 플래그 /// </summary> public uint Flag; #endregion } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Import ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 시스템 메트릭스 구하기 - GetSystemMetrics(index) /// <summary> /// 시스템 메트릭스 구하기 /// </summary> /// <param name="index">인덱스</param> /// <returns>처리 결과</returns> [DllImport("user32")] private static extern int GetSystemMetrics(int index); #endregion #region 윈도우에서 모니터 구하기 - MonitorFromWindow(windowHandle, flag) /// <summary> /// 윈도우에서 모니터 구하기 /// </summary> /// <param name="windowHandle">윈도우 핸들</param> /// <param name="flag">플래그</param> /// <returns>모니터 핸들</returns> [DllImport("user32")] private static extern IntPtr MonitorFromWindow(IntPtr windowHandle, uint flag); #endregion #region 모니터 정보 구하기 - GetMonitorInfo(monitorHandle, monitorInformation) /// <summary> /// 모니터 정보 구하기 /// </summary> /// <param name="monitorHandle">모니터 핸들</param> /// <param name="monitorInformation">모니터 정보</param> /// <returns>처리 결과</returns> [DllImport("user32")] private static extern bool GetMonitorInfo(IntPtr monitorHandle, ref MONITOR_INFORMATION monitorInformation); #endregion #region 시스템 매개 변수 정보 설정하기 - SystemParametersInfo(action, parameter1, parameter2, flag) /// <summary> /// 시스템 매개 변수 정보 설정하기 /// </summary> /// <param name="action">액션</param> /// <param name="parameter1">매개 변수 1</param> /// <param name="parameter2">매개 변수 2</param> /// <param name="flag">플래그</param> /// <returns>처리 결과</returns> [DllImport("user32", SetLastError = true)] private static extern bool SystemParametersInfo(uint action, uint parameter1, IntPtr parameter2, uint flag); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// SM_CXSCREEN /// </summary> private const int SM_CXSCREEN = 0; /// <summary> /// SM_CYSCREEN /// </summary> private const int SM_CYSCREEN = 1; /// <summary> /// MONITOR_DEFAULTTONEAREST /// </summary> private const uint MONITOR_DEFAULTTONEAREST = 2; /// <summary> /// SPI_SETWORKAREA /// </summary> private const uint SPI_SETWORKAREA = 0x002F; /// <summary> /// 원본 작업 영역 사각형 /// </summary> private RECT originalWorkAreaRectangle; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); Loaded += Window_Loaded; Closing += Window_Closing; } #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) { SetWindowPosition(); SetWorkArea(); SetTopMostWindow(); } #endregion #region 윈도우 닫을 경우 처리하기 - Window_Closing(sender, e) /// <summary> /// 윈도우 닫을 경우 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void Window_Closing(object sender, CancelEventArgs e) { RestoreWorkArea(); } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 윈도우 위치 설정하기 - SetWindowPosition() /// <summary> /// 윈도우 위치 설정하기 /// </summary> private void SetWindowPosition() { IntPtr windowHandle = new WindowInteropHelper(this).Handle; IntPtr monitorHandle = MonitorFromWindow(windowHandle, MONITOR_DEFAULTTONEAREST); MONITOR_INFORMATION monitorInformation = new MONITOR_INFORMATION(); monitorInformation.Size = (uint)Marshal.SizeOf(typeof(MONITOR_INFORMATION)); GetMonitorInfo(monitorHandle, ref monitorInformation); RECT workAreaRectangle = monitorInformation.WorkAreaRectangle; Width = 400 + 7; Height = workAreaRectangle.Bottom - workAreaRectangle.Top + 7; Left = workAreaRectangle.Right - 400; Top = workAreaRectangle.Top; } #endregion #region 작업 영역 설정하기 - SetWorkArea() /// <summary> /// 작업 영역 설정하기 /// </summary> private void SetWorkArea() { IntPtr windowHandle = new WindowInteropHelper(this).Handle; IntPtr monitorHandle = MonitorFromWindow(windowHandle, MONITOR_DEFAULTTONEAREST); MONITOR_INFORMATION monitorInformation = new MONITOR_INFORMATION(); monitorInformation.Size = (uint)Marshal.SizeOf(typeof(MONITOR_INFORMATION)); GetMonitorInfo(monitorHandle, ref monitorInformation); this.originalWorkAreaRectangle = monitorInformation.WorkAreaRectangle; RECT workAreaRectangle = monitorInformation.WorkAreaRectangle; workAreaRectangle.Right -= (int)Width; IntPtr workAreaRectangleHandle = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(RECT))); Marshal.StructureToPtr(workAreaRectangle, workAreaRectangleHandle, false); SystemParametersInfo(SPI_SETWORKAREA, 0, workAreaRectangleHandle, 0); Marshal.FreeHGlobal(workAreaRectangleHandle); } #endregion #region 작업 영역 복원하기 - RestoreWorkArea() /// <summary> /// 작업 영역 복원하기 /// </summary> private void RestoreWorkArea() { IntPtr workAreaRectangleHandle = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(RECT))); Marshal.StructureToPtr(originalWorkAreaRectangle, workAreaRectangleHandle, false); SystemParametersInfo(SPI_SETWORKAREA, 0, workAreaRectangleHandle, 0); Marshal.FreeHGlobal(workAreaRectangleHandle); } #endregion #region 최상위 윈도우 설정하기 - SetTopMostWindow() /// <summary> /// 최상위 윈도우 설정하기 /// </summary> private void SetTopMostWindow() { Topmost = true; } #endregion } |