C #. NET lock screen program (with blocking Task Manager)

Source: Internet
Author: User

Step 1: Set FormBorderStyle to none and WindowState to Maximized.

Occupies the entire screen.

Step 2: use hooks to monitor global Keyboard Events. This shields most system hotkeys. However, blocking ctrl + alt + del Task Manager is complicated. This special case will be discussed later.

Note when using global hooks: place the code in an independent class library (only dll can be injected into other processes ).

Using System;
Using System. Collections. Generic;
Using System. Text;
Using System. Runtime. InteropServices;
Using System. IO;
Using System. Reflection;

Namespace KeyboardHookDLL
{
Public class KeyboardHook
{
Public delegate int KeyboardProc (int nCode, IntPtr wParam, IntPtr lParam );

Static int hKeyboardHook = 0;
KeyboardProc KeyboardHookProcedure;

/// <Summary>
/// Hook function, which must reference space (using System. Reflection ;)
/// Set the thread hook to listen to the keyboard message to 2, and the global hook to listen to the keyboard message to 13
/// Set the mouse message to 7 for thread hook monitoring and the mouse message to 14 for global hook monitoring.
/// </Summary>

Public const int WH_KEYBOARD = 13;
Public const int WH_MOUSE_LL = 14;

Public struct KeyboardMSG
{
Public int vkCode;
Public int scanCode;
Public int flags;
Public int time;
Public int dwExtraInfo;

}
// Private FileStream MyFs;
// ASC codes of various key locations
Private const byte LLKHF_ALTDOWN = 0x20;
Private const byte VK_CAPITAL = 0x14;
Private const byte VK_ESCAPE = 0x1B;
Private const byte VK_F4 = 0x73;
Private const byte VK_LCONTROL = 0xA2;
Private const byte VK_NUMLOCK = 0x90;
Private const byte VK_RCONTROL = 0xA3;
Private const byte VK_SHIFT = 0x10;
Private const byte VK_TAB = 0x09;
// Public const int WH_KEYBOARD = 13;
Private const int WH_KEYBOARD_LL = 13;
Private const int WH_MOUSE = 7;
// Private const int WH_MOUSE_LL = 14;
Private const int WM_KEYDOWN = 0x100;
Private const int WM_KEYUP = 0x101;
Private const int WM_LBUTTONDBLCLK = 0x203;
Private const int WM_LBUTTONDOWN = 0x201;
Private const int WM_LBUTTONUP = 0x202;
Private const int WM_MBUTTONDBLCLK = 0x209;
Private const int WM_MBUTTONDOWN = 0x207;
Private const int WM_MBUTTONUP = 0x208;
Private const int WM_MOUSEMOVE = 0x200;
Private const int WM_MOUSEWHEEL = 0x020A;
Private const int WM_RBUTTONDBLCLK = 0x206;
Private const int WM_RBUTTONDOWN = 0x204;
Private const int WM_RBUTTONUP = 0x205;
Private const int WM_SYSKEYDOWN = 0x104;
Private const int WM_SYSKEYUP = 0x105;
// Private static int hKeyboardHook = 0;

/// <Summary>
/// The declaration method in vs2008 is slightly different in vs2010

/// </Summary>
/// <Returns> </returns>
[DllImport ("kernel32")]
Public static extern int GetCurrentThreadId ();

[DllImport ("user32.dll", CharSet = CharSet. Auto, CallingConvention =

CallingConvention. StdCall)]
Public static extern int SetWindowsHookEx (int idHook, KeyboardProc lpfn, IntPtr

HInstance, int threadId );

[DllImport ("user32.dll", CharSet = CharSet. Auto, CallingConvention =

CallingConvention. StdCall)]
Public static extern bool UnhookWindowsHookEx (int idHook );

[DllImport ("user32.dll", CharSet = CharSet. Auto, CallingConvention = CallingConvention. StdCall)]
Public static extern int CallNextHookEx (int idHook, int nCode, IntPtr wParam, IntPtr lParam );

[DllImport ("user32.dll", CharSet = CharSet. Auto, CallingConvention = CallingConvention. Winapi)]
Public static extern short GetKeyState (int keycode );

// Here you can define the key to intercept.
Private int KeyboardHookProc (int nCode, IntPtr wParam, IntPtr lParam)
{
KeyboardMSG m = (KeyboardMSG) Marshal. PtrToStructure (lParam, typeof (KeyboardMSG ));
If (
(Int) m. vkCode = 91) | (int) m. vkCode = 92) |
(M. vkCode = VK_TAB) & (m. flags & LLKHF_ALTDOWN )! = 0) |
(M. vkCode = VK_ESCAPE) & (m. flags & LLKHF_ALTDOWN )! = 0) |
(M. vkCode = VK_F4) & (m. flags & LLKHF_ALTDOWN )! = 0) |
(M. vkCode = VK_ESCAPE) & (GetKeyState (VK_LCONTROL) & 0x8000 )! = 0) |
(M. vkCode = VK_ESCAPE) & (GetKeyState (VK_RCONTROL) & 0x8000 )! = 0)
)
{
Return 1;
}
Return CallNextHookEx (hKeyboardHook, nCode, wParam, lParam );
& Nb

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.