后台程序全局钩子获取鼠标滚轮滚动方向

泊客天涯 2009-08-25 11:39:15
后台程序用以下代码成功勾到滚轮滚动事件,但是无法得到滚动方向,求助!
Form1 :


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 Const WH_MOUSE_LL As Long = 14


Private Sub Form_Load()
hHook = SetWindowsHookEx(WH_MOUSE_LL, AddressOf HookProc, App.hInstance, 0)
End Sub

Private Sub Form_Unload(Cancel As Integer)
Call UnhookWindowsHookEx(hHook)
End Sub


Module1:

Public Const WM_MOUSEMOVE = &H200
Public Const WM_LBUTTONDOWN = &H201
Public Const WM_LBUTTONUP = &H202
Public Const WM_LBUTTONDBLCLK = &H203
Public Const WM_RBUTTONDOWN = &H204
Public Const WM_RBUTTONUP = &H205
Public Const WM_RBUTTONDBLCLK = &H206
Public Const WM_MBUTTONDOWN = &H207
Public Const WM_MBUTTONUP = &H208
Public Const WM_MBUTTONDBLCLK = &H209
Public Const WM_MOUSEACTIVATE = &H21
Public Const WM_MOUSEFIRST = &H200
Public Const WM_MOUSELAST = &H209
Public Const WM_MOUSEWHEEL = &H20A '以上是鼠标的各个值

Private Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal nCode As Long, ByVal wParam As Long, lParam As Any) As Long


Public Function HookProc(ByVal nCode As Long, ByVal wParam As Long, ByVal lParam As Long) As Long

If nCode < 0 Then
HookProc = CallNextHookEx(hHook, nCode, wParam, lParam)
Exit Function
End If
If wParam = WM_MOUSEWHEEL Then


MsgBox "滚动"




End If
End Function
网上很多都是线程钩子,窗体激活时才有效,我的是后台获取,

如何得到滚动方向?
...全文
1070 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
嗷嗷叫的老马 2010-09-27
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 lingll 的回复:]
不知道有没有试过,有些鼠标驱动会导致捕获不到滚轮消息,例如双飞燕的
[/Quote]
那个是他们的驱动在搞鬼,收到滚轮动作后,不发滚轮消息,而是发送了PageUp/PageDown按键,有的还是发送上下左右方向键...
lingll 2010-09-27
  • 打赏
  • 举报
回复
不知道有没有试过,有些鼠标驱动会导致捕获不到滚轮消息,例如双飞燕的
嗷嗷叫的老马 2010-09-27
  • 打赏
  • 举报
回复
把11楼看完.
wgc3306 2009-09-04
  • 打赏
  • 举报
回复
全局钩子始终无法获得滚轮方向,请高手赐教
wgc3306 2009-09-01
  • 打赏
  • 举报
回复
如何解决的,能给源码吗
泊客天涯 2009-08-27
  • 打赏
  • 举报
回复
解决了 谢谢马哥
嗷嗷叫的老马 2009-08-27
  • 打赏
  • 举报
回复
而且,有关WH_MOUSE_LL的用法,MSDN都说得非常非常非常之清楚了:
[Quote=引用 MSDN:]
LowLevelMouseProc
The LowLevelMouseProc hook procedure is an application-defined callback function used with the SetWindowsHookEx function. The system call this function every time a new mouse input event is about to be posted into a thread input queue. The mouse input can come from the local mouse driver or from calls to themouse_event function. If the input comes from a call to mouse_event, the input was "injected".

The HOOKPROC type defines a pointer to this callback function. LowLevelMouseProc is a placeholder for the application-defined or library-defined function name.

LRESULT CALLBACK LowLevelMouseProc(
int nCode, // hook code
WPARAM wParam, // message identifier
LPARAM lParam // pointer to structure with message data
);

Parameters
nCode
Specifies a code the hook procedure uses to determine how to process the message. This parameter can be one of the following values: Value Meaning
HC_ACTION The wParam and lParam parameters contain information about a mouse message.


If nCode is less than zero, the hook procedure must pass the message to the CallNextHookEx function without further processing and should return the value returned by CallNextHookEx.

wParam
Specifies the identifier of the mouse message. This parameter can be one of the following messages:WM_LBUTTONDOWN,WM_LBUTTONUP,WM_MOUSEMOVE,WM_MOUSEWHEEL,WM_RBUTTONDOWN, orWM_RBUTTONUP.
lParam
Pointer to the MSLLHOOKSTRUCT structure.

Return Values
If nCode is less than zero, the hook procedure must return the value returned by CallNextHookEx.

If nCode is greater than or equal to zero, and the hook procedure did not process the message, it is highly recommended that you call CallNextHookEx and return the value it returns; otherwise, other applications that have installed WH_MOUSE_LL hooks will not receive hook notifications and may behave incorrectly as a result. If the hook procedure processed the message, it may return a nonzero value to prevent the system from passing the message to the rest of the hook chain or the target window procedure.

Remarks
An application installs the hook procedure by specifying the WH_MOUSE_LL hook type and the address of the hook procedure in a call to the SetWindowsHookEx function.

This hook is called in the context of the thread that installed it. The call is made by sending a message to the thread that installed the hook. Therefore, the thread that installed the hook must have a message loop.

The hook procedure should process a message in less time than the data entry specified in the LowLevelHooksTimeout value in the following registry key:

HKEY_CURRENT_USER\Control Panel\Desktop

The value is in milliseconds. If the hook procedure does not return during this interval, the system will pass the message to the next hook.

Note that debug hooks can not track this type of hook.

