0

How can I set the Delta parameter of the WM_MOUSEWHEEL message and send the message to a Windows using PostMessage?

My code:

[DllImport("user32.dll")]
static extern bool PostMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);

const uint WM_MOUSEWHEEL = 0x020A;

private int MAKELPARAM(int p, int p_2)
{
    return ((p_2 << 16) | (p & 0xFFFF));
}

Now I'm using it like this:

IntPtr hwnd1;
hwnd1 = FindWindow(null, "NoxPlayer");
Point location = new Point(1205,411);
PostMessage(hwnd1, WM_MOUSEWHEEL, 0, MAKELPARAM(location.X, location.Y));

When I do it like that I have:

enter image description here

The Windows doesn't scroll because delta is 0.

2
  • What is it you are trying to accomplish? Send scroll input to a window? You might want to look into SendInput (also user32.dll)
    – Fixation
    Feb 13, 2020 at 10:36
  • Im trying just to send scroll mouse button to window , without focos window. Right and left click button works well. I have only problem with scroll because of this zDelta parametr.
    – adam
    Feb 13, 2020 at 10:40

1 Answer 1

4

The WHEEL_DELTA is the default value of a mouse wheel increment (for mouse controllers with no freely-rotating wheel). This value is returned by SystemInformation.MouseWheelScrollDelta.
It's usually set to 120, but it can be a different value.
When the Delta value is positive, it indicates that the mouse wheel is rotated forward, causing a Window to scroll upwards, the opposite when Delta is negative.
In the code sample, directionUp and directionDown determine this direction.

The Delta can be set to a fraction or a multiple of the base increment (can be used to fine-tune the scrolling). See the Docs about WM_MOUSEWHEEL for more information.

In the example, the wheel increment is divided in half (the float multiplier argument of the MAKEWPARAM macro is set to .5f).

The Window identified by [Handle] doesn't need to be activated (it doesn't need to be the Foreground Window).

The Cursor position represents Screen coordinates. The application should be DpiAware to handle Cursor positions and Screen coordinates correctly.
See the notes about the VirtualScreen and how DpiAwareness is involved here:

Using SetWindowPos with multiple monitors.

int directionUp = 1;
int directionDown = -1;

// Scrolls [Handle] down by 1/2 wheel rotation with Left Button pressed
IntPtr wParam = MAKEWPARAM(directionDown, .5f, WinMsgMouseKey.MK_LBUTTON);
IntPtr lParam = MAKELPARAM(Cursor.Position.X, Cursor.Position.Y);

PostMessage([Handle], WM_MOUSEWHEEL, wParam, lParam);

internal const uint WM_MOUSEWHEEL = 0x020A;

[Flags]
public enum WinMsgMouseKey : int
{
    MK_NONE = 0x0000,
    MK_LBUTTON = 0x0001,
    MK_RBUTTON = 0x0002,
    MK_SHIFT = 0x0004,
    MK_CONTROL = 0x0008,
    MK_MBUTTON = 0x0010,
    MK_XBUTTON1 = 0x0020,
    MK_XBUTTON2 = 0x0040
}

[DllImport("User32.dll", SetLastError = true, CharSet = CharSet.Auto)]
internal static extern bool PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

internal static IntPtr MAKEWPARAM(int direction, float multiplier, WinMsgMouseKey button)
{
    int delta = (int)(SystemInformation.MouseWheelScrollDelta * multiplier);
    return (IntPtr)(((delta << 16) * Math.Sign(direction) | (ushort)button));
}

internal static IntPtr MAKELPARAM(int low, int high)
{
    return (IntPtr)((high << 16) | (low & 0xFFFF));
}
5
  • It looks like that your example works only for forward wheeling. Backwards does not work, as delta remains positive as a result to MAKEWPARAM call. Feb 11, 2021 at 5:10
  • @Henri Nope, that's what the direction parameter is for. If you pass a negative value, you scroll down, otherwise you scroll up.
    – Jimi
    Feb 11, 2021 at 9:46
  • I've tested your code and it did not work. I changed the MAKEWPARAM signature to MAKEWPARAM(delta, button) where delta can either be positive or negative and the function returns (delta << 16) | button. Feb 11, 2021 at 13:41
  • @Henri Well, that's exactly what this code is doing. You pass a Delta (absolute value - no sign) and a direction int directionUp = 1; int directionDown = -1;. You can apply the Direction directly to the Delta value (making it signed). It's the same exact thing, just a different implementation: you do it beforehand or later, it doesn't change the result. This code is tested.
    – Jimi
    Feb 11, 2021 at 13:46
  • @Henri Here, the implemetation accepts a direction because the Delta is calculated based on the System setting - which is a User preference - and overrides it to provide a custom behavior, in int delta = (int)(SystemInformation.MouseWheelScrollDelta * multiplier);, so you can perform smoother scrolling if needed. The direction, which is provided as parameter, is applied later. -- You can also write it as int delta = (int)(SystemInformation.MouseWheelScrollDelta * multiplier) * Math.Sign(direction); return (IntPtr)((delta << 16) | (ushort)buttons);
    – Jimi
    Feb 11, 2021 at 14:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.