首页 >> 读书频道 >> 电脑 >> 如何得知Mouse已离开某物件(Mouse Hook)
 
· 网络数据库教程六
· TextBox模拟拖曳选
· 网络数据库教程五
· 如何在开始功能表的程式集
· 网络数据库教程四
· 设定Caret的大小与其
· 如何Restart Sh
· 于PictureBox/
· 如何用VB建立捷径(Sh
· 让一组相同的控制项有相同
· 网络数据库教程三
· 改变ComboBox中L
 
· 电视剧《刁蛮公主》下载和
· 美容养颜手册
· 加油金顺剧情分集介绍完整
· 红楼梦:120回全本[清
· 素质教育在美国
· 全唐诗卷四十六
· 韩剧《布拉格恋人》剧情介
· 夜航船[作者:明·张岱]
· 局外人[作者:韩·可爱淘
· 现场流行病学
· 源氏物语[日本:紫式部]
· 武林寓言故事
 
· (出租)中动商场部分及写
· (出租)中动动漫基地&#
· 喜剧学院
· 《善德女王》剧情介绍
· 魔女18号 剧情
· 丑女无敌剧情介绍
· 魔女幼熙剧情介绍
· 龙游天下剧情介绍
· 震撼世界的七日剧情介绍
· 静静的白桦林剧情介绍
· 心情日记—老公今天我想对
· 旗舰剧情介绍
欢迎来到月影社区!如果您觉得这里不错,请推荐给您的朋友们。月影社区:http://wf66.com/

如何得知Mouse已离开某物件(Mouse Hook)


查看有无更新版本

关键字:VB 2006-9-18

 

常见到某些软体,当Mouse进入其区域时,会启动某个行为,Mouse离开时,又有其他的
动作,例如Cool Bar,当Mouse移入时,Button会上升,离开时Button水变平面。

第一个想到的是在物件的MouseMove中设定进入的行为,这没有问题,但离开呢?有几
个想法:1.如果该物件在Form上,可以在Form的MouseMove上作离开的动作。2.於该物
件的MouseMove上Check是否Mouse的座标已在物件的边缘,若是则执行离开的动作。
但这两者,都会遇上一个问题,如果Mouse的移动很快,使得MouseMove的Event根本没
有在该物件或Form上面发生,那就不可行了;所以看来简单的问题又变复杂了,那只好
使用Mouse Hook来做。

Mouse Hook是拦截硬体所产生Mouse硬体的讯息,不管Mouse现在於何处,都会将Mouse的
讯息送往Hook Procedure,当然,一般情况下,是於该程式正处於Active的情况下
(Local Hook),讯息才会送往该Hook Procedure,如果别的程式所产生的Mouse讯息也要
进入该Hook Function时,那便得使用Remote Hook,而Remote Hook的方式,是要把Hook
Procedure放在.Dll之中,而Local Hook只要把 Hook Procedure放在.Bas之中便可以了。

因挂上了Mouse Hook(Local),所以该程式执行时所有的Mouse 的讯息便会送往该Hook
Function,而且有包含Mouse所在的座标(相对於Screen),於是我们可以Check Mouse
的座标,进而得知Mouse是否仍在物件范围。

Please Reference : 如何得知Mouse已离开某物件(二)
'以下在.BasOption ExplicitPublic Const WM_MOUSEMOVE = &H200Public Const WH_MOUSE = 7Type POINTAPI X As Long Y As LongEnd TypeType MOUSEHOOKSTRUCT pt As POINTAPI hwnd As Long wHitTestCode As Long dwExtraInfo As LongEnd TypeType RECT Left As Long Top As Long Right As Long Bottom As LongEnd TypeDeclare Function SetWindowsHookEx Lib "user32" Alias _ "SetWindowsHookExA" (ByVal idHook As Long, ByVal lpfn As Long, _ ByVal hmod As Long, ByVal dwThreadId As Long) As LongDeclare Function UnhookWindowsHookEx Lib "user32" _ (ByVal hHook As Long) As LongDeclare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, _ ByVal nCode As Long, ByVal wParam As Long, lParam As Any) As LongDeclare Sub CopyMemory Lib "KERNEL32" Alias "RtlMoveMemory" _ (lpvDest As Any, ByVal lpvSource As Long, ByVal cbCopy As Long)Declare Function ScreenToClient Lib "user32" (ByVal hwnd As Long, lpPoint As POINTAPI) As LongPublic theForm As FormPublic hHook As Long ' handle of Hook ProcedurePublic imgRect As RECTSub EnableHook(ctl As Control) If hHook = 0 Then imgRect.Top = ctl.Top imgRect.Left = ctl.Left imgRect.Right = imgRect.Left + ctl.Width imgRect.Bottom = imgRect.Top + ctl.Height hHook = SetWindowsHookEx(WH_MOUSE, AddressOf MouseHookProc, App.hInstance, App.ThreadID) End IfEnd SubSub FreeHook()Dim ret As LongIf hHook <> 0 Then ret = UnhookWindowsHookEx(hHook) hHook = 0End IfEnd SubFunction MouseHookProc(ByVal code As Long, ByVal wParam As Long, _ ByVal lParam As Long) As Long Dim mStru As MOUSEHOOKSTRUCT, i As Long If wParam = WM_MOUSEMOVE Then CopyMemory mStru, lParam, LenB(mStru) 'mStru.pt的座标是萤幕座标,所以要经转换成相对於Form的座标 Call ScreenToClient(Screen.ActiveForm.hwnd, mStru.pt) '不在imgButton之内 If Not (mStru.pt.Y >= imgRect.Top And mStru.pt.Y <= imgRect.Bottom And _ mStru.pt.X >= imgRect.Left And mStru.pt.X <= imgRect.Right) Then MouseHookProc = 0 Call CallNextHookEx(hHook, code, wParam, lParam) Call FreeHook Debug.Print "Out of The Range " Exit Function Else Debug.Print "In The Range" End If End If MouseHookProc = 0 '表示要处理这个讯息 Call CallNextHookEx(hHook, code, wParam, lParam)End Function'以下在Form,需一个Command1Private Sub Command1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)Call EnableHook(Command1)End SubPrivate Sub Form_Load()Me.ScaleMode = 3End Sub

如何得知Mouse已离开某物件(Mouse Hook)

[ 1 ]
如何得知Mouse已离开某物件(Mouse Hook) num

打印本页 关闭

关于我们版权声明本站导航友情连结作品演示 TOP↑