|
- Option Explicit
- Public Const WM_MOUSEMOVE = &H200
- Public Const WH_MOUSE = 7
- Type POINTAPI
- X As Long
- Y As Long
- End Type
- Type MOUSEHOOKSTRUCT
- pt As POINTAPI
- hwnd As Long
- wHitTestCode As Long
- dwExtraInfo As Long
- End Type
- Type RECT
- Left As Long
- Top As Long
- Right As Long
- Bottom As Long
- End Type
- Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Long, ByVal lpfn As Long, ByVal hmod As Long, ByVal dwThreadId As Long) As Long
- Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal nCode As Long, ByVal wParam As Long, lParam As Any) As Long
- Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long
- Declare 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 Long
- Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
- Private mhHook As Long
- Private mctlRect As RECT
- Public gfrmMouseMove As Form
- Public gctlMouseMove As Control
- Function g_MouseMoveEvent(ctl As Control, frm As Form) As Boolean
- Dim pid As Long
- If mhHook = 0 Then
- Set gctlMouseMove = ctl
- Set gfrmMouseMove = frm
- With mctlRect
- .Top = ctl.Top
- .Left = ctl.Left
- .Right = .Left + ctl.Width
- .Bottom = .Top + ctl.Height
- End With
- '问题就在下面这一行中,hMod参数是应用程序实例的句柄,用Application.hWndAccessApp无效,要怎么弄?
- mhHook = SetWindowsHookEx(WH_MOUSE, AddressOf HookProc, hmod:=Application.hWndAccessApp, _
- dwThreadId:=GetWindowThreadProcessId(gfrmMouseMove.hwnd, pid))
- g_MouseMoveEvent = True
- End If
- End Function
- Function HookProc(ByVal code As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
- Dim msu As MOUSEHOOKSTRUCT
- Dim blnMoveIn As Boolean
- Dim blnOnControl As Boolean
- Static i As Integer
- If wParam <> WM_MOUSEMOVE Then Exit Function
- CopyMemory msu, lParam, LenB(msu)
- Call ScreenToClient(gfrmMouseMove.hwnd, msu.pt)
- blnOnControl = (msu.pt.X * 15 >= mctlRect.Left And msu.pt.X * 15 <= mctlRect.Right _
- And msu.pt.Y * 15 >= mctlRect.Top And msu.pt.Y * 15 <= mctlRect.Bottom)
- If blnOnControl = False Then
- HookProc = 0
- CallNextHookEx mhHook, code, wParam, lParam
- If mhHook <> 0 Then
- Call UnhookWindowsHookEx(mhHook)
- mhHook = 0
- End If
- Call g_MouseOut(gctlMouseMove)
- Else
- If blnMoveIn = False Then
- Call g_MouseMove(gctlMouseMove)
- blnMoveIn = True
- End If
- End If
- End Function
- Sub g_MouseMove(ctl As Control)
- ctl.BackColor = vbRed
- End Sub
- Sub g_MouseOut(ctl As Control)
- ctl.BackColor = vbWhite
- End Sub
复制代码 |
|