forked from zk2013/ReClass.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScrollableCustomControl.cs
More file actions
214 lines (185 loc) · 5.46 KB
/
Copy pathScrollableCustomControl.cs
File metadata and controls
214 lines (185 loc) · 5.46 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
using System;
using System.Diagnostics.Contracts;
using System.Windows.Forms;
namespace ReClassNET.UI
{
public class ScrollableCustomControl : UserControl
{
public ScrollableCustomControl()
{
VScroll = true;
HScroll = true;
}
protected override void OnMouseWheel(MouseEventArgs e)
{
Contract.Assume(VerticalScroll != null);
Contract.Assume(HorizontalScroll != null);
const int WHEEL_DELTA = 120;
var scrollProperties = VerticalScroll.Enabled ? VerticalScroll : (ScrollProperties)HorizontalScroll;
var wheelDelta = e.Delta;
while (Math.Abs(wheelDelta) >= WHEEL_DELTA)
{
if (wheelDelta > 0)
{
wheelDelta -= WHEEL_DELTA;
DoScroll(ScrollEventType.SmallDecrement, scrollProperties);
}
else
{
wheelDelta += WHEEL_DELTA;
DoScroll(ScrollEventType.SmallIncrement, scrollProperties);
}
}
base.OnMouseWheel(e);
}
private const int SB_LINEUP = 0;
private const int SB_LINEDOWN = 1;
private const int SB_PAGEUP = 2;
private const int SB_PAGEDOWN = 3;
private const int SB_THUMBPOSITION = 4;
private const int SB_THUMBTRACK = 5;
private const int SB_TOP = 6;
private const int SB_BOTTOM = 7;
private const int SB_ENDSCROLL = 8;
private ScrollEventType WParamToScrollEventType(IntPtr wParam)
{
switch (LoWord((int)wParam))
{
case SB_LINEUP:
return ScrollEventType.SmallDecrement;
case SB_LINEDOWN:
return ScrollEventType.SmallIncrement;
case SB_PAGEUP:
return ScrollEventType.LargeDecrement;
case SB_PAGEDOWN:
return ScrollEventType.LargeIncrement;
case SB_THUMBTRACK:
return ScrollEventType.ThumbTrack;
case SB_TOP:
return ScrollEventType.First;
case SB_BOTTOM:
return ScrollEventType.Last;
case SB_THUMBPOSITION:
return ScrollEventType.ThumbPosition;
case SB_ENDSCROLL:
return ScrollEventType.EndScroll;
default:
return ScrollEventType.EndScroll;
}
}
private const int WM_HSCROLL = 0x114;
private const int WM_VSCROLL = 0x115;
private void SetValue(ScrollEventType type, ScrollProperties scrollProperties, int newValue)
{
Contract.Requires(scrollProperties != null);
if (!scrollProperties.Enabled)
{
return;
}
if (newValue < scrollProperties.Minimum)
{
newValue = scrollProperties.Minimum;
}
if (newValue > scrollProperties.Maximum - scrollProperties.LargeChange)
{
newValue = scrollProperties.Maximum - scrollProperties.LargeChange + 1;
}
if (scrollProperties.Value != newValue)
{
var oldValue = scrollProperties.Value;
scrollProperties.Value = newValue;
if (type != ScrollEventType.EndScroll)
{
OnScroll(new ScrollEventArgs(
type,
oldValue,
newValue,
scrollProperties is VScrollProperties ? ScrollOrientation.VerticalScroll : ScrollOrientation.HorizontalScroll
));
Invalidate();
}
}
}
private void DoScroll(ScrollEventType type, ScrollProperties scrollProperties)
{
Contract.Requires(scrollProperties != null);
var oldValue = scrollProperties.Value;
var newValue = oldValue;
switch (type)
{
case ScrollEventType.SmallDecrement:
newValue = oldValue - (ModifierKeys == Keys.Control ? 1 : scrollProperties.SmallChange);
break;
case ScrollEventType.SmallIncrement:
newValue = oldValue + (ModifierKeys == Keys.Control ? 1 : scrollProperties.SmallChange);
break;
case ScrollEventType.LargeDecrement:
newValue = oldValue - scrollProperties.LargeChange;
break;
case ScrollEventType.LargeIncrement:
newValue = oldValue + scrollProperties.LargeChange;
break;
case ScrollEventType.First:
newValue = scrollProperties.Minimum;
break;
case ScrollEventType.Last:
newValue = scrollProperties.Maximum;
break;
}
SetValue(type, scrollProperties, newValue);
}
public void DoScroll(ScrollOrientation orientation, int amount)
{
if (orientation == ScrollOrientation.VerticalScroll && VerticalScroll.Enabled == false)
{
return;
}
if (orientation == ScrollOrientation.HorizontalScroll && HorizontalScroll.Enabled == false)
{
return;
}
var scrollProperties = orientation == ScrollOrientation.VerticalScroll ? VerticalScroll : (ScrollProperties)HorizontalScroll;
SetValue(ScrollEventType.ThumbPosition, scrollProperties, scrollProperties.Value + amount);
}
private void ProcessMessage(ref Message msg, ScrollProperties scrollProperties)
{
Contract.Requires(scrollProperties != null);
var type = WParamToScrollEventType(msg.WParam);
switch (type)
{
case ScrollEventType.SmallDecrement:
case ScrollEventType.SmallIncrement:
case ScrollEventType.LargeDecrement:
case ScrollEventType.LargeIncrement:
case ScrollEventType.First:
case ScrollEventType.Last:
DoScroll(type, scrollProperties);
break;
case ScrollEventType.ThumbTrack:
case ScrollEventType.ThumbPosition:
SetValue(type, scrollProperties, HiWord((int)msg.WParam));
break;
}
}
protected override void WndProc(ref Message msg)
{
if (msg.HWnd == Handle)
{
switch (msg.Msg)
{
case WM_VSCROLL:
case WM_HSCROLL:
if (msg.LParam != IntPtr.Zero)
{
break;
}
ProcessMessage(ref msg, msg.Msg == WM_VSCROLL ? VerticalScroll : (ScrollProperties)HorizontalScroll);
return;
}
}
base.WndProc(ref msg);
}
static int HiWord(int number) => (number >> 16) & 0xffff;
static int LoWord(int number) => number & 0xffff;
}
}