For maximum clarity, I would define a set of such functions:
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 a function where wParam is the wParam parameter that you get from Win32 message processing WM_MOUSEWHEEL or WM_MOUSEHWHEEL :
int zDelta = NativeMethods.GET_WHEEL_DELTA_WPARAM(wParam);
You may need to suppress the overflow check for this to work correctly. To do this, change the project settings or wrap the appropriate conversion functions in an unchecked block.
source share