C#winform程序怎样屏蔽热键,如alt+f4等等,还有任务管理器
展开全部
通过修改注册表来屏蔽任务管理器
try
{
RegistryKey r = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System", true);
r.SetValue("DisableTaskMgr", "1"); //屏蔽任务管理器
}
catch
{
RegistryKey r = Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System");
r.DeleteValue("DisableTaskMgr") ;
}
使用Hook API,通过平台调用引入Win32 Hook API,把键盘的Alt、F4和Tab、Del+Ctrl,Hook掉。
参考下这个:
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.");
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询