WINFORM程序如何屏蔽系统热键
//我前两天刚刚做的,屏蔽桌面都可以,不过小心使用啊,
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Diagnostics;
using System.Reflection;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Reflection;
using System.Security.Permissions;
using Microsoft.Win32;
namespace testHook
{
public partial class Form1 : Form
{
//委托
public delegate int HookProc(int nCode, int wParam, IntPtr lParam);
static int hHook = 0;
public const int WH_KEYBOARD_LL = 13;
//LowLevel键盘截获,如果是WH_KEYBOARD=2,并不能对系统键盘截取,Acrobat Reader会在你截取之前获得键盘。
HookProc KeyBoardHookProcedure;
//键盘Hook结构函数
[StructLayout(LayoutKind.Sequential)]
public class KeyBoardHookStruct
{
public int vkCode;
public int scanCode;
public int flags;
public int time;
public int dwExtraInfo;
}
#region DllImport
//设置钩子
[DllImport("user32.dll")]
public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
//抽掉钩子
public static extern bool UnhookWindowsHookEx(int idHook);
[DllImport("user32.dll")]
//调用下一个钩子
public static extern int CallNextHookEx(int idHook, int nCode, int wParam, IntPtr lParam);
[DllImport("kernel32.dll")]
public static extern int GetCurrentThreadId();
[DllImport("kernel32.dll")]
public static extern IntPtr GetModuleHandle(string name);
[DllImport("User32.dll")]
public static extern IntPtr FindWindowEx(IntPtr ph, IntPtr ch, String cn, String wn);
[DllImport("User32.dll")]
public static extern bool ShowWindow(IntPtr hWnd, long nCmdShow);
private const uint spi_screensaverrunning = 97;
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern bool SystemParametersInfo(uint uiAction, uint uiParam, ref bool pvParam, uint fWinIni);
#endregion
#region 自定义事件
public void HookStart()
{
// 安装键盘钩子
if (hHook == 0)
{
KeyBoardHookProcedure = new HookProc(KeyBoardHookProc);
hHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyBoardHookProcedure,
GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName), 0);
//如果设置钩子失败.
if (hHook == 0)
{
HookClear();
//throw new Exception("set Hook failed!");
}
}
}
//取消钩子事件
public void HookClear()
{
bool retKeyboard = true;
if (hHook != 0)
{
retKeyboard = UnhookWindowsHookEx(hHook);
hHook = 0;
}
//如果去掉钩子失败.
if (!retKeyboard) throw new Exception("UnhookWindowsHookEx failed.");
}
//这里可以添加自己想要的信息处理
public static int KeyBoardHookProc(int nCode, int wParam, IntPtr lParam)
{
if (nCode >= 0)
{
KeyBoardHookStruct kbh = (KeyBoardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyBoardHookStruct));
if (kbh.vkCode == (int)Keys.S && (int)Control.ModifierKeys == (int)Keys.Control) // 截获Ctrl+S
{
return 1;
}
if (kbh.vkCode == (int)Keys.Y && (int)Control.ModifierKeys == (int)Keys.Control + (int)Keys.Alt) //截获Ctrl+Alt+Y
{
return 1;
}
if (kbh.vkCode == (int)Keys.LWin) //截获win
{
return 1;
}
if (kbh.vkCode == (int)Keys.Tab && (int)Control.ModifierKeys == (int)Keys.Alt) //截获Ctrl+Tab
{
return 1;
}
if (kbh.vkCode == (int)Keys.F4 && (int)Control.ModifierKeys == (int)Keys.Alt)//截获Alt + F4
{
return 1;
}
if (kbh.vkCode == (int)Keys.E && (int)Control.ModifierKeys == (int)Keys.LWin)//截获win + e
{
return 1;
}
if (kbh.vkCode == (int)Keys.Escape && (int)Control.ModifierKeys == (int)Keys.Alt)////截获Alt + esc
{
return 1;
}
if ((int)Control.ModifierKeys == (int)Keys.Control + (int)Keys.Alt + (int)Keys.Delete)//截获Ctrl+Alt+Delete 不成功 , 系统不返回键盘值
{
return 1;
}
if (kbh.vkCode == (int)Keys.Escape && (int)Control.ModifierKeys == (int)Keys.Control)////截获Ctrl + esc
{
return 1;
}
}
return CallNextHookEx(hHook, nCode, wParam, lParam);
}
#endregion
public void SetCtrlAltDeleteStatus(int value)
{
//1不可用 0可用
Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.CurrentUser;
Microsoft.Win32.RegistryKey keySystem = key.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System");
keySystem.SetValue("DisableTaskMgr", 1, Microsoft.Win32.RegistryValueKind.DWord);
RegistryKey regKey;
regKey = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System", true);
if (regKey == null)
{
regKey = Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System");
}
regKey.SetValue("DisableTaskMgr", value, RegistryValueKind.DWord);
}
private void DisableAll()
{
HookStart();
IntPtr handleTaskMask = FindWindowEx(IntPtr.Zero, IntPtr.Zero, "Shell_TrayWnd", null);
ShowWindow(handleTaskMask, 0);
IntPtr handleDeskTop = FindWindowEx(IntPtr.Zero, IntPtr.Zero, "Progman", null);
ShowWindow(handleDeskTop, 0);
SetCtrlAltDeleteStatus(1);
bool p = true;
SystemParametersInfo(spi_screensaverrunning, 1, ref p, 0);
}
private void RecoverAll()
{
HookClear();
IntPtr handle = FindWindowEx(IntPtr.Zero, IntPtr.Zero, "Shell_TrayWnd", null);
ShowWindow(handle, 1);
IntPtr handleDeskTop = FindWindowEx(IntPtr.Zero, IntPtr.Zero, "Progman", null);
ShowWindow(handleDeskTop, 1);
SetCtrlAltDeleteStatus(0);
bool p = true;
SystemParametersInfo(spi_screensaverrunning, 0, ref p, 0);
}
public Form1()
{
string xx = Environment.CurrentDirectory;
InitializeComponent();
DisableAll();
}
private void Form1_Load(object sender, EventArgs e)
{
// statrhook();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
RecoverAll();
//sotphook();
}
}
}

浙公网安备 33010602011771号