-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathControlUtilities.cs
332 lines (202 loc) · 10.3 KB
/
ControlUtilities.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
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
using Gsemac.Win32.Native;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;
namespace Gsemac.Forms {
public static class ControlUtilities {
// Public members
public static bool MouseIntersectsWith(Control control) {
return MouseIntersectsWith(control, control.ClientRectangle);
}
public static bool MouseIntersectsWith(Control control, Rectangle rect) {
Point mousePos = control.PointToClient(Cursor.Position);
Rectangle mouseRect = new Rectangle(mousePos.X, mousePos.Y, 1, 1);
return rect.IntersectsWith(mouseRect);
}
public static bool ContainsText(Control control, string text, StringComparison comparisonType = StringComparison.CurrentCulture) {
if (control.Text.IndexOf(text, comparisonType) != -1)
return true;
foreach (Control childControl in control.Controls)
if (ContainsText(childControl, text, comparisonType))
return true;
return false;
}
public static string EscapeLabelText(string text) {
// The "UseMnemonic" property can be set to "false" instead of escaping the text.
// However, this property is not available on the ToolStripLabel control.
if (string.IsNullOrWhiteSpace(text))
return text;
return text.Replace("&", "&&");
}
public static string UnescapeLabelText(string text) {
if (string.IsNullOrWhiteSpace(text))
return text;
return text.Replace("&&", "&");
}
public static TextFormatFlags GetTextFormatFlags(ContentAlignment contentAlignment) {
TextFormatFlags flags = TextFormatFlags.Default;
if ((contentAlignment & (ContentAlignment.TopLeft | ContentAlignment.TopCenter | ContentAlignment.TopRight)) != 0)
flags |= TextFormatFlags.Top;
if ((contentAlignment & (ContentAlignment.TopLeft | ContentAlignment.MiddleLeft | ContentAlignment.BottomLeft)) != 0)
flags |= TextFormatFlags.Left;
if ((contentAlignment & (ContentAlignment.TopRight | ContentAlignment.MiddleRight | ContentAlignment.BottomRight)) != 0)
flags |= TextFormatFlags.Right;
if ((contentAlignment & (ContentAlignment.BottomRight | ContentAlignment.BottomCenter | ContentAlignment.BottomLeft)) != 0)
flags |= TextFormatFlags.Bottom;
if ((contentAlignment & (ContentAlignment.MiddleRight | ContentAlignment.MiddleCenter | ContentAlignment.MiddleLeft)) != 0)
flags |= TextFormatFlags.VerticalCenter;
if ((contentAlignment & (ContentAlignment.TopCenter | ContentAlignment.MiddleCenter | ContentAlignment.BottomCenter)) != 0)
flags |= TextFormatFlags.HorizontalCenter;
return flags;
}
public static TextFormatFlags GetTextFormatFlags(HorizontalAlignment horizontalAlignment) {
TextFormatFlags flags = TextFormatFlags.Default;
if (horizontalAlignment == HorizontalAlignment.Left)
flags |= TextFormatFlags.Left;
else if (horizontalAlignment == HorizontalAlignment.Center)
flags |= TextFormatFlags.HorizontalCenter;
else if (horizontalAlignment == HorizontalAlignment.Right)
flags |= TextFormatFlags.Right;
return flags;
}
public static ScrollBars GetVisibleScrollBars(ListBox control) {
ScrollBars scrollBars = ScrollBars.None;
if (Enumerable.Range(0, control.Items.Count).Sum(i => control.GetItemHeight(i)) > control.Height)
scrollBars |= ScrollBars.Vertical;
return scrollBars;
}
public static ScrollBars GetVisibleScrollBars(TextBox control) {
ScrollBars scrollBars = ScrollBars.None;
if (control.Multiline) {
scrollBars = control.ScrollBars;
// If WordWrap is enabled, the horizontal scroll bar never appears, even if it is enabled.
if (control.WordWrap)
scrollBars &= ~ScrollBars.Horizontal;
}
return scrollBars;
}
public static ScrollBars GetVisibleScrollBars(Control control) {
if (control is ListBox listBox)
return GetVisibleScrollBars(listBox);
if (control is TextBox textBox)
return GetVisibleScrollBars(textBox);
ScrollBars scrollBars = ScrollBars.None;
Size size = control.GetPreferredSize(Size.Empty);
if (size.Height > control.Height)
scrollBars |= ScrollBars.Vertical;
if (size.Width > control.Width)
scrollBars |= ScrollBars.Horizontal;
return scrollBars;
}
public static int GetScrollPosition(Control control) {
return GetScrollPosition(control, ScrollBars.Vertical);
}
public static int GetScrollPosition(Control control, ScrollBars scrollBar) {
if (control is null)
throw new ArgumentNullException(nameof(control));
if (scrollBar == ScrollBars.Both)
throw new ArgumentOutOfRangeException(nameof(scrollBar));
if (scrollBar == ScrollBars.None)
return 0;
int scrollBarNum = scrollBar == ScrollBars.Horizontal ?
Constants.SB_HORZ :
Constants.SB_VERT;
return User32.GetScrollPos(control.Handle, scrollBarNum);
}
public static void SetScrollPosition(Control control, int position) {
SetScrollPosition(control, ScrollBars.Vertical, position);
}
public static void SetScrollPosition(Control control, ScrollBars scrollBar, int position) {
if (control is null)
throw new ArgumentNullException(nameof(control));
if (scrollBar == ScrollBars.None)
return;
int scrollBarNum = Constants.SB_VERT;
int scrollBarDirection = Constants.WM_VSCROLL;
if (scrollBar == ScrollBars.Both) {
// TODO: What direction should be set here?
scrollBarNum = Constants.SB_BOTH;
}
else if (scrollBar == ScrollBars.Horizontal) {
scrollBarNum = Constants.SB_HORZ;
scrollBarDirection = Constants.WM_HSCROLL;
}
User32.SetScrollPos(control.Handle, scrollBarNum, position, true);
// We need to adjust the position before using SendMessage.
// https://stackoverflow.com/a/39027122/5383169
IntPtr msgPosition = new IntPtr((position << 16) + 4);
User32.SendMessage(control.Handle, scrollBarDirection, msgPosition, IntPtr.Zero);
}
public static bool GetStyle(Control control, ControlStyles styles) {
MethodInfo getStyleMethod = control.GetType()
.GetMethod("GetStyle", BindingFlags.Instance | BindingFlags.NonPublic);
if (getStyleMethod is null)
return false;
return (bool)getStyleMethod.Invoke(control, new object[] { styles });
}
public static bool SetStyle(Control control, ControlStyles styles, bool value) {
MethodInfo setStyleMethod = control.GetType()
.GetMethod("SetStyle", BindingFlags.Instance | BindingFlags.NonPublic);
if (setStyleMethod is null)
return false;
setStyleMethod.Invoke(control, new object[] { styles, value });
return true;
}
public static ControlStyles GetStyles(Control control) {
ControlStyles result = 0;
foreach (ControlStyles style in Enum.GetValues(typeof(ControlStyles)))
if (GetStyle(control, style))
result |= style;
return result;
}
public static bool SetStyles(Control control, ControlStyles styles) {
ClearStyles(control);
bool success = true;
foreach (ControlStyles style in Enum.GetValues(typeof(ControlStyles)))
if (styles.HasFlag(style))
success &= SetStyle(control, style, true);
return success;
}
public static bool SetDoubleBuffered(Control control, bool value) {
PropertyInfo doubleBufferedProperty = control.GetType()
.GetProperty("DoubleBuffered", BindingFlags.Instance | BindingFlags.NonPublic);
if (doubleBufferedProperty is null)
return false;
doubleBufferedProperty.SetValue(control, value, null);
return true;
}
public static bool SetResizeRedraw(Control control, bool value) {
// ResizeRedraw needs to be set to true to prevent smearing when custom-painting controls.
// https://stackoverflow.com/a/39419274/5383169
PropertyInfo drawModeProperty = control.GetType()
.GetProperty("ResizeRedraw", BindingFlags.NonPublic | BindingFlags.Instance);
if (drawModeProperty is null)
return false;
drawModeProperty.SetValue(control, value, null);
return true;
}
public static IEnumerable<IComponent> GetComponents(Control control) {
// Returns components added via the designer (e.g. ToolTips).
FieldInfo fieldInfo = control.GetType()
.GetField("components", BindingFlags.Instance | BindingFlags.NonPublic);
if (fieldInfo is object && fieldInfo.GetValue(control) is IContainer container) {
return container.Components.OfType<IComponent>();
}
else {
return Enumerable.Empty<IComponent>();
}
}
public static IEnumerable<ToolTip> GetToolTips(Control control) {
return GetComponents(control).OfType<ToolTip>();
}
// Private members
private static void ClearStyles(Control control) {
foreach (ControlStyles style in Enum.GetValues(typeof(ControlStyles)))
SetStyle(control, style, false);
}
}
}