为全局鼠标钩创建新的MouseWheelEventArgs

时间:2014-08-23 18:55:52

标签: c# wpf

根据MSDN,下面是MouseWheelEventArgs构造函数

public MouseWheelEventArgs(
    MouseDevice mouse,
    int timestamp,
    int delta
)

这是我试图完成的Global Mouse Hook中的一种方法:

private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{
    if (nCode >= 0 && MouseMessages.WM_MOUSEWHEEL == (MouseMessages)wParam)
    {
        MouseDevice mouseDev = InputManager.Current.PrimaryMouseDevice;

        MSLLHOOKSTRUCT hookStruct = (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT));

        //Initializes a new instance of the MouseWheelEventArgs class using the specified MouseDevice, timestamp, and delta.
        MouseAction(null, new MouseWheelEventArgs(mouseDev, timestamp, delta));
    }
    return CallNextHookEx(_hookID, nCode, wParam, lParam);
}

我正在寻找的是:

时间戳 - 只需动态生成当前日期时间即可吗?

delta - 您必须能够以某种方式以编程方式从鼠标设备获取此信息,但是如何?

1 个答案:

答案 0 :(得分:3)

Reference Source可以显示从" Environment.TickCount`中检索到timestamp

MSLLHOOKSTRUCT的MSDN Library article可以向您显示delta作为MSLLHOOKSTRUCT.mouseData元素的高位字传递。确保您将该成员声明为 int ,而不是 uint ,以便符号扩展正常工作。

因此:

   int timestamp = Environment.TickCount;
   int delta = hookStruct.mouseData >> 16;
   MouseAction(null, new MouseWheelEventArgs(mouseDev, timestamp, delta));