Skip to content

Commit

Permalink
fix: add horizontal mouse wheel scrolling
Browse files Browse the repository at this point in the history
  • Loading branch information
Rahul-Siemens committed May 3, 2024
1 parent 8ccf27f commit 92b3a13
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 3 deletions.
18 changes: 16 additions & 2 deletions Src/SourceGrid/Common/CustomScrollControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -696,11 +696,11 @@ public virtual void CustomScrollPageLeft()
if (HScrollBarVisible)
mHScrollBar.Value = Math.Max(mHScrollBar.Value - mHScrollBar.LargeChange, mHScrollBar.Minimum);
}
public virtual void CustomScrollWheel(int rotationDelta)
public virtual void CustomScrollWheel(int rotationDelta, bool asVertical)
{
if (rotationDelta >= 120 || rotationDelta <= -120)
{
if (VScrollBarVisible)
if (asVertical && VScrollBarVisible)
{
Point current = CustomScrollPosition;
int newY = current.Y +
Expand All @@ -714,6 +714,20 @@ public virtual void CustomScrollWheel(int rotationDelta)

CustomScrollPosition = new Point(current.X, newY);
}
else if (!asVertical && HScrollBarVisible)
{
Point current = CustomScrollPosition;
int newX = current.X +
SystemInformation.MouseWheelScrollLines * HScrollBar.SmallChange * -Math.Sign(rotationDelta);

//check that the value is between max and min
if (newX < 0)
newX = 0;
if (newX > MaximumHScroll)
newX = MaximumHScroll;

CustomScrollPosition = new Point(newX, current.Y);
}
}
}

Expand Down
49 changes: 48 additions & 1 deletion Src/SourceGrid/Grids/GridVirtual.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2101,7 +2101,54 @@ protected override void OnMouseWheel(MouseEventArgs e)
{
base.OnMouseWheel(e);

CustomScrollWheel(e.Delta);
// Check if shift key is being held down to simulate horizontal scroll
bool asVertical = (Control.ModifierKeys & Keys.Shift) != Keys.Shift;

CustomScrollWheel(e.Delta, asVertical);
}

protected virtual void OnMouseHWheel(MouseEventArgs e)
{
// Invert delta to match handling and scroll direction in Excel
CustomScrollWheel(-1 * e.Delta, false);
}

private static class NativeMethods
{
internal static int SignedHIWORD(IntPtr ptr)
{
unchecked
{
int n = (int)(long)ptr;
return (short)((n >> 16) & (int)ushort.MaxValue);
}
}

internal static int SignedLOWORD(IntPtr ptr)
{
unchecked
{
int n = (int)(long)ptr;
return (short)(n & (int)ushort.MaxValue);
}
}
}

protected override void WndProc(ref Message m)
{
const int WM_MOUSEHWHEEL = 0x020E;
if (m.Msg == WM_MOUSEHWHEEL)
{
Point p = new Point(NativeMethods.SignedLOWORD(m.LParam), NativeMethods.SignedHIWORD(m.LParam));
p = PointToClient(p);
OnMouseHWheel(new MouseEventArgs(MouseButtons.None, 0, p.X, p.Y, NativeMethods.SignedHIWORD(m.WParam)));

// Set as handled and do not call base method (same as Control.WmMouseWheel)
m.Result = (IntPtr)1;
return;
}

base.WndProc(ref m);
}

#endregion
Expand Down

0 comments on commit 92b3a13

Please sign in to comment.