QuickInfo
Windows NT: Requires version 4.0 SP3 or later.
Windows: Unsupported.
Windows CE: Unsupported.
Header: Declared in winuser.h.
Import Library: User-defined.

See Also
Hooks Overview, Hook Functions, CallNextHookEx,mouse_event, MSLLHOOKSTRUCT, SetWindowsHookEx,WM_LBUTTONDOWN,WM_LBUTTONUP,WM_MOUSEMOVE,WM_MOUSEWHEEL,WM_RBUTTONDOWN,WM_RBUTTONUP.
[/Quote]
一路都是链接,直接翻译一下就行了呀.
嗷嗷叫的老马 2009-08-27
  • 打赏
  • 举报
回复
我晕倒...........

叫你参考那个代码,是子类化的......

我的意思是让你看看我是如何取滚轮方向,不是叫你使用子类化......

要用LL HOOK取滚轮方向很简单,我测试了一下.

先下载这个代码:

http://www.m5home.com/blog/article.asp?id=245

然后把鼠标HOOK事件里面写下面代码(其它相关代码也需要修改):

Private Type MSLLHOOKSTRUCT     '鼠标HOOK时lParam指针指向的结构
pt As POINTAPI
dwMouseData As Long
dwFlags As Long
dwTime As Long
dwExtraInfo As Long
End Type
Private Const WM_MOUSEWHEEL As Long = &H20A

Private Sub objHookLL_MouseHook(ByVal Code As Long, ByVal wParam As Long, ByVal lParam As Long, lRet As Long)
'鼠标HOOK事件.
'要吃了当前消息,把lRet = -1即可.
If Code = HC_ACTION And wParam = WM_MOUSEWHEEL Then
Dim stMLL As MSLLHOOKSTRUCT

Call CopyMemory(ByVal VarPtr(stMLL), ByVal lParam, Len(stMLL))

With stMLL
Debug.Print .dwMouseData / 65536 '输出滚轮状态
'在这里可以得到鼠标坐标以及其它信息
End With
End If
txtMCode.Text = Code
txtMwParam.Text = wParam
txtMlParam.Text = lParam
Debug.Print " M = " & Code, wParam, lParam
End Sub
泊客天涯 2009-08-26
  • 打赏
  • 举报
回复

当程序逝去焦点时怎么截获滚轮消息?
泊客天涯 2009-08-26
  • 打赏
  • 举报
回复
马哥,我做系统钩子 ,无法确定消息发到哪个窗体,因此hwnd属性要怎么控制啊
泊客天涯 2009-08-26
  • 打赏
  • 举报
回复
只能拦截本进程指定句柄的消息,对其他程序无效啊 帮忙啊
泊客天涯 2009-08-26
  • 打赏
  • 举报
回复

Private Sub Form_Load()
Set objSubClass = New cSubclass
objSubClass.AddWindowMsgs Me.hWnd '挂上子类化
s = GetForegroundWindow
' hHook = SetWindowsHookEx(WH_MOUSE_LL, AddressOf HookProc, App.hInstance, 0)
End Sub

Private Sub Form_Unload(Cancel As Integer)
' Call UnhookWindowsHookEx(hHook)
objSubClass.DeleteWindowMsg Me.hWnd '取消子类化
Set objSubClass = Nothing
End Sub

Private Sub objSubClass_MsgCome(ByVal bBefore As Boolean, bHandled As Boolean, lReturn As Long, lng_hWnd As Long, uMsg As Long, wParam As Long, lParam As Long)
If bBefore Then
Select Case uMsg
Case WM_MOUSEWHEEL '拦截滚轮消息
Select Case (wParam / 65536) '判断是向上滚还是向下滚
Case 120
MsgBox "{UP}" '模拟按键
Case -120
MsgBox "{DOWN}" '模拟按键
End Select
End Select
End If
End Sub
简单的代码

我用上类模块后编制上面程序,当窗体失去焦点时再滚动滚轮无法弹出MsgBox "{DOWN}"
泊客天涯 2009-08-26
  • 打赏
  • 举报
回复
可是我试着不行啊,当窗体后台运行时无法识别鼠标滚轮操作
hHook = SetWindowsHookEx(WH_MOUSE_LL, AddressOf HookProc, App.hInstance, 0)
返回值wParam = WM_MOUSEWHEEL 是无符号的吧 鼠标滚轮上下滚动 msgbox wParam 得到的结果都是一样的啊
嗷嗷叫的老马 2009-08-26
  • 打赏
  • 举报
回复
一样的吧

全局的HOOK还管啥焦点,全拦截.
Tiger_Zhao 2009-08-25
  • 打赏
  • 举报
回复
[Quote=MSDN:]WM_MOUSEWHEEL
fwKeys = LOWORD(wParam); // key flags
zDelta = (short) HIWORD(wParam); // wheel rotation
xPos = (short) LOWORD(lParam); // horizontal position of pointer
yPos = (short) HIWORD(lParam); // vertical position of pointer
...
zDelta
The value of the high-order word of wParam. Indicates the distance that the wheel is rotated, expressed in multiples or divisions of WHEEL_DELTA, which is 120. A positive value indicates that the wheel was rotated forward, away from the user; a negative value indicates that the wheel was rotated backward, toward the user.[/Quote]
看 zDelta 的正负,即 wParam 的正负。
波导终结者 2009-08-25
  • 打赏
  • 举报
回复
http://topic.csdn.net/t/20040413/15/2959988.html
贝隆 2009-08-25
  • 打赏
  • 举报
回复
学习!

1,486

社区成员

发帖
与我相关
我的任务
社区描述
VB API
社区管理员
  • API
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