C # Winform Implementation of the Screen Keyboard win and ALT+F4 code _c# tutorial

Source: Internet
Author: User
At this point, you want the user not to end the program through the keyboard alt+f4, and through the win key combination of the window operation. I searched the Internet, using the global keyboard hook method can do to screen the user to the keyboard operation. The following are related code that uses the Form1_Load event and the Form1_formclosing event:
Copy Code code 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
{
Installing hooks
[DllImport ("user32.dll")]
public static extern int SetWindowsHookEx (int idhook, HookProc lpfn, IntPtr hinstance, int threadId);
Uninstall Hook
[DllImport ("user32.dll")]
public static extern bool UnhookWindowsHookEx (int idhook);
Continue to 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 Form1_formclosing (object sender, FormClosingEventArgs e)
{
Hookstop ();
}
Installing hooks
public void Hookstart ()
{
if (Hkeyboardhook = 0)
{
Create a HookProc instance
Keyboardhookprocedure = new HookProc (KEYBOARDHOOKPROC);
Define Global Hooks
Hkeyboardhook = SetWindowsHookEx (Keyboardhookprocedure, Marshal.gethinstance (assembly.getexecutingassembly (). GetModules () [0]), 0);
if (Hkeyboardhook = 0)
{
Hookstop ();
throw new Exception ("SetWindowsHookEx failed.");
}
}
}
The hook is the hook to do the thing.
private int Keyboardhookproc (int ncode, Int32 wParam, IntPtr LParam)
{
Here you can add other features to the code
return 1;
}
Uninstall 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 shield win and alt+f4 but not shield Ctrl+alt+del)
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.