问题描述
我在网上找到一个例子,自己试了一下,鼠标的移动监听可以实现,点击监听实现不了。具体代码如下:第一个类:publicclassWin32Api{[StructLayout(LayoutKind.Sequential)]publicclassPOINT{publicintx;publicinty;}[StructLayout(LayoutKind.Sequential)]publicclassMouseHookStruct{publicPOINTpt;publicinthwnd;publicintwHitTestCode;publicintdwExtraInfo;}publicdelegateintHookProc(intnCode,IntPtrwParam,IntPtrlParam);//安装钩子[DllImport("user32.dll",CharSet=CharSet.Auto,CallingConvention=CallingConvention.StdCall)]publicstaticexternintSetWindowsHookEx(intidHook,HookProclpfn,IntPtrhInstance,intthreadId);//卸载钩子[DllImport("user32.dll",CharSet=CharSet.Auto,CallingConvention=CallingConvention.StdCall)]publicstaticexternboolUnhookWindowsHookEx(intidHook);//调用下一个钩子[DllImport("user32.dll",CharSet=CharSet.Auto,CallingConvention=CallingConvention.StdCall)]publicstaticexternintCallNextHookEx(intidHook,intnCode,IntPtrwParam,IntPtrlParam);}
第二个类:publicclassMouseHook{privatePointpoint;privatePointPoint{get{returnpoint;}set{if(point!=value){point=value;if(MouseMoveEvent!=null){vare=newMouseEventArgs(MouseButtons.Left,0,point.X,point.Y,0);MouseMoveEvent(this,e);}}}}privateinthHook;publicconstintWH_MOUSE_LL=14;publicWin32Api.HookProchProc;publicMouseHook(){this.Point=newPoint();}publicintSetHook(){hProc=newWin32Api.HookProc(MouseHookProc);hHook=Win32Api.SetWindowsHookEx(WH_MOUSE_LL,hProc,IntPtr.Zero,0);returnhHook;}publicvoidUnHook(){Win32Api.UnhookWindowsHookEx(hHook);}privateintMouseHookProc(intnCode,IntPtrwParam,IntPtrlParam){Win32Api.MouseHookStructMyMouseHookStruct=(Win32Api.MouseHookStruct)Marshal.PtrToStructure(lParam,typeof(Win32Api.MouseHookStruct));if(nCode<0){returnWin32Api.CallNextHookEx(hHook,nCode,wParam,lParam);}else{this.Point=newPoint(MyMouseHookStruct.pt.x,MyMouseHookStruct.pt.y);returnWin32Api.CallNextHookEx(hHook,nCode,wParam,lParam);}}//委托+事件(把钩到的消息封装为事件,由调用者处理)publicdelegatevoidMouseMoveHandler(objectsender,MouseEventArgse);publiceventMouseMoveHandlerMouseMoveEvent;publicdelegatevoidMouseClickHandler(objectsender,MouseEventArgse);publiceventMouseClickHandlerMouseClickEvent;}
操作:publicpartialclassgetLocationForm:Form{publicgetLocationForm(int[]location){InitializeComponent();}MouseHookmh;privatevoidgetLocationForm_Load(objectsender,EventArgse){mh=newMouseHook();mh.SetHook();mh.MouseMoveEvent+=mh_MouseMoveEvent;mh.MouseClickEvent+=mh_MouseClickEvent;}privatevoidmh_MouseClickEvent(objectsender,MouseEventArgse){MessageBox.Show(e.X+"-"+e.Y);}privatevoidmh_MouseMoveEvent(objectsender,MouseEventArgse){intx=e.Location.X;inty=e.Location.Y;textBox1.Text=x+"";textBox2.Text=y+"";}privatevoidForm1_FormClosed(objectsender,FormClosedEventArgse){mh.UnHook();}}
也就是说移动鼠标时能执行privatevoidmh_MouseMoveEvent(objectsender,MouseEventArgse)这个函数里的内容,但鼠标点击时不能执行privatevoidmh_MouseClickEvent(objectsender,MouseEventArgse)这个函数的内容。问题就是怎么改才能实现鼠标点击监听呢?
解决方案
解决方案二:
你在privatePointPoint中只执行了move委托,你在哪执行了click委托啊
解决方案三:
你MouseHook类里对MouseMoveEvent事件处理了,没对MouseClickEvent事件处理
解决方案四:
你应该在hook的回调中判断是否有鼠标按下,然后执行click委托
解决方案五:
提示一下:MouseHookProc(intnCode,IntPtrwParam,IntPtrlParam)中的wParam就是鼠标动作,可以是WM_LBUTTONDOWN,WM_MOUSEMOVE,WM_MOUSEWHEEL等等。如果你志在搬代码而不求甚解,...
解决方案六:
楼上各位,在哪儿加语句啊,怎么加,求赐教啊,楼主是菜鸟
解决方案七:
我发现越是菜鸟,越喜欢研究些高大上的东西,比如IE直接操作客户机,修改其他程序的内存变量,进程通信,截获系统鼠标事件什么的就不敢先从基础开始研究嘛
解决方案八:
hHook=Win32Api.SetWindowsHookEx(WH_MOUSE_LL,hProc,IntPtr.Zero,0);你只写了hook鼠标移动
解决方案九:
错了,是这里Win32Api.MouseHookStructMyMouseHookStruct=(Win32Api.MouseHookStruct)Marshal.PtrToStructure(lParam,typeof(Win32Api.MouseHookStruct));看了一晚上的帖子了···
解决方案十:
你的代码里面,我上面赋值的那句话你就处理了this.Point=newPoint(MyMouseHookStruct.pt.x,MyMouseHookStruct.pt.y);之触发了move的event
解决方案十一:
改了改MouseEventArgs(MouseButtons.None,再加个MouseClickEvent(this,e);就行publicclassMouseHook{privatePointpoint;privatePointPoint{get{returnpoint;}set{if(point!=value){point=value;if(MouseMoveEvent!=null){vare=newMouseEventArgs(MouseButtons.None,0,point.X,point.Y,0);MouseMoveEvent(this,e);}}}}privateinthHook;privateconstintWM_LBUTTONDOWN=0x201;publicconstintWH_MOUSE_LL=14;publicWin32Api.HookProchProc;publicMouseHook(){this.Point=newPoint();}publicintSetHook(){hProc=newWin32Api.HookProc(MouseHookProc);hHook=Win32Api.SetWindowsHookEx(WH_MOUSE_LL,hProc,IntPtr.Zero,0);returnhHook;}publicvoidUnHook(){Win32Api.UnhookWindowsHookEx(hHook);}privateintMouseHookProc(intnCode,IntPtrwParam,IntPtrlParam){Win32Api.MouseHookStructMyMouseHookStruct=(Win32Api.MouseHookStruct)Marshal.PtrToStructure(lParam,typeof(Win32Api.MouseHookStruct));if(nCode<0){returnWin32Api.CallNextHookEx(hHook,nCode,wParam,lParam);}else{if(MouseClickEvent!=null){MouseButtonsbutton=MouseButtons.None;intclickCount=0;switch((Int32)wParam){caseWM_LBUTTONDOWN:button=MouseButtons.Left;clickCount=1;break;}vare=newMouseEventArgs(button,clickCount,point.X,point.Y,0);MouseClickEvent(this,e);}this.Point=newPoint(MyMouseHookStruct.pt.x,MyMouseHookStruct.pt.y);returnWin32Api.CallNextHookEx(hHook,nCode,wParam,lParam);}}publicdelegatevoidMouseMoveHandler(objectsender,MouseEventArgse);publiceventMouseMoveHandlerMouseMoveEvent;publicdelegatevoidMouseClickHandler(objectsender,MouseEventArgse);publiceventMouseClickHandlerMouseClickEvent;}
解决方案十二:
再指定MouseButtons.Leftprivatevoidmh_MouseClickEvent(objectsender,MouseEventArgse){if(e.Button==MouseButtons.Left){MessageBox.Show(e.X+"-"+e.Y);}}
解决方案十三:
MouseClickEvent的方法没写对吧
解决方案十四:
!!!!!!!