forked from cyotek/Dithering
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGroupBox.cs
360 lines (305 loc) · 8.34 KB
/
GroupBox.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
namespace Cyotek.Windows.Forms
{
// Cyotek GroupBox Component
// www.cyotek.com
/// <summary>
/// Represents a Windows control that displays a frame at the top of a group of controls with an optional caption and icon.
/// </summary>
[ToolboxItem(true)]
[DefaultEvent("Click")]
[DefaultProperty("Text")]
[Designer("System.Windows.Forms.Design.GroupBoxDesigner, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
[Designer("System.Windows.Forms.Design.DocumentDesigner, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(IRootDesigner))]
internal class GroupBox : System.Windows.Forms.GroupBox
{
#region Instance Fields
private Border3DSide _borders = Border3DSide.Top;
private Pen _bottomPen;
private Color _headerForeColor;
private Size _iconMargin;
private Image _image;
private Color _lineColorBottom;
private Color _lineColorTop;
private bool _showBorders;
private Pen _topPen;
#endregion
#region Public Constructors
/// <summary>
/// Initializes a new instance of the <see cref="GroupBox"/> class.
/// </summary>
public GroupBox()
{
_showBorders = true;
_iconMargin = new Size(0, 6);
_lineColorBottom = SystemColors.ButtonHighlight;
_lineColorTop = SystemColors.ButtonShadow;
_headerForeColor = SystemColors.HotTrack;
SetStyle(ControlStyles.DoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.ResizeRedraw | ControlStyles.UserPaint | ControlStyles.SupportsTransparentBackColor, true);
CreateResources();
}
#endregion
#region Overridden Properties
/// <summary>
/// Gets a rectangle that represents the dimensions of the <see cref="T:System.Windows.Forms.GroupBox"/>.
/// </summary>
/// <value></value>
/// <returns>
/// A <see cref="T:System.Drawing.Rectangle"/> with the dimensions of the <see cref="T:System.Windows.Forms.GroupBox"/>.
/// </returns>
public override Rectangle DisplayRectangle
{
get
{
Size clientSize;
int fontHeight;
int imageSize;
clientSize = base.ClientSize;
fontHeight = Font.Height;
if (_image != null)
{
imageSize = _iconMargin.Width + _image.Width + 3;
}
else
{
imageSize = 0;
}
return new Rectangle(3 + imageSize, fontHeight + 3, Math.Max(clientSize.Width - (imageSize + 6), 0), Math.Max(clientSize.Height - fontHeight - 6, 0));
}
}
/// <summary>
/// Returns or sets the text displayed in this control.
/// </summary>
/// <value></value>
/// <returns>
/// The text associated with this control.
/// </returns>
[Browsable(true)]
[DefaultValue("")]
public override string Text
{
get { return base.Text; }
set
{
base.Text = value;
Invalidate();
}
}
#endregion
#region Overridden Methods
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing)
{
CleanUpResources();
}
base.Dispose(disposing);
}
/// <summary>
/// Occurs when the control is to be painted.
/// </summary>
/// <param name="e">A <see cref="T:System.Windows.Forms.PaintEventArgs"/> that contains the event data.</param>
protected override void OnPaint(PaintEventArgs e)
{
SizeF size;
int y;
TextFormatFlags flags;
Rectangle textBounds;
flags = TextFormatFlags.WordEllipsis | TextFormatFlags.NoPrefix | TextFormatFlags.Left | TextFormatFlags.SingleLine;
size = TextRenderer.MeasureText(e.Graphics, Text, Font, ClientSize, flags);
y = (int)(size.Height + 3) / 2;
textBounds = new Rectangle(1, 1, (int)size.Width, (int)size.Height);
if (ShowBorders)
{
if ((_borders & Border3DSide.All) == Border3DSide.All)
{
e.Graphics.DrawRectangle(_bottomPen, 1, y + 1, Width - 2, Height - (y + 2));
e.Graphics.DrawRectangle(_topPen, 0, y, Width - 2, Height - (y + 2));
}
else
{
if ((_borders & Border3DSide.Top) == Border3DSide.Top)
{
e.Graphics.DrawLine(_topPen, size.Width + 3, y, Width - 5, y);
e.Graphics.DrawLine(_bottomPen, size.Width + 3, y + 1, Width - 5, y + 1);
}
if ((_borders & Border3DSide.Left) == Border3DSide.Left)
{
e.Graphics.DrawLine(_topPen, 0, y, 0, Height - 1);
e.Graphics.DrawLine(_bottomPen, 1, y, 1, Height - 1);
}
if ((_borders & Border3DSide.Right) == Border3DSide.Right)
{
e.Graphics.DrawLine(_bottomPen, Width - 1, y, Width - 1, Height - 1);
e.Graphics.DrawLine(_topPen, Width - 2, y, Width - 2, Height - 1);
}
if ((_borders & Border3DSide.Bottom) == Border3DSide.Bottom)
{
e.Graphics.DrawLine(_topPen, 0, Height - 2, Width, Height - 2);
e.Graphics.DrawLine(_bottomPen, 0, Height - 1, Width, Height - 1);
}
}
}
// header text
TextRenderer.DrawText(e.Graphics, Text, Font, textBounds, HeaderForeColor, flags);
// draw the image
if ((_image != null))
{
e.Graphics.DrawImage(_image, Padding.Left + _iconMargin.Width, Padding.Top + (int)size.Height + _iconMargin.Height, _image.Width, _image.Height);
}
//draw a designtime outline
if (DesignMode && (_borders & Border3DSide.All) != Border3DSide.All)
{
using var pen = new Pen(SystemColors.ButtonShadow);
pen.DashStyle = DashStyle.Dot;
e.Graphics.DrawRectangle(pen, 0, 0, Width - 1, Height - 1);
}
}
/// <summary>
/// Raises the <see cref="System.Windows.Forms.Control.SystemColorsChanged"/> event.
/// </summary>
/// <param name="e">An <see cref="T:System.EventArgs"/> that contains the event data.</param>
protected override void OnSystemColorsChanged(EventArgs e)
{
base.OnSystemColorsChanged(e);
CreateResources();
Invalidate();
}
#endregion
#region Public Properties
[Category("Appearance")]
[DefaultValue(typeof(Border3DSide), "Top")]
public Border3DSide Borders
{
get { return _borders; }
set
{
_borders = value;
Invalidate();
}
}
[Category("Appearance")]
[DefaultValue(typeof(Color), "HotTrack")]
public Color HeaderForeColor
{
get { return _headerForeColor; }
set
{
_headerForeColor = value;
CreateResources();
Invalidate();
}
}
/// <summary>
/// Gets or sets the icon margin.
/// </summary>
/// <value>The icon margin.</value>
[Category("Appearance")]
[DefaultValue(typeof(Size), "0, 6")]
public Size IconMargin
{
get { return _iconMargin; }
set
{
_iconMargin = value;
Invalidate();
}
}
/// <summary>
/// Gets or sets the image to display.
/// </summary>
/// <value>The image to display.</value>
[Browsable(true)]
[Category("Appearance")]
[DefaultValue(typeof(Image), "")]
public Image Image
{
get { return _image; }
set
{
_image = value;
Invalidate();
}
}
/// <summary>
/// Gets or sets the line color bottom.
/// </summary>
/// <value>The line color bottom.</value>
[Browsable(true)]
[Category("Appearance")]
[DefaultValue(typeof(Color), "ButtonHighlight")]
public Color LineColorBottom
{
get { return _lineColorBottom; }
set
{
_lineColorBottom = value;
CreateResources();
Invalidate();
}
}
/// <summary>
/// Gets or sets the line color top.
/// </summary>
/// <value>The line color top.</value>
[Browsable(true)]
[Category("Appearance")]
[DefaultValue(typeof(Color), "ButtonShadow")]
public Color LineColorTop
{
get { return _lineColorTop; }
set
{
_lineColorTop = value;
CreateResources();
Invalidate();
}
}
[DefaultValue(true)]
[Category("Appearance")]
public bool ShowBorders
{
get { return _showBorders; }
set
{
_showBorders = value;
Invalidate();
}
}
#endregion
#region Private Members
/// <summary>
/// Cleans up GDI resources.
/// </summary>
private void CleanUpResources()
{
if (_topPen != null)
{
_topPen.Dispose();
}
if (_bottomPen != null)
{
_bottomPen.Dispose();
}
}
/// <summary>
/// Creates GDI resources.
/// </summary>
private void CreateResources()
{
CleanUpResources();
_topPen = new Pen(_lineColorTop);
_bottomPen = new Pen(_lineColorBottom);
}
#endregion
}
}