从 C# 中 WM_MOUSEHWHEEL 消息中的 wParam 检索 WHEEL_DELTA

发布于 2025-01-06 09:57:46 字数 1447 浏览 0 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

紫﹏色ふ单纯 2025-01-13 09:57:46

问题是 GET_WHEEL_DELTA_WPARAM 用于从 WindowProcwParam 中提取鼠标滚轮增量,而您的有一个 LowLevelMouseProc 回调。在您的案例 ,

wParam [输入]

类型:WPARAM

鼠标消息的标识符。该参数可以是以下消息之一:WM_LBUTTONDOWN、WM_LBUTTONUP、WM_MOUSEMOVE、
WM_MOUSEWHEEL、WM_MOUSEHWHEEL、WM_RBUTTONDOWN 或 WM_RBUTTONUP。

wParam 就是 WM_MOUSEWHEEL;要获取车轮增量,您需要查看

lParam [输入]

类型:LPARAM

指向 的指针MSLLHOOKSTRUCT 结构。

在该结构中,

鼠标数据

类型:DWORD

如果消息是WM_MOUSEWHEEL,则该成员的高位字是滚轮增量。低位字被保留。积极的
值表示车轮向前旋转,远离
用户;负值表示车轮向后旋转,
朝向用户。一轮点击定义为 WHEEL_DELTA,即
120.

你会发现你的价值。

请不要向我询问使用此结构所需的 C# P/Invoke 详细信息,因为我几乎肯定会弄错:)

The problem is that GET_WHEEL_DELTA_WPARAM is for extracting the mouse wheel delta from the wParam of a WindowProc, whereas what you have is a LowLevelMouseProc callback. In your case,

wParam [in]

Type: WPARAM

The identifier of the mouse message. This parameter can be one of the following messages: WM_LBUTTONDOWN, WM_LBUTTONUP, WM_MOUSEMOVE,
WM_MOUSEWHEEL, WM_MOUSEHWHEEL, WM_RBUTTONDOWN, or WM_RBUTTONUP.

the wParam is simply WM_MOUSEWHEEL; to get the wheel delta, you need to look in

lParam [in]

Type: LPARAM

A pointer to an MSLLHOOKSTRUCT structure.

and within that struct,

mouseData

Type: DWORD

If the message is WM_MOUSEWHEEL, the high-order word of this member is the wheel delta. The low-order word is reserved. 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. One wheel click is defined as WHEEL_DELTA, which is
120.

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 :)

预谋 2025-01-13 09:57:46

在这里,您正在寻找WM_MOUSE**H**WHEEL,这是滚轮的水平移动(从一侧到另一侧)

不是滚动滚轮的动作,即 WM_MOUSEWHEEL。

        if (eventType == WM_MOUSE**H**WHEEL)
        {
            int wheelMovement = GetWheelDeltaWParam(eventType);
        }

你确定这就是你想要的吗?也许只需在这里替换 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_MOUSEWHEEL.

        if (eventType == WM_MOUSE**H**WHEEL)
        {
            int wheelMovement = GetWheelDeltaWParam(eventType);
        }

Are you sure that's what you want? Maybe just substitute WM_MOUSEWHEEL here, as well as other suggestions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文