从 C# 中 WM_MOUSEHWHEEL 消息中的 wParam 检索 WHEEL_DELTA
我在 C# 中使用来自 user32.dll 的全局钩子和 dllimport 。键盘一工作正常,但鼠标滚轮事件是一个问题。这是我的鼠标事件回调:
private IntPtr MouseInputCallback(
int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode < 0) return CallNextHookEx(mouseHookId, nCode, wParam, lParam);
int eventType = wParam.ToInt32();
if (eventType == WM_MOUSEHWHEEL)
{
int wheelMovement = GetWheelDeltaWParam(eventType);
}
return CallNextHookEx(mouseHookId, nCode, wParam, lParam);
}
一切都很顺利,直到我必须检索 WHEEL_DELTA 值,该值显示滚轮滚动的方式和程度。由于 C# 缺少 GET_WHEEL_DELTA_WPARAM 宏,我正在使用应该完成这项工作的代码:
private static int GetWheelDeltaWParam(int wparam) { return (int)(wparam>>16); }
输出总是0,这没有任何意义。
编辑 - 结果:
MSLLHOOKSTRUCT mouseData = (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT));
int wheelMovement = GetWheelDeltaWParam(mouseData.mouseData);
[StructLayout(LayoutKind.Sequential)]
private struct MSLLHOOKSTRUCT
{
public Point pt;
public int mouseData;
public int flags;
public int time;
public long dwExtraInfo;
}
I'm using global hooks from user32.dll with dllimport in C#. Keyboard one works fine, but the mouse wheel events are a problem. This is my mouse event callback:
private IntPtr MouseInputCallback(
int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode < 0) return CallNextHookEx(mouseHookId, nCode, wParam, lParam);
int eventType = wParam.ToInt32();
if (eventType == WM_MOUSEHWHEEL)
{
int wheelMovement = GetWheelDeltaWParam(eventType);
}
return CallNextHookEx(mouseHookId, nCode, wParam, lParam);
}
Everything goes fine until I have to retrieve the WHEEL_DELTA value that shows which way and how much the wheel was scrolled. Since C# lacks the GET_WHEEL_DELTA_WPARAM macro, I'm using this code that should do the job:
private static int GetWheelDeltaWParam(int wparam) { return
(int)(wparam >> 16); }
But the output is always 0, which doesn't make any sense.
EDIT - result:
MSLLHOOKSTRUCT mouseData = (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT));
int wheelMovement = GetWheelDeltaWParam(mouseData.mouseData);
[StructLayout(LayoutKind.Sequential)]
private struct MSLLHOOKSTRUCT
{
public Point pt;
public int mouseData;
public int flags;
public int time;
public long dwExtraInfo;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是
GET_WHEEL_DELTA_WPARAM
用于从WindowProc
的wParam
中提取鼠标滚轮增量,而您的有一个LowLevelMouseProc
回调。在您的案例 ,wParam
就是WM_MOUSEWHEEL
;要获取车轮增量,您需要查看在该结构中,
你会发现你的价值。
请不要向我询问使用此结构所需的 C# P/Invoke 详细信息,因为我几乎肯定会弄错:)
The problem is that
GET_WHEEL_DELTA_WPARAM
is for extracting the mouse wheel delta from thewParam
of aWindowProc
, whereas what you have is aLowLevelMouseProc
callback. In your case,the
wParam
is simplyWM_MOUSEWHEEL
; to get the wheel delta, you need to look inand within that struct,
you will find your value.
Please don't ask me for the necessary C# P/Invoke details for working this struct as I would almost certainly get them wrong :)
在这里,您正在寻找
WM_MOUSE**H**WHEEL
,这是滚轮的水平移动(从一侧到另一侧),不是滚动滚轮的动作,即
WM_MOUSEWHEE
L。你确定这就是你想要的吗?也许只需在这里替换
WM_MOUSEWHEEL
以及其他建议即可。Here you are looking for
WM_MOUSE**H**WHEEL
, which is movement horizontally of the wheel (side to side),NOT the scrolling action of the wheel, which is
WM_MOUSEWHEE
L.Are you sure that's what you want? Maybe just substitute
WM_MOUSEWHEEL
here, as well as other suggestions.