GET_WHEEL_DELTA_WPARAM macro in C#
Asked Answered
H

3

6

How would I go about using the GET_WHEEL_DELTA_WPARAM macro in C#?

Hopi answered 15/2, 2012 at 23:2 Comment(2)
A sanity check is required here. You cannot use C# to write code that's called by a global hook. That requires an injectable DLL.Sig
It seems to kind of work for me from C# with dllimport but sometimes hogs windows 'n stuffHopi
T
2

High-order word, signed:

 ((short)(wParam>>16))
Tophole answered 15/2, 2012 at 23:6 Comment(6)
My method: public static short GetWheelDeltaWParam(int wparam) { return (short)(wparam >> 16); }, but it always return zero. I call it like this: GetWheelDeltaWParam((int)wParam);Hopi
If that doesn't work, try GetWDWP(unsigned int wparam) { return (short)((unsigned short)(wparam>>16)); }Tophole
unfortunately nope.. the first one should work, but it doesn'tHopi
Are you sure your wparam is correct? The problem may not be in this function. I don't see why it should be zero otherwise.Tophole
that may be the case, but I don't understand how I can move the mouse wheel and generate this event so that WHEEL_DELTA would be 0Hopi
I guess you need to post more of your code. Perhaps in a new question since this one is strictly about the macro.Tophole
B
9

For maximum clarity, I would define a set of functions like this:

internal static class NativeMethods
{
    internal static ushort HIWORD(IntPtr dwValue)
    {
        return (ushort)((((long)dwValue) >> 0x10) & 0xffff);
    }

    internal static ushort HIWORD(uint dwValue)
    {
        return (ushort)(dwValue >> 0x10);
    }

    internal static int GET_WHEEL_DELTA_WPARAM(IntPtr wParam)
    {
        return (short)HIWORD(wParam);
    }

    internal static int GET_WHEEL_DELTA_WPARAM(uint wParam)
    {
        return (short)HIWORD(wParam);
    }
}

And then use the function like so, where wParam is the WPARAM parameter you get from handling the Win32 WM_MOUSEWHEEL or WM_MOUSEHWHEEL messages:

int zDelta = NativeMethods.GET_WHEEL_DELTA_WPARAM(wParam);

You might need to suppress overflow-checking in order for this to work properly. To do so, either change your project settings, or wrap the relevant conversion functions in an unchecked block.

Bloch answered 16/2, 2012 at 1:20 Comment(0)
T
2

High-order word, signed:

 ((short)(wParam>>16))
Tophole answered 15/2, 2012 at 23:6 Comment(6)
My method: public static short GetWheelDeltaWParam(int wparam) { return (short)(wparam >> 16); }, but it always return zero. I call it like this: GetWheelDeltaWParam((int)wParam);Hopi
If that doesn't work, try GetWDWP(unsigned int wparam) { return (short)((unsigned short)(wparam>>16)); }Tophole
unfortunately nope.. the first one should work, but it doesn'tHopi
Are you sure your wparam is correct? The problem may not be in this function. I don't see why it should be zero otherwise.Tophole
that may be the case, but I don't understand how I can move the mouse wheel and generate this event so that WHEEL_DELTA would be 0Hopi
I guess you need to post more of your code. Perhaps in a new question since this one is strictly about the macro.Tophole
R
1

There's the MouseWheelEventArgs.Delta Property:

Gets a value that indicates the amount that the mouse wheel has changed.

private void MouseWheelHandler(object sender, MouseWheelEventArgs e)
{
    if (e.Delta > 0)
    {
        // Do one thing
    }
    else if (e.Delta < 0)
    {
        // Do the other thing
    }
}
Ruthenious answered 15/2, 2012 at 23:7 Comment(1)
unfortunately I forgot to mention I am using global hooks from user32.dll with dllimport so I don't have access to such fancy objects..Hopi

© 2022 - 2024 — McMap. All rights reserved.