C # Winform Implementation code that implements the win and alt+f4 of the shielded keyboard

Source: Internet
Author: User

Recently in a spoof program, is opened, the program gets the desktop then, and then full screen display on the screen, the user can not do anything at this time.

At this time, I hope that the user can not use the keyboard alt+f4 to end the program and through the win key to the window operation. I searched the Internet, using the global keyboard hook method can be done to block the user's keyboard operation. The following is the related code, which uses the Form1_Load event and the Form1_formclosing event:

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{//mounting hooks [DllImport ("User32.dll")]public static extern int SetWindowsHookEx (int idhook, HookProc lpfn, IntPtr hinstance, int threadId);//unload Hook [DllImport ("User32.dll")]public static extern bool UnhookWindowsHookEx (int idhook);//Continue Next Hook [DllImport ("User32.dll")]public static extern int CallNextHookEx (int idhook, int nCode, Int32 wParam, IntPtr lParam);//Declaration defines public delegate int HookProc (int nCode, Int32 wPa RAM, IntPtr lParam); static int hkeyboardhook = 0; HookProc keyboardhookprocedure;public Form1 () {InitializeComponent ();} private void Form1_Load (object sender, EventArgs e) {Hookstart ();} private void Form1_formclosing (object sender, FormClosingEventArgs e) {hookstop ();} Install hook public void Hookstart () {if (HkeyboardhoOK = = 0) {//create HookProc instance keyboardhookprocedure = new HookProc (KEYBOARDHOOKPROC);//define global hook Hkeyboardhook = SetWindowsHookEx (Keyboardhookprocedure, Marshal.gethinstance (assembly.getexecutingassembly). GetModules () [0]), 0);
If not, put Marshal.gethinstance (assembly.getexecutingassembly (). GetModules () [0]) changed to IntPtr.Zero
if (Hkeyboardhook = = 0) {hookstop (); throw new Exception ("SetWindowsHookEx failed.");}} }//The hook is what the hook is going to do. private int Keyboardhookproc (int nCode, Int32 wParam, IntPtr lParam) {//Here you can add another function of code return 1;}//unload hook public void Hoo Kstop () {bool Retkeyboard = true; if (Hkeyboardhook! = 0) {Retkeyboard = UnhookWindowsHookEx (hkeyboardhook); Hkeyboardho OK = 0; } if (! ( Retkeyboard)) throw new Exception ("UnhookWindowsHookEx failed."); } } }

(Note: This method can shield win and alt+f4 but not shield Ctrl+alt+del)

C # Winform Implementation code that implements the win and alt+f4 of the shielded keyboard

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.