C# Winform 实现屏蔽键盘的win和alt+F4的实现代码
时间:2021-07-01 11:48:18
此时希望用户不能通过键盘alt+F4来结束程序及通过Win的组合键对窗口进行操作。我在网上搜索了一下,采用全局键盘钩子的方法可以做到屏蔽用户对键盘的操作。。以下为相关代码,用到了Form1_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.");
}
}
}
(注:该方法可以屏蔽win和alt+F4但是不能屏蔽ctrl+alt+del)
标签:Winform,屏蔽键盘,win,alt+F4
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java SpringMVC数据响应超详细讲解
2022-04-08 15:10:26
![](https://img.aspxhome.com/file/2023/1/63161_0s.png)
springboot中bean的加载顺序问题
2022-01-04 19:55:57
![](https://img.aspxhome.com/file/2023/0/62510_0s.png)
C#实现自由组合本地缓存、分布式缓存和数据查询
2021-06-13 00:56:11
mybatis的动态SQL和模糊查询实例详解
2022-03-10 09:47:03
![](https://img.aspxhome.com/file/2023/1/99851_0s.png)
Android文本输入框(EditText)输入密码时显示与隐藏
2022-04-24 06:23:47
Winform 显示Gif图片的实例代码
2021-06-29 21:14:44
java基础javeSE程序逻辑控制语法
2022-09-21 23:41:47
![](https://img.aspxhome.com/file/2023/4/79914_0s.png)
Java 深入浅出掌握Collection单列集合Set
2023-10-07 18:49:32
![](https://img.aspxhome.com/file/2023/5/58545_0s.gif)
C# IQueryable及IEnumerable区别解析
2021-11-27 13:46:36
![](https://img.aspxhome.com/file/2023/6/119486_0s.png)
Java实现FIFO任务调度队列策略
2021-09-19 09:05:37
![](https://img.aspxhome.com/file/2023/7/75787_0s.jpg)
Android使用ViewPager实现图片滑动预览效果
2023-02-15 15:28:28
![](https://img.aspxhome.com/file/2023/9/88689_0s.jpg)
Spring @Transactional注解失效解决方案
2022-10-25 05:30:30
java io文件操作删除文件或文件夹的7种方法
2022-08-09 13:29:21
![](https://img.aspxhome.com/file/2023/8/99848_0s.jpg)
Android显示网络图片实例
2022-11-02 14:13:31
深入理解Java设计模式之装饰模式
2021-11-23 11:29:52
![](https://img.aspxhome.com/file/2023/3/88153_0s.jpg)
IDEA启动tomcat控制台中文乱码问题的解决方法(100%有效)
2021-06-25 10:45:23
![](https://img.aspxhome.com/file/2023/9/62699_0s.jpg)
Android通过HTTP协议实现断点续传下载实例
2022-12-05 23:39:05
Java实现简易俄罗斯方块
2022-12-18 14:07:58
![](https://img.aspxhome.com/file/2023/1/67471_0s.png)
浅谈Java HttpURLConnection请求方式
2021-06-24 12:34:49
利用源码编译Android系统的APK和可执行命令的方法
2023-12-22 13:09:46