C # winform implements win and ALT + F4 code for shielding the keyboard

Source: Internet
Author: User

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)

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.