问题描述
vb.net如何调用全局鼠标钩子,求一个实例!!!
解决方案
解决方案二:
以前收藏的全局键盘鼠标勾子。太多删了一点,鼠标的保留下来了ImportsSystem.Reflection,System.Threading,System.ComponentModel,System.Runtime.InteropServicesPublicClasshook#Region"定义结构"PrivateStructureMouseHookStructDimPTAsPointDimHwndAsIntegerDimWHitTestCodeAsIntegerDimDwExtraInfoAsIntegerEndStructurePrivateStructureMouseLLHookStructDimPTAsPointDimMouseDataAsIntegerDimFlagsAsIntegerDimTimeAsIntegerDimDwExtraInfoAsIntegerEndStructurePrivateStructureKeyboardHookStructDimvkCodeAsIntegerDimScanCodeAsIntegerDimFlagsAsIntegerDimTimeAsIntegerDimDwExtraInfoAsIntegerEndStructure#EndRegion#Region"API声明导入"PrivateDeclareFunctionSetWindowsHookExALib"user32"(ByValidHookAsInteger,ByVallpfnAsHookProc,ByValhModAsIntPtr,ByValdwThreadIdAsInteger)AsIntegerPrivateDeclareFunctionUnhookWindowsHookExLib"user32"(ByValidHookAsInteger)AsIntegerPrivateDeclareFunctionCallNextHookExLib"user32"(ByValidHookAsInteger,ByValnCodeAsInteger,ByValwParamAsInteger,ByVallParamAsIntPtr)AsIntegerPrivateDeclareFunctionToAsciiLib"user32"(ByValuVirtKeyAsInteger,ByValuScancodeAsInteger,ByVallpdKeyStateAsByte(),ByVallpwTransKeyAsByte(),ByValfuStateAsInteger)AsIntegerPrivateDeclareFunctionGetKeyboardStateLib"user32"(ByValpbKeyStateAsByte())AsIntegerPrivateDeclareFunctionGetKeyStateLib"user32"(ByValvKeyAsInteger)AsShortPrivateDelegateFunctionHookProc(ByValnCodeAsInteger,ByValwParamAsInteger,ByVallParamAsIntPtr)AsInteger#EndRegion#Region"常量声明"PrivateConstWH_MOUSE_LL=14PrivateConstWH_KEYBOARD_LL=13PrivateConstWH_MOUSE=7PrivateConstWH_KEYBOARD=2PrivateConstWM_MOUSEMOVE=&H200PrivateConstWM_LBUTTONDOWN=&H201PrivateConstWM_RBUTTONDOWN=&H204PrivateConstWM_MBUTTONDOWN=&H207PrivateConstWM_LBUTTONUP=&H202PrivateConstWM_RBUTTONUP=&H205PrivateConstWM_MBUTTONUP=&H208PrivateConstWM_LBUTTONDBLCLK=&H203PrivateConstWM_RBUTTONDBLCLK=&H206PrivateConstWM_MBUTTONDBLCLK=&H209PrivateConstWM_MOUSEWHEEL=&H20APrivateConstWM_KEYDOWN=&H100PrivateConstWM_KEYUP=&H101PrivateConstWM_SYSKEYDOWN=&H104PrivateConstWM_SYSKEYUP=&H105PrivateConstVK_SHIFTAsByte=&H10PrivateConstVK_CAPITALAsByte=&H14PrivateConstVK_NUMLOCKAsByte=&H90#EndRegion'''<summary>鼠标激活事件</summary>PublicEventMouseActivityAsMouseEventHandler'''<summary>键盘按下事件</summary>PublicEventKeyDownAsKeyEventHandler'''<summary>键盘输入事件</summary>PublicEventKeyPressAsKeyPressEventHandler'''<summary>键盘松开事件</summary>PublicEventKeyUpAsKeyEventHandlerPrivatehMouseHookAsIntegerPrivatehKeyboardHookAsIntegerPrivateSharedMouseHookProcedureAsHookProcPrivateSharedKeyboardHookProcedureAsHookProc'''<summary>创建一个全局鼠标键盘钩子(请使用Start方法开始监视)</summary>SubNew()'留空即可EndSub'''<summary>创建一个全局鼠标键盘钩子,决定是否安装钩子</summary>'''<paramname="InstallAll">是否立刻挂钩系统消息</param>SubNew(ByValInstallAllAsBoolean)IfInstallAllThenStartHook(True,True)EndSub'''<summary>创建一个全局鼠标键盘钩子,并决定安装钩子的类型</summary>'''<paramname="InstallKeyboard">挂钩键盘消息</param>'''<paramname="InstallMouse">挂钩鼠标消息</param>SubNew(ByValInstallKeyboardAsBoolean,ByValInstallMouseAsBoolean)StartHook(InstallKeyboard,InstallMouse)EndSub'''<summary>析构函数</summary>ProtectedOverridesSubFinalize()UnHook()'卸载对象时反注册系统钩子MyBase.Finalize()EndSub'''<summary>开始安装系统钩子</summary>'''<paramname="InstallKeyboardHook">挂钩键盘消息</param>'''<paramname="InstallMouseHook">挂钩鼠标消息</param>PublicSubStartHook(OptionalByValInstallKeyboardHookAsBoolean=True,OptionalByValInstallMouseHookAsBoolean=False)'注册键盘钩子IfInstallKeyboardHookAndAlsohKeyboardHook=0ThenKeyboardHookProcedure=NewHookProc(AddressOfKeyboardHookProc)hKeyboardHook=SetWindowsHookExA(WH_KEYBOARD_LL,KeyboardHookProcedure,Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly.GetModules()(0)),0)IfhKeyboardHook=0Then'检测是否注册完成UnHook(True,False)'在这里反注册ThrowNewWin32Exception(Marshal.GetLastWin32Error)'报告错误EndIfEndIf'注册鼠标钩子IfInstallMouseHookAndAlsohMouseHook=0ThenMouseHookProcedure=NewHookProc(AddressOfMouseHookProc)hMouseHook=SetWindowsHookExA(WH_MOUSE_LL,MouseHookProcedure,Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly.GetModules()(0)),0)IfhMouseHook=0ThenUnHook(False,True)ThrowNewWin32Exception(Marshal.GetLastWin32Error)EndIfEndIfEndSub'''<summary>立刻卸载系统钩子</summary>'''<paramname="UninstallKeyboardHook">卸载键盘钩子</param>'''<paramname="UninstallMouseHook">卸载鼠标钩子</param>'''<paramname="ThrowExceptions">是否报告错误</param>PublicSubUnHook(OptionalByValUninstallKeyboardHookAsBoolean=True,OptionalByValUninstallMouseHookAsBoolean=True,OptionalByValThrowExceptionsAsBoolean=False)'卸载键盘钩子IfhKeyboardHook<>0AndAlsoUninstallKeyboardHookThenDimretKeyboardAsInteger=UnhookWindowsHookEx(hKeyboardHook)hKeyboardHook=0IfThrowExceptionsAndAlsoretKeyboard=0Then'如果出现错误,是否报告错误ThrowNewWin32Exception(Marshal.GetLastWin32Error)'报告错误EndIfEndIf'卸载鼠标钩子IfhMouseHook<>0AndAlsoUninstallMouseHookThenDimretMouseAsInteger=UnhookWindowsHookEx(hMouseHook)hMouseHook=0IfThrowExceptionsAndAlsoretMouse=0ThenThrowNewWin32Exception(Marshal.GetLastWin32Error)EndIfEndIfEndSub'鼠标消息的委托处理代码PrivateFunctionMouseHookProc(ByValnCodeAsInteger,ByValwParamAsInteger,ByVallParamAsIntPtr)AsIntegerIfnCode>=0ThenDimmouseHookStructAsMouseLLHookStruct=DirectCast(Marshal.PtrToStructure(lParam,GetType(MouseLLHookStruct)),MouseLLHookStruct)DimmoubutAsMouseButtons=MouseButtons.None'鼠标按键DimmouseDeltaAsInteger=0'滚轮值SelectCasewParamCaseWM_LBUTTONDOWNmoubut=MouseButtons.LeftCaseWM_RBUTTONDOWNmoubut=MouseButtons.RightCaseWM_MBUTTONDOWNmoubut=MouseButtons.MiddleCaseWM_MOUSEWHEELDimintAsInteger=(mouseHookStruct.MouseData>>16)And&HFFFF'本段代码CLE添加,模仿C#的Short从Int弃位转换Ifint>Short.MaxValueThenmouseDelta=int-65536ElsemouseDelta=intEndSelectDimclickCountAsInteger=0'单击次数Ifmoubut<>MouseButtons.NoneThenIfwParam=WM_LBUTTONDBLCLKOrElsewParam=WM_RBUTTONDBLCLKOrElsewParam=WM_MBUTTONDBLCLKThenclickCount=2ElseclickCount=1EndIfEndIfDimeAsNewMouseEventArgs(moubut,clickCount,mouseHookStruct.PT.X,mouseHookStruct.PT.Y,mouseDelta)RaiseEventMouseActivity(Me,e)EndIfReturnCallNextHookEx(hMouseHook,nCode,wParam,lParam)'激活下一个钩子EndFunction'''<summary>键盘钩子是否有效</summary>PublicPropertyKeyHookEnabled()AsBooleanGetReturnhKeyboardHook<>0EndGetSet(ByValvalueAsBoolean)IfvalueThenStartHook(True,False)ElseUnHook(True,False)EndSetEndProperty'''<summary>鼠标钩子是否有效</summary>PublicPropertyMouseHookEnabled()AsBooleanGetReturnhMouseHook<>0EndGetSet(ByValvalueAsBoolean)IfvalueThenStartHook(False,True)ElseUnHook(False,True)EndSetEndPropertyEndClass