C# 禁止ALT+F4(钩子)
1. Windows Forms中禁用窗体的关闭按钮
添加必要的命名空间:
using System.Runtime.InteropServices;
添加必要的常数和API函数的引用
private const int SC_CLOSE = 0xF060;
private const int MF_ENABLED = 0x00000000; private const int MF_GRAYED = 0x00000001; private const int MF_DISABLED = 0x00000002;
[DllImport("user32.dll", EntryPoint = "GetSystemMenu")]
private static extern IntPtr GetSystemMenu(IntPtr hWnd, int bRevert); [DllImport("User32.dll")]
public static extern bool EnableMenuItem(IntPtr hMenu, int uIDEnableItem, int uEnable);
在窗体的Load事件处理函数内添加代码
private void Form6_Load(object sender, EventArgs e) {
IntPtr hMenu = GetSystemMenu(this.Handle, 0);
EnableMenuItem(hMenu, SC_CLOSE, MF_DISABLED | MF_GRAYED); }
2. Windows Forms中禁用窗体的关闭按钮和ALT+F4关闭
protected override CreateParams CreateParams { get {
const int CS_NOCLOSE = 0x200; CreateParams cp = base.CreateParams; cp.ClassStyle = cp.ClassStyle | CS_NOCLOSE; return cp; } }
3.不允许拖动窗体和最大化最小化按钮
protected override void WndProc(ref Message m) {
base.WndProc(ref m);
if (m.Msg == 0x84 && m.Result == (IntPtr)2) // 不让拖动标题栏 {
m.Result = (IntPtr)1; }
if (m.Msg == 0xA3) // 双击标题栏无反应 {
m.WParam = System.IntPtr.Zero; } }
///////////////////////////////////////////////////////////////////////////////////////////////////////// 钩子
我用的是全局键盘钩子。这个程序可以屏蔽win和alt+f4但是不能屏蔽ctrl+alt+del。我一直在找屏蔽ctrl+alt+del的方法希望会的人指点一下。用了 form_load事件和Form1_FormClosing事件 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 {
// 安装钩子
[DllImport("user32.dll")]
public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId); // 卸载钩子
[DllImport("user32.dll")]
public static extern bool UnhookWindowsHookEx(int idHook); // 继续下一个钩子 [DllImport("user32.dll")]
public static extern int CallNextHookEx(int idHook, int nCode, Int32 wParam, IntPtr lParam);
//声明定义
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(); }
// 安装钩子
public void HookStart() {
if (hKeyboardHook == 0) {
// 创建HookProc实例
KeyboardHookProcedure = new HookProc(KeyboardHookProc); //定义全局钩子
hKeyboardHook = SetWindowsHookEx(13, KeyboardHookProcedure, Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]), 0); if (hKeyboardHook == 0) {
HookStop();
throw new Exception("SetWindowsHookEx failed."); } } }
//钩子子程就是钩子所要做的事情。
private int KeyboardHookProc(int nCode, Int32 wParam, IntPtr lParam) {
//这里可以添加别的功能的代码 return 1; }
// 卸载钩子
public void HookStop() {
bool retKeyboard = true; if (hKeyboardHook != 0) {
retKeyboard = UnhookWindowsHookEx(hKeyboardHook); hKeyboardHook = 0; }
if (!(retKeyboard)) throw new Exception("UnhookWindowsHookEx failed."); } }
}
////////////////////////////////////////////////////////
//这个是仅仅屏掉alt f4
if(e.KeyCode == Keys.F4 && e.Modifiers==Keys.Alt) {
e.Handled=true; }
//这个是不让窗口以任何方式退出
class Test : System.Windows.Forms.Form {
protected override void
OnClosing(System.ComponentModel.CancelEventArgs e) {
e.Cancel = true; }
static void Main() {
System.Windows.Forms.Application.Run(new Test()); } }