In this case, you do not want to end with ALT + F4 on the keyboard. Program And windows through the win key combination. I searched the internet and used the global keyboard Hook method to block users' operations on the keyboard .. The following are related Code The form1_load event and form1_formclosing event are used:
Copy code The Code is as follows: using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. text;
Using system. Windows. forms;
Using system. runtime. interopservices;
Using system. reflection;
Namespace windowsapplication10
{
Public partial class form1: Form
{
// Install the hook
[Dllimport ("user32.dll")]
Public static extern int setwindowshookex (INT idhook, hookproc lpfn, intptr hinstance, int threadid );
// Uninstall the hook
[Dllimport ("user32.dll")]
Public static extern bool unhookwindowshookex (INT idhook );
// Continue the next hook
[Dllimport ("user32.dll")]
Public static extern int callnexthookex (INT idhook, int ncode, int32 wparam, intptr lparam );
// Declaration definition
Public Delegate int hookproc (INT ncode, int32 wparam, intptr lparam );
Static int hkeyboardhook = 0;
Hookproc keyboardhookprocedure;
Public form1 ()
{
Initializecomponent ();
}
Private void form1_load (Object sender, eventargs E)
{
Hookstart ();
}
Private void form=formclosing (Object sender, formclosingeventargs E)
{
Hookstop ();
}
// Install the hook
Public void hookstart ()
{
If (hkeyboardhook = 0)
{
// Create a hookproc instance
Keyboardhookprocedure = new hookproc (keyboardhookproc );
// Define a global hook
Hkeyboardhook = setwindowshookex (13, keyboardhookprocedure, Marshal. gethinstance (assembly. getexecutingassembly (). getmodules () [0]), 0 );
If (hkeyboardhook = 0)
{
Hookstop ();
Throw new exception ("setwindowshookex failed .");
}
}
}
// The Hook process is what the hook is going to do.
Private int keyboardhookproc (INT ncode, int32 wparam, intptr lparam)
{
// Code for other functions can be added here
Return 1;
}
// Uninstall the hook
Public void hookstop ()
{
Bool retkeyboard = true;
If (hkeyboardhook! = 0)
{
Retkeyboard = unhookwindowshookex (hkeyboardhook );
Hkeyboardhook = 0;
}
If (! (Retkeyboard) throw new exception ("unhookwindowshookex failed .");
}
}
}
(Note: This method can block win and ALT + F4, but cannot block CTRL + ALT + DEL)