forked from gitextensions/gitextensions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMouseWheelRedirector.cs
113 lines (96 loc) · 3.17 KB
/
MouseWheelRedirector.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using ResourceManager;
namespace GitUI
{
public sealed class MouseWheelRedirector : IMessageFilter
{
private static readonly MouseWheelRedirector instance = new();
private MouseWheelRedirector()
{
}
private bool _active;
public static bool Active
{
get { return instance._active; }
set
{
if (instance._active != value)
{
instance._active = value;
if (instance._active)
{
Application.AddMessageFilter(instance);
}
else
{
Application.RemoveMessageFilter(instance);
}
}
}
}
public bool PreFilterMessage(ref Message m)
{
const int WM_MOUSEWHEEL = 0x20a;
const int WM_MOUSEHWHEEL = 0x20e;
if (m.Msg != WM_MOUSEWHEEL && m.Msg != WM_MOUSEHWHEEL)
{
return false;
}
// WM_MOUSEWHEEL, find the control at screen position m.LParam
IntPtr hwnd = NativeMethods.WindowFromPoint(m.LParam.ToPoint());
if (hwnd == IntPtr.Zero)
{
return false;
}
Control control = Control.FromHandle(hwnd);
if (control is null)
{
return false;
}
if (hwnd == m.HWnd && !IsNonScrollableRichTextBox(control))
{
return false;
}
while (control is not (null or GitExtensionsControl))
{
bool nonScrollableRtbx = IsNonScrollableRichTextBox(control);
control = control.Parent;
if (nonScrollableRtbx)
{
hwnd = control.Handle;
}
}
if (control is null)
{
return false;
}
NativeMethods.SendMessage(hwnd, m.Msg, m.WParam, m.LParam);
return true;
static bool IsNonScrollableRichTextBox(Control c) => c is RichTextBox { ScrollBars: RichTextBoxScrollBars.None };
}
private static class NativeMethods
{
// P/Invoke declarations
[DllImport("user32.dll")]
public static extern IntPtr WindowFromPoint(POINT pt);
[DllImport("user32.dll")]
public static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);
[StructLayout(LayoutKind.Sequential)]
public readonly struct POINT
{
public readonly int X;
public readonly int Y;
public POINT(int x, int y)
{
X = x;
Y = y;
}
public static implicit operator Point(POINT p) => new(p.X, p.Y);
public static implicit operator POINT(Point p) => new(p.X, p.Y);
}
}
}
}