Office中国论坛/Access中国论坛

标题: [求助]捕捉鼠标移出事件遇到的钩子问题 [打印本页]

作者: 红尘如烟    时间: 2009-4-11 13:29
标题: [求助]捕捉鼠标移出事件遇到的钩子问题
  1. Option Explicit
  2. Public Const WM_MOUSEMOVE = &H200
  3. Public Const WH_MOUSE = 7
  4. Type POINTAPI
  5. X As Long
  6. Y As Long
  7. End Type
  8. Type MOUSEHOOKSTRUCT
  9. pt As POINTAPI
  10. hwnd As Long
  11. wHitTestCode As Long
  12. dwExtraInfo As Long
  13. End Type
  14. Type RECT
  15. Left As Long
  16. Top As Long
  17. Right As Long
  18. Bottom As Long
  19. End Type
  20. 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
  21. Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal nCode As Long, ByVal wParam As Long, lParam As Any) As Long
  22. Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long
  23. Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (lpvDest As Any, ByVal lpvSource As Long, ByVal cbCopy As Long)
  24. Declare Function ScreenToClient Lib "user32" (ByVal hwnd As Long, lpPoint As POINTAPI) As Long
  25. Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
  26. Private mhHook As Long
  27. Private mctlRect As RECT

  28. Public gfrmMouseMove As Form
  29. Public gctlMouseMove As Control

  30. Function g_MouseMoveEvent(ctl As Control, frm As Form) As Boolean
  31. Dim pid As Long
  32. If mhHook = 0 Then
  33. Set gctlMouseMove = ctl
  34. Set gfrmMouseMove = frm
  35. With mctlRect
  36. .Top = ctl.Top
  37. .Left = ctl.Left
  38. .Right = .Left + ctl.Width
  39. .Bottom = .Top + ctl.Height
  40. End With
  41. '问题就在下面这一行中,hMod参数是应用程序实例的句柄,用Application.hWndAccessApp无效,要怎么弄?
  42. mhHook = SetWindowsHookEx(WH_MOUSE, AddressOf HookProc, hmod:=Application.hWndAccessApp, _
  43. dwThreadId:=GetWindowThreadProcessId(gfrmMouseMove.hwnd, pid))
  44. g_MouseMoveEvent = True
  45. End If
  46. End Function

  47. Function HookProc(ByVal code As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
  48. Dim msu As MOUSEHOOKSTRUCT
  49. Dim blnMoveIn As Boolean
  50. Dim blnOnControl As Boolean
  51. Static i As Integer
  52. If wParam <> WM_MOUSEMOVE Then Exit Function
  53. CopyMemory msu, lParam, LenB(msu)
  54. Call ScreenToClient(gfrmMouseMove.hwnd, msu.pt)
  55. blnOnControl = (msu.pt.X * 15 >= mctlRect.Left And msu.pt.X * 15 <= mctlRect.Right _
  56. And msu.pt.Y * 15 >= mctlRect.Top And msu.pt.Y * 15 <= mctlRect.Bottom)
  57. If blnOnControl = False Then
  58. HookProc = 0
  59. CallNextHookEx mhHook, code, wParam, lParam
  60. If mhHook <> 0 Then
  61. Call UnhookWindowsHookEx(mhHook)
  62. mhHook = 0
  63. End If
  64. Call g_MouseOut(gctlMouseMove)
  65. Else
  66. If blnMoveIn = False Then
  67. Call g_MouseMove(gctlMouseMove)
  68. blnMoveIn = True
  69. End If
  70. End If
  71. End Function

  72. Sub g_MouseMove(ctl As Control)
  73. ctl.BackColor = vbRed
  74. End Sub

  75. Sub g_MouseOut(ctl As Control)
  76. ctl.BackColor = vbWhite
  77. End Sub





复制代码

作者: t小宝    时间: 2009-4-11 13:46
把 hMod参数 设为 0 试试
作者: liwen    时间: 2009-4-11 15:17
本帖最后由 liwen 于 2009-4-11 15:23 编辑

试试
Public Declare Function GetWindowLong Lib "user32" Alias _
         "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) _
         As Long

Public Const GWL_HINSTANCE = (-6)
hmod=GetWindowLong(Forms!窗体A.hwnd, (-6))
作者: andymark    时间: 2009-4-11 16:05
试试下面的代码
Dim hInst As Long
  Dim Thread As Long
  hInst = GetModuleHandle(vbNullString)
  Thread = GetCurrentThreadId()         
  
mhHook = SetWindowsHookEx(WH_MOUSE, AddressOf HookProc, hInst, Thread)
作者: andymark    时间: 2009-4-11 16:07
在调用前必须加上GetModuleHandle GetCurrentThreadId API 的声明




欢迎光临 Office中国论坛/Access中国论坛 (http://www.office-cn.net/) Powered by Discuz! X3.3