c# 屏蔽快捷鍵的實現(xiàn)示例
前言
有時候開發(fā)會遇到這樣一個需求,軟件需要屏蔽用戶的組合快捷鍵或某些按鍵,避免強(qiáng)制退出軟件,防止勿操作等。
原理
1、要實現(xiàn)組合鍵,按鍵攔截,需要用到user32.dll中的SetWindowsHookEx。
2、要攔截ctrl+alt+del,需要使用ntdll.dll的ZwSuspendProcess函數(shù)掛起winlogon程序,退出之后使用ZwResumeProcess恢復(fù)winlogon程序。
3、軟件需要開啟topMost,以及全屏,否則離開軟件則攔截?zé)o效。
4、如果要實現(xiàn)熱鍵監(jiān)聽(非焦點攔截),則需要用到user32.dll的RegisterHotKey以及UnregisterHotKey。
實現(xiàn)
1、Program類
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace LockForm
{
static class Program
{
/// <summary>
/// 應(yīng)用程序的主入口點。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
SuspendWinLogon();
Application.Run(new Form1());
ResumeWinLogon();
}
[DllImport("ntdll.dll")]
public static extern int ZwSuspendProcess(IntPtr ProcessId);
[DllImport("ntdll.dll")]
public static extern int ZwResumeProcess(IntPtr ProcessId);
private static void SuspendWinLogon()
{
Process[] pc = Process.GetProcessesByName("winlogon");
if (pc.Length > 0)
{
ZwSuspendProcess(pc[0].Handle);
}
}
private static void ResumeWinLogon()
{
Process[] pc = Process.GetProcessesByName("winlogon");
if (pc.Length > 0)
{
ZwResumeProcess(pc[0].Handle);
}
}
}
}
2、Form1類
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
namespace LockForm
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "123")
{
Application.ExitThread();
}
else
{
webBrowser1.Navigate(textBox1.Text);
}
}
private void Form1_Load(object sender, EventArgs e)
{
//webBrowser1.Navigate("https://baidu.com");
HookStart();
//this.TopMost = false;
//SuspendWinLogon();
}
private void webBrowser1_NewWindow(object sender, CancelEventArgs e)
{
e.Cancel = true;
webBrowser1.Navigate(webBrowser1.Document.ActiveElement.GetAttribute("href"));
}
private void button3_Click(object sender, EventArgs e)
{
webBrowser1.GoBack();
}
private void button2_Click(object sender, EventArgs e)
{
webBrowser1.GoForward();
}
private void button4_Click(object sender, EventArgs e)
{
webBrowser1.GoHome();
}
private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{
textBox1.Text = webBrowser1.Url.ToString();
}
private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
}
#region 鍵盤鉤子
public delegate int HookProc(int nCode, int wParam, IntPtr lParam);//定義全局鉤子過程委托,以防被回收(鉤子函數(shù)原型)
HookProc KeyBoardProcedure;
//定義鍵盤鉤子的相關(guān)內(nèi)容,用于截獲鍵盤消息
static int hHook = 0;//鉤子函數(shù)的句柄
public const int WH_KEYBOARD = 13;
//鉤子結(jié)構(gòu)函數(shù)
public struct KeyBoardHookStruct
{
public int vkCode;
public int scanCode;
public int flags;
public int time;
public int dwExtraInfo;
}
//安裝鍵盤鉤子
public void HookStart()
{
if (hHook == 0)
{
//實例化一個HookProc對象
KeyBoardProcedure = new HookProc(Form1.KeyBoardHookProc);
//創(chuàng)建線程鉤子
hHook = Win32API.SetWindowsHookEx(WH_KEYBOARD, KeyBoardProcedure, Win32API.GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName), 0);
//如果設(shè)置線程鉤子失敗
if (hHook == 0)
{
HookClear();
}
}
}
//取消鉤子
public void HookClear()
{
bool rsetKeyboard = true;
if (hHook != 0)
{
rsetKeyboard = Win32API.UnhookWindowsHookEx(hHook);
hHook = 0;
}
if (!rsetKeyboard)
{
throw new Exception("取消鉤子失敗!");
}
}
//對截獲的鍵盤操作的處理
public static int KeyBoardHookProc(int nCode, int wParam, IntPtr lParam)
{
if (nCode >= 0)
{
KeyBoardHookStruct kbh = (KeyBoardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyBoardHookStruct));
if (kbh.vkCode == 91)//截獲左邊WIN鍵
{
return 1;
}
if (kbh.vkCode == 92)//截獲右邊WIN鍵
{
return 1;
}
if (kbh.vkCode == (int)Keys.Escape && (int)Control.ModifierKeys == (int)Keys.Control)//截獲Ctrl+ESC鍵
{
return 1;
}
if (kbh.vkCode == (int)Keys.Escape && (int)Control.ModifierKeys == (int)Keys.Alt)
{
return 1;
}
if (kbh.vkCode == (int)Keys.F4 && (int)Control.ModifierKeys == (int)Keys.Alt)//截獲ALT+F4
{
return 1;
}
if (kbh.vkCode == (int)Keys.Tab && (int)Control.ModifierKeys == (int)Keys.Alt)//截獲ALT+TAB
{
return 1;
}
if (kbh.vkCode == (int)Keys.Delete&&(int)Control.ModifierKeys == (int)Keys.Control + (int)Keys.Alt)
{
return 1;
}
if ( kbh.vkCode == (int) Keys.Escape && (int) Control.ModifierKeys == (int) Keys.Control + (int) Keys.Alt ) /* 截獲Ctrl+Shift+Esc */
{
return 1;
}
}
return Win32API.CallNextHookEx(hHook, nCode, wParam, lParam);
}
#endregion
}
}
3、聲明windows api
//設(shè)置鉤子
[DllImport("user32.dll")]
public static extern int SetWindowsHookEx(int idHook, LockForm.Form1.HookProc lpfn, IntPtr hInstance, int threadID);
//卸載鉤子
[DllImport("user32.dll", CallingConvention = CallingConvention.StdCall)]
public static extern bool UnhookWindowsHookEx(int idHook);
//調(diào)用下一個鉤子
[DllImport("user32.dll")]
public static extern int CallNextHookEx(int idHook, int nCode, int wParam, IntPtr lParam);
PS:
windows api查詢
http://www.pinvoke.net/index.aspx
demo下載
鏈接:http://pan.baidu.com/s/1jGpOvsE 密碼:dbj2
以上就是c# 屏蔽快捷鍵的實現(xiàn)示例的詳細(xì)內(nèi)容,更多關(guān)于c# 屏蔽快捷鍵的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C# ThreadPool之QueueUserWorkItem使用案例詳解
這篇文章主要介紹了C# ThreadPool之QueueUserWorkItem使用案例詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08
C#中使用快速排序按文件創(chuàng)建時間將文件排序的源碼
C#中使用快速排序按文件創(chuàng)建時間將文件排序的源碼...2007-03-03
C#實現(xiàn)Winform鼠標(biāo)拖動窗口大小時設(shè)定窗口最小尺寸的方法
這篇文章主要介紹了C#實現(xiàn)Winform鼠標(biāo)拖動窗口大小時設(shè)定窗口最小尺寸的方法,涉及WinForm改變窗口大小時動態(tài)判斷當(dāng)前窗口尺寸的相關(guān)技巧,非常簡單實用,需要的朋友可以參考下2015-11-11
C#?WPF中RadioButton控件的用法及應(yīng)用場景
在WPF應(yīng)用程序中,RadioButton控件是一種常用的用戶界面元素,本文主要介紹了C#?WPF中RadioButton控件的用法及應(yīng)用場景,具有一定的參考價值,感興趣的可以了解一下2024-03-03
winform開發(fā)使用通用多線程基類分享(以隊列形式)
多線程這個概念大家都很熟悉,對于winform的開發(fā)人員來說,用的還是多的.但估計都是用Timer,或者backgroundWorker,為大家寫了一個多線程的基類,只有你用到多線程拿過來就可以用了2013-12-12

