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)