■ 잠금 화면을 설정하는 방법을 보여준다.
▶ WallForm.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 |
using System; using System.Windows.Forms; namespace TestProject { /// <summary> /// 벽 폼 /// </summary> public partial class WallForm : Form { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 부모 종료 버튼 - ParentFinishButton /// <summary> /// 부모 종료 버튼 /// </summary> public Button ParentFinishButton { get; set; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - WallForm() /// <summary> /// 생성자 /// </summary> public WallForm() { InitializeComponent(); SetStyle(ControlStyles.DoubleBuffer , true); SetStyle(ControlStyles.AllPaintingInWmPaint, true); SetStyle(ControlStyles.UserPaint , true); this.returnButton.Click += returnButton_Click; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 돌아가기 버튼 클릭시 처리하기 - returnButton_Click(sender, e) /// <summary> /// 돌아가기 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void returnButton_Click(object sender, EventArgs e) { ParentFinishButton.PerformClick(); } #endregion } } |
▶ WindowsHelper.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 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 |
using Microsoft.Win32; using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Runtime.InteropServices; using System.Windows.Forms; namespace TestProject { /// <summary> /// 윈도우즈 헬퍼 /// </summary> public class WindowsHelper { //////////////////////////////////////////////////////////////////////////////////////////////////// Structure ////////////////////////////////////////////////////////////////////////////////////////// Private #region 키보드 저수준 후킹 구조체 - KBDLLHOOKSTRUCT /// <summary> /// 키보드 저수준 후킹 구조체 /// </summary> private struct KBDLLHOOKSTRUCT { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Public #region Field /// <summary> /// 가상 키 코드 /// </summary> public int VirtualKeyCode; /// <summary> /// 스캔 코드 /// </summary> public int ScanCode; /// <summary> /// 플래그 /// </summary> public int Flags; /// <summary> /// 시간 /// </summary> public int Time; /// <summary> /// 부가 정보 /// </summary> public int ExtraInfo; #endregion } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Delegate ////////////////////////////////////////////////////////////////////////////////////////// Private #region 키보드 후킹 처리 대리자 - ProcessKeyboardHookDelegate(code, wordParameter, longParameter) /// <summary> /// 키보드 후킹 처리 대리자 /// </summary> /// <param name="code">코드</param> /// <param name="wordParameter">WORD 매개 변수</param> /// <param name="longParameter">LONG 매개 변수</param> /// <returns>처리 결과</returns> private delegate int ProcessKeyboardHookDelegate(int code, int wordParameter, ref KBDLLHOOKSTRUCT longParameter); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Import ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 윈도우 후킹 설정하기 - SetWindowsHookEx(hookID, processKeyboardHookDelegate, moduleHandle, threadID) /// <summary> /// 윈도우 후킹 설정하기 /// </summary> /// <param name="hookID">후킹 ID</param> /// <param name="processKeyboardHookDelegate">키보드 후킹 처리 대리자</param> /// <param name="moduleHandle">모듈 핸들</param> /// <param name="threadID">스레드 ID</param> /// <returns>처리 결과</returns> [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] private static extern int SetWindowsHookEx ( int hookID, ProcessKeyboardHookDelegate processKeyboardHookDelegate, IntPtr moduleHandle, uint threadID ); #endregion #region 윈도우 후킹 해제하기 - UnhookWindowsHookEx(hookHandle) /// <summary> /// 윈도우 후킹 해제하기 /// </summary> /// <param name="hookHandle">후킹 핸들</param> /// <returns>처리 결과</returns> [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] private static extern bool UnhookWindowsHookEx(int hookHandle); #endregion #region 다음 후킹 호출하기 - CallNextHookEx(hookHandle, code, wordParameter, longParameter) /// <summary> /// 다음 후킹 호출하기 /// </summary> /// <param name="hookHandle">후킹 핸들</param> /// <param name="code">코드</param> /// <param name="wordParameter">WORD 매개 변수</param> /// <param name="longParameter">LONG 매개 변수</param> /// <returns>처리 결과</returns> [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] private static extern int CallNextHookEx(int hookHandle, int code, int wordParameter, ref KBDLLHOOKSTRUCT longParameter); #endregion #region 모듈 핸들 구하기 - GetModuleHandle(modulName) /// <summary> /// 모듈 핸들 구하기 /// </summary> /// <param name="modulName">모듈명</param> /// <returns>모듈 핸들</returns> [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] private static extern IntPtr GetModuleHandle(string modulName); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 키보드 후킹 처리 대리자 /// </summary> private static ProcessKeyboardHookDelegate _processKeyboardHookDelegate = ProcessKeyboardHook; /// <summary> /// 후킹 ID /// </summary> private static int _hookID = 0; /// <summary> /// 태스크 관리자 이용 가능 여부 /// </summary> private static bool _taskManagerEnabled = true; /// <summary> /// 특수 키보드 입력 이용 가능 여부 /// </summary> private static bool _specialKeyboardInputEnabled = true; #endregion ////////////////////////////////////////////////////////////////////////////////////////// Instance //////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// WH_KEYBOARD_LL /// </summary> private const int WH_KEYBOARD_LL = 13; /// <summary> /// WM_KEYDOWN /// </summary> private const int WM_KEYDOWN = 0x0100; /// <summary> /// WM_KEYUP /// </summary> private const int WM_KEYUP = 0x0101; /// <summary> /// WM_SYSKEYDOWN /// </summary> private const int WM_SYSKEYDOWN = 0x0104; /// <summary> /// WM_SYSKEYUP /// </summary> private const int WM_SYSKEYUP = 0x0105; /// <summary> /// 부모 종료 버튼 /// </summary> private Button parentFinishButton; /// <summary> /// 잠금 여부 /// </summary> private bool isLocked = false; /// <summary> /// 화면 리스트 /// </summary> private List<Screen> screenList = null; /// <summary> /// 폼 리스트 /// </summary> private List<WallForm> formList = null; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - WindowsHelper(parentFinishButton) /// <summary> /// 생성자 /// </summary> /// <param name="parentFinishButton">부모 종료 버튼</param> public WindowsHelper(Button parentFinishButton) { this.parentFinishButton = parentFinishButton; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 태스크 관리자 비활성화 하기 - DisableTaskManager() /// <summary> /// 태스크 관리자 비활성화 하기 /// </summary> /// <remarks>Ctrl + Alt + Delete 키 입력을 거부한다.</remarks> public static void DisableTaskManager() { if(!_taskManagerEnabled) { return; } RegistryKey registryKey; int keyValue = 1; string subKey = "Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System"; registryKey = Registry.CurrentUser.CreateSubKey(subKey); registryKey.SetValue("DisableTaskMgr", keyValue); registryKey.Close(); _taskManagerEnabled = false; } #endregion #region 태스크 관리자 활성화 하기 - EnableTaskManager() /// <summary> /// 태스크 관리자 활성화 하기 /// </summary> /// <remarks>Ctrl + Alt + Delete 키 입력을 허용한다.</remarks> public static void EnableTaskManager() { if(_taskManagerEnabled) { return; } RegistryKey currentUserRegistryKey = Registry.CurrentUser; string subKey = "Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System"; RegistryKey registryKey = currentUserRegistryKey.OpenSubKey(subKey); if(registryKey != null) { currentUserRegistryKey.DeleteSubKeyTree(subKey); } _taskManagerEnabled = true; } #endregion #region 특수 키보드 입력 비활성 화기 - DisableSpecialKeyboardInput() /// <summary> /// 특수 키보드 입력 비활성 화기 /// </summary> public static void DisableSpecialKeyboardInput() { if(!_specialKeyboardInputEnabled) { return; } _hookID = SetHook(_processKeyboardHookDelegate); _specialKeyboardInputEnabled = false; } #endregion #region 특수 키보드 입력 활성화 하기 - EnableSpecialKeyboardInput() /// <summary> /// 특수 키보드 입력 활성화 하기 /// </summary> public static void EnableSpecialKeyboardInput() { if(_specialKeyboardInputEnabled) { return; } UnhookWindowsHookEx(_hookID); _specialKeyboardInputEnabled = true; } #endregion //////////////////////////////////////////////////////////////////////////////// Private #region 후킹 설정하기 - SetHook(processKeyboardHookDelegate) /// <summary> /// 후킹 설정하기 /// </summary> /// <param name="processKeyboardHookDelegate">키보드 후킹 처리 대리자</param> /// <returns>처리 결과</returns> private static int SetHook(ProcessKeyboardHookDelegate processKeyboardHookDelegate) { using(Process process = Process.GetCurrentProcess()) { using(ProcessModule processModule = process.MainModule) { return SetWindowsHookEx(WH_KEYBOARD_LL, processKeyboardHookDelegate, GetModuleHandle(processModule.ModuleName), 0); } } } #endregion #region 키보드 후킹 처리하기 - ProcessKeyboardHook(code, wordParameter, longParameter) /// <summary> /// 키보드 후킹 처리하기 /// </summary> /// <param name="code">코드</param> /// <param name="wordParameter">WORD 매개 변수</param> /// <param name="longParameter">LONG 매개 변수</param> /// <returns>처리 결과</returns> private static int ProcessKeyboardHook(int code, int wordParameter, ref KBDLLHOOKSTRUCT longParameter) { bool result = false; switch(wordParameter) { case WM_KEYDOWN : case WM_KEYUP : case WM_SYSKEYDOWN : case WM_SYSKEYUP : result = ((longParameter.VirtualKeyCode == 0x09) && (longParameter.Flags == 0x20)) || // Alt + Tab ((longParameter.VirtualKeyCode == 0x1B) && (longParameter.Flags == 0x20)) || // Alt + Esc ((longParameter.VirtualKeyCode == 0x1B) && (longParameter.Flags == 0x00)) || // Ctrl + Esc ((longParameter.VirtualKeyCode == 0x5B) && (longParameter.Flags == 0x01)) || // Left Windows Key ((longParameter.VirtualKeyCode == 0x5C) && (longParameter.Flags == 0x01)) || // Right Windows Key ((longParameter.VirtualKeyCode == 0x73) && (longParameter.Flags == 0x20)); // Alt + F4 break; } if(result == true) { return 1; } else { return CallNextHookEx(0, code, wordParameter, ref longParameter); } } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Instance //////////////////////////////////////////////////////////////////////////////// Public #region 화면 잠그기 - LockScreen() /// <summary> /// 화면 잠그기 /// </summary> public void LockScreen() { if(this.isLocked) { return; } DisableTaskManager(); DisableSpecialKeyboardInput(); this.screenList = Screen.AllScreens.ToList(); this.formList = new List<WallForm>(); for(int i = 0; i < this.screenList.Count; i++) { WallForm form = new WallForm(); form.ParentFinishButton = this.parentFinishButton; form.FormBorderStyle = FormBorderStyle.None; form.WindowState = FormWindowState.Maximized; form.StartPosition = FormStartPosition.Manual; form.Left = this.screenList[i].WorkingArea.Left; form.Top = this.screenList[i].WorkingArea.Top; form.Width = this.screenList[i].WorkingArea.Width; form.Height = this.screenList[i].WorkingArea.Height; form.TopMost = true; this.formList.Add(form); form.Show(); } this.isLocked = true; } #endregion #region 화면 잠금 취소하기 - UnlockScreen() /// <summary> /// 화면 잠금 취소하기 /// </summary> public void UnlockScreen() { if(!this.isLocked) { return; } EnableTaskManager(); EnableSpecialKeyboardInput(); for(int i = this.formList.Count - 1; i > -1; i--) { WallForm form = this.formList[i]; this.formList.Remove(form); form.Hide(); form.Dispose(); } this.isLocked = false; } #endregion } } |
▶ 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 |
using System; using System.Windows.Forms; namespace TestProject { /// <summary> /// 메인 폼 /// </summary> public partial class MainForm : Form { private WindowsHelper helper; //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainForm() /// <summary> /// 생성자 /// </summary> public MainForm() { InitializeComponent(); this.helper = new WindowsHelper(this.unlockButton); this.lockButton.Click += lockButton_Click; this.unlockButton.Click += unlockButton_Click; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 화면 잠금 버튼 클릭시 처리하기 - lockButton_Click(sender, e) /// <summary> /// 화면 잠금 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void lockButton_Click(object sender, EventArgs e) { this.helper.LockScreen(); } #endregion #region 잠금 취소 버튼 클릭시 처리하기 - unlockButton_Click(sender, e) /// <summary> /// 잠금 취소 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void unlockButton_Click(object sender, EventArgs e) { this.helper.UnlockScreen(); } #endregion } } |
※ 관리자 권한으로 실행한다.