Skip to content

Commit 240bcf3

Browse files
fix: add horizontal mouse wheel scrolling (#37)
1 parent 04df51e commit 240bcf3

File tree

2 files changed

+64
-3
lines changed

2 files changed

+64
-3
lines changed

Src/SourceGrid/Common/CustomScrollControl.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -696,11 +696,11 @@ public virtual void CustomScrollPageLeft()
696696
if (HScrollBarVisible)
697697
mHScrollBar.Value = Math.Max(mHScrollBar.Value - mHScrollBar.LargeChange, mHScrollBar.Minimum);
698698
}
699-
public virtual void CustomScrollWheel(int rotationDelta)
699+
public virtual void CustomScrollWheel(int rotationDelta, bool asVertical)
700700
{
701701
if (rotationDelta >= 120 || rotationDelta <= -120)
702702
{
703-
if (VScrollBarVisible)
703+
if (asVertical && VScrollBarVisible)
704704
{
705705
Point current = CustomScrollPosition;
706706
int newY = current.Y +
@@ -714,6 +714,20 @@ public virtual void CustomScrollWheel(int rotationDelta)
714714

715715
CustomScrollPosition = new Point(current.X, newY);
716716
}
717+
else if (!asVertical && HScrollBarVisible)
718+
{
719+
Point current = CustomScrollPosition;
720+
int newX = current.X +
721+
SystemInformation.MouseWheelScrollLines * HScrollBar.SmallChange * -Math.Sign(rotationDelta);
722+
723+
//check that the value is between max and min
724+
if (newX < 0)
725+
newX = 0;
726+
if (newX > MaximumHScroll)
727+
newX = MaximumHScroll;
728+
729+
CustomScrollPosition = new Point(newX, current.Y);
730+
}
717731
}
718732
}
719733

Src/SourceGrid/Grids/GridVirtual.cs

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2101,7 +2101,54 @@ protected override void OnMouseWheel(MouseEventArgs e)
21012101
{
21022102
base.OnMouseWheel(e);
21032103

2104-
CustomScrollWheel(e.Delta);
2104+
// Check if shift key is being held down to simulate horizontal scroll
2105+
bool asVertical = (Control.ModifierKeys & Keys.Shift) != Keys.Shift;
2106+
2107+
CustomScrollWheel(e.Delta, asVertical);
2108+
}
2109+
2110+
protected virtual void OnMouseHWheel(MouseEventArgs e)
2111+
{
2112+
// Invert delta to match handling and scroll direction in Excel
2113+
CustomScrollWheel(-1 * e.Delta, false);
2114+
}
2115+
2116+
private static class NativeMethods
2117+
{
2118+
internal static int SignedHIWORD(IntPtr ptr)
2119+
{
2120+
unchecked
2121+
{
2122+
int n = (int)(long)ptr;
2123+
return (short)((n >> 16) & (int)ushort.MaxValue);
2124+
}
2125+
}
2126+
2127+
internal static int SignedLOWORD(IntPtr ptr)
2128+
{
2129+
unchecked
2130+
{
2131+
int n = (int)(long)ptr;
2132+
return (short)(n & (int)ushort.MaxValue);
2133+
}
2134+
}
2135+
}
2136+
2137+
protected override void WndProc(ref Message m)
2138+
{
2139+
const int WM_MOUSEHWHEEL = 0x020E;
2140+
if (m.Msg == WM_MOUSEHWHEEL)
2141+
{
2142+
Point p = new Point(NativeMethods.SignedLOWORD(m.LParam), NativeMethods.SignedHIWORD(m.LParam));
2143+
p = PointToClient(p);
2144+
OnMouseHWheel(new MouseEventArgs(MouseButtons.None, 0, p.X, p.Y, NativeMethods.SignedHIWORD(m.WParam)));
2145+
2146+
// Set as handled and do not call base method (same as Control.WmMouseWheel)
2147+
m.Result = (IntPtr)1;
2148+
return;
2149+
}
2150+
2151+
base.WndProc(ref m);
21052152
}
21062153

21072154
#endregion

0 commit comments

Comments
 (0)