C#什么是钩子,关于钩子的使用

 我来答
蜗牛P科技
高粉答主

2017-01-01 · 图灵的梦想,信息的时代
蜗牛P科技
采纳数:68329 获赞数:214555

向TA提问 私信TA
展开全部
c#钩子屏蔽键盘快捷键usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;namespaceHook_Demo{publicpartialclassForm1:Form{Win32Hook.Hookhook;publicForm1(){InitializeComponent();this.FormBorderStyle=FormBorderStyle.None;this.WindowState=FormWindowState.Maximized;this.TopMost=true;hook=newWin32Hook.Hook();hook.Start();this.FormClosing+=newFormClosingEventHandler(Form1_FormClosing);this.buttonExit.Click+=newEventHandler(buttonExit_Click);}voidForm1_FormClosing(objectsender,FormClosingEventArgse){hook.Close();}privatevoidbuttonExit_Click(objectsender,EventArgse){Application.Exit();}}}cs*调用:*Hookhook=newHook();*hook.Start();//安装钩子*hook.Close();//卸载钩子*如果需要屏蔽键盘,请在KeyBoardHookProc方法中添加处理**时间:2010/7/2715:56:05**备注:调用此类必须已管理员身份运行,否则没有权限写入注册表*判断是否是管理员代码:(isAdministrator如果为true为系统管理员,false不是管理员)*AppDomainmyDomain=Thread.GetDomain();*myDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);*WindowsPrincipalmyPrincipal=(WindowsPrincipal)Thread.CurrentPrincipal;*boolisAdministrator=myPrincipal.IsInRole(WindowsBuiltInRole.Administrator);*/usingSystem;usingSystem.Collections.Generic;usingSystem.Text;usingSystem.Runtime.InteropServices;usingSystem.Diagnostics;usingSystem.Windows.Forms;usingSystem.IO;usingMicrosoft.Win32;namespaceWin32Hook{publicclassHook:IDisposable{publicdelegateintHookProc(intnCode,intwParam,IntPtrlParam);staticinthHook=0;publicconstintWH_KEYBOARD_LL=13;HookProcKeyBoardHookProcedure;[StructLayout(LayoutKind.Sequential)]publicclassKeyBoardHookStruct{publicintvkCode;publicintscanCode;publicintflags;publicinttime;publicintdwExtraInfo;}[DllImport("user32.dll")]publicstaticexternintSetWindowsHookEx(intidHook,HookProclpfn,IntPtrhInstance,intthreadId);[DllImport("user32.dll",CharSet=CharSet.Auto,CallingConvention=CallingConvention.StdCall)]publicstaticexternboolUnhookWindowsHookEx(intidHook);[DllImport("user32.dll")]publicstaticexternintCallNextHookEx(intidHook,intnCode,intwParam,IntPtrlParam);[DllImport("kernel32.dll")]publicstaticexternIntPtrGetModuleHandle(stringname);publicvoidStart(){//安装键盘钩子if(hHook==0){KeyBoardHookProcedure=newHookProc(KeyBoardHookProc);hHook=SetWindowsHookEx(WH_KEYBOARD_LL,KeyBoardHookProcedure,GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName),0);//如果设置钩子失败.if(hHook==0)Close();else{RegistryKeykey=Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Policies\System",true);if(key==null)//如果该项不存在的话,则创建该项key=Registry.CurrentUser.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Policies\System");key.SetValue("DisableTaskMgr",1,RegistryValueKind.DWord);key.Close();}}}publicvoidClose(){boolretKeyboard=true;if(hHook!=0){retKeyboard=UnhookWindowsHookEx(hHook);hHook=0;}//如果去掉钩子失败.//if(!retKeyboard)thrownewException("UnhookWindowsHookExfailed.");RegistryKeykey=Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Policies\System",true);if(key!=null){key.DeleteValue("DisableTaskMgr",false);key.Close();}}publicstaticintKeyBoardHookProc(intnCode,intwParam,IntPtrlParam){if(nCode>=0){KeyBoardHookStructkbh=(KeyBoardHookStruct)Marshal.PtrToStructure(lParam,typeof(KeyBoardHookStruct));if(kbh.vkCode==91)//截获左win(开始菜单键)return1;if(kbh.vkCode==92)//截获右winreturn1;if(kbh.vkCode==(int)Keys.Escape&&(int)Control.ModifierKeys==(int)Keys.Control)//截获Ctrl+Escreturn1;if(kbh.vkCode==(int)Keys.F4&&(int)Control.ModifierKeys==(int)Keys.Alt)//截获alt+f4return1;if(kbh.vkCode==(int)Keys.Tab&&(int)Control.ModifierKeys==(int)Keys.Alt)//截获alt+tabreturn1;if(kbh.vkCode==(int)Keys.Escape&&(int)Control.ModifierKeys==(int)Keys.Control+(int)Keys.Shift)//截获Ctrl+Shift+Escreturn1;if(kbh.vkCode==(int)Keys.Space&&(int)Control.ModifierKeys==(int)Keys.Alt)//截获alt+空格return1;if(kbh.vkCode==241)//截获F1return1;if(kbh.vkCode==(int)Keys.Control&&kbh.vkCode==(int)Keys.Alt&&kbh.vkCode==(int)Keys.Delete)return1;if((int)Control.ModifierKeys==(int)Keys.Control+(int)Keys.Alt+(int)Keys.Delete)//截获Ctrl+Alt+Deletereturn1;if((int)Control.ModifierKeys==(int)Keys.Control+(int)Keys.Shift)//截获Ctrl+Shiftreturn1;}returnCallNextHookEx(hHook,nCode,wParam,lParam);}#regionIDisposable成员publicvoidDispose(){Close();}#endregion}}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式