Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add horizontal mouse wheel scrolling #37

Merged
merged 1 commit into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading