c# 屏蔽快捷鍵的實(shí)現(xiàn)示例
前言
有時(shí)候開發(fā)會遇到這樣一個(gè)需求,軟件需要屏蔽用戶的組合快捷鍵或某些按鍵,避免強(qiáng)制退出軟件,防止勿操作等。
原理
1、要實(shí)現(xiàn)組合鍵,按鍵攔截,需要用到user32.dll中的SetWindowsHookEx。
2、要攔截ctrl+alt+del,需要使用ntdll.dll的ZwSuspendProcess函數(shù)掛起winlogon程序,退出之后使用ZwResumeProcess恢復(fù)winlogon程序。
3、軟件需要開啟topMost,以及全屏,否則離開軟件則攔截?zé)o效。
4、如果要實(shí)現(xiàn)熱鍵監(jiān)聽(非焦點(diǎn)攔截),則需要用到user32.dll的RegisterHotKey以及UnregisterHotKey。
實(shí)現(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)用程序的主入口點(diǎn)。 /// </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) { //實(shí)例化一個(gè)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)用下一個(gè)鉤子 [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# 屏蔽快捷鍵的實(shí)現(xiàn)示例的詳細(xì)內(nèi)容,更多關(guān)于c# 屏蔽快捷鍵的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C#解析char型指針?biāo)赶虻膬?nèi)容(實(shí)例解析)
在c++代碼中定義了一個(gè)功能函數(shù),這個(gè)功能函數(shù)會將計(jì)算的結(jié)果寫入一個(gè)字符串型的數(shù)組中output,然后c#會調(diào)用c++導(dǎo)出的dll中的接口函數(shù),然后獲取這個(gè)output并解析成string類型,本文通過實(shí)例解析C#?char型指針?biāo)赶虻膬?nèi)容,感興趣的朋友一起看看吧2024-03-03C# 利用ICSharpCode.SharpZipLib實(shí)現(xiàn)在線壓縮和解壓縮
本文主要主要介紹了利用ICSharpCode.SharpZipLib第三方的DLL庫實(shí)現(xiàn)在線壓縮和解壓縮的功能,并做了相關(guān)的代碼演示。2016-04-04C# 實(shí)現(xiàn)ADSL自動斷網(wǎng)和撥號的方法(適用于撥號用戶)
下面小編就為大家?guī)硪黄狢# 實(shí)現(xiàn)ADSL自動斷網(wǎng)和撥號的方法(適用于撥號用戶)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-12-12詳解C# ConcurrentBag的實(shí)現(xiàn)原理
ConcurrentBag<T>實(shí)現(xiàn)了IProducerConsumerCollection<T>接口,該接口主要用于生產(chǎn)者消費(fèi)者模式下,可見該類基本就是為生產(chǎn)消費(fèi)者模式定制的。然后還實(shí)現(xiàn)了常規(guī)的IReadOnlyCollection<T>類,實(shí)現(xiàn)了該類就需要實(shí)現(xiàn)IEnumerable<T>、IEnumerable、 ICollection類2021-06-06