鼠标WM_LBUTTONDOWN时GetCapture为空?
If wParam = WM_LBUTTONDOWN Then
Debug.Print "Cap:" & GetCapture
Print结果始终为0,得不到焦点控件的句柄,而WM_LBUTTONUP时则可以 --------------------编程问答-------------------- 不知所云 --------------------编程问答-------------------- If wParam = WM_LBUTTONDOWN Then
Debug.Print "Cap:" & GetFocus --------------------编程问答-------------------- 这是鼠标全局hook的一行代码,在截获鼠标信息后的判断的一句话,判断左键按下(wParam = WM_LBUTTONDOWN )并判断某个控件是否获得焦点(GetCapture)
GetCapture()总是返回Null
如果是判断左键抬起wParam = WM_LBUTTONUP )则GetCapture()能返回正确的控件句柄 --------------------编程问答-------------------- 应该不行,单击窗体时其实已经触发了GetCapture
你试一试捕获单击消息是调用GetCapture,应该可以。 --------------------编程问答-------------------- 还有这么回事,见识了。 --------------------编程问答--------------------
捕获单击消息?
那不还是用WM_LBUTTONDOWN 或WM_LBUTTONUP啊? --------------------编程问答-------------------- 贴代码吧,网上找了好多VB做的鼠标全局hook的代码
--------------------编程问答--------------------
'模块部分
Option Explicit
Private Declare Function GetCapture Lib "user32" () As Long
Private Declare Sub CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory" (ByRef Destination As Any, ByRef Source As Any, ByVal Length As Long)
Private 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
Private Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long
Private Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal nCode As Long, ByVal wParam As Long, lparam As Any) As Long
Private Const HC_ACTION = 0
Private Const WH_MOUSE_LL As Long = 14
Private Const WM_MOUSEMOVE = &H200
Private Const WM_LBUTTONDOWN = &H201
Private Const WM_LBUTTONUP = &H202
Private Const WM_LBUTTONDBLCLK = &H203
Private Const WM_RBUTTONDOWN = &H204
Private Const WM_RBUTTONUP = &H205
Private Const WM_RBUTTONDBLCLK = &H206
Private Const WM_MBUTTONDOWN = &H207
Private Const WM_MBUTTONUP = &H208
Private Const WM_MBUTTONDBLCLK = &H209
Private Const WM_MOUSEACTIVATE = &H21
Private Const WM_MOUSEFIRST = &H200
Private Const WM_MOUSELAST = &H209
Private Const WM_MOUSEWHEEL = &H20A '以上是鼠标的各个值
Private Type POINTAPI
x As Long
y As Long
End Type
Private Type MSLLHOOKSTRUCT
pt As POINTAPI
mouseData As Long
Flags As Long
time As Long
dwExtraInfo As Long
End Type
Public hHook As Long
Public Sub EnableHook()
If hHook = 0 Then
hHook = SetWindowsHookEx(WH_MOUSE_LL, AddressOf HookProc, App.hInstance, 0)
End If
End Sub
Public Sub FreeHook()
If hHook <> 0 Then
Call UnhookWindowsHookEx(hHook)
hHook = 0
End If
End Sub
Public Function HookProc(ByVal nCode As Long, ByVal wParam As Long, ByVal lparam As Long) As Long
Dim typMHS As MSLLHOOKSTRUCT, pt As POINTAPI
If wParam = WM_MOUSEMOVE Then
Call CopyMemory(typMHS, ByVal lparam, LenB(typMHS))
pt = typMHS.pt
Debug.Print "mouse Cursor at " + CStr(pt.x) + "," + CStr(pt.y)
End If
Debug.Print "----------------------------------"
Debug.Print Hex(wParam)
If wParam = WM_LBUTTONUP Then
If GetCapture=FindWindow(...) Then _'这里GetCapture能正确返回
Debug.Print "u"
End If
If wParam = WM_LBUTTONDOWN Then
If GetCapture=FindWindow(...) Then _'这里GetCapture总是返回null
Debug.Print "d"
End If
HookProc = CallNextHookEx(hHook, nCode, wParam, lparam)
End Function
是的 --------------------编程问答--------------------
看我前面贴出的代码
WM_LBUTTONDOWN 不行 WM_LBUTTONUP可以
补充:VB , API