-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathNiceLine.cs
More file actions
368 lines (338 loc) · 12.3 KB
/
NiceLine.cs
File metadata and controls
368 lines (338 loc) · 12.3 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
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
361
362
363
364
365
366
367
368
// http://www.codeproject.com/KB/miscctrl/NiceLine.aspx
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
namespace SharpOcarina
{
/// <summary>
/// "NiceLine" draws a shaded line separator. Can have an aligned text caption.
/// </summary>
[DefaultProperty("Caption")]
[ToolboxBitmap(typeof(System.Windows.Forms.GroupBox))]
public class NiceLine : System.Windows.Forms.UserControl
{
private System.ComponentModel.Container components = null;
private string _Caption = "";
private int _CaptionMarginSpace = 16;
private int _CaptionPadding = 2;
private LineVerticalAlign _LineVerticalAlign = LineVerticalAlign.Middle;
private CaptionOrizontalAlign _CaptionOrizontalAlign = CaptionOrizontalAlign.Left;
private LineOrientation _LineOrientation = LineOrientation.Horizontal;
public NiceLine()
{
// This call is required by the Windows.Forms Form Designer.
InitializeComponent();
// Cannot focus via tab
this.TabStop = false;
}
/// <summary>
/// The caption text displayed on the line.
/// If the caption is "" (the default) the line is not broken
/// </summary>
[Category("Appearance")]
[DefaultValue("")]
[Description("The caption text displayed on the line. If the caption is \"\" (the default) the line is not broken")]
public string Caption
{
get { return _Caption; }
set
{
_Caption = value;
this.Invalidate();
}
}
/// <summary>
/// The distance in pixels form the control margin to caption text
/// </summary>
[Category("Appearance")]
[DefaultValue(16)]
[Description("The distance in pixels form the control margin to caption text")]
public int CaptionMarginSpace
{
get { return _CaptionMarginSpace; }
set
{
_CaptionMarginSpace = value;
Invalidate();
}
}
/// <summary>
/// The space in pixels around text caption
/// </summary>
[Category("Appearance")]
[DefaultValue(2)]
[Description("The space in pixels around text caption")]
public int CaptionPadding
{
get { return _CaptionPadding; }
set
{
_CaptionPadding = value;
Invalidate();
}
}
/// <summary>
/// The vertical alignment of the line within the space of the control
/// </summary>
[Category("Appearance")]
[DefaultValue(LineVerticalAlign.Middle)]
[Description("The vertical alignment of the line within the space of the control")]
public LineVerticalAlign LineVerticalAlign
{
get { return _LineVerticalAlign; }
set
{
_LineVerticalAlign = value;
Invalidate();
}
}
/// <summary>
/// Tell where the text caption is aligned in the control
/// </summary>
[Category("Appearance")]
[DefaultValue(CaptionOrizontalAlign.Left)]
[Description("Tell where the text caption is aligned in the control")]
public CaptionOrizontalAlign CaptionOrizontalAlign
{
get { return _CaptionOrizontalAlign; }
set
{
_CaptionOrizontalAlign = value;
Invalidate();
}
}
/// <summary>
/// ---
/// </summary>
[Category("Appearance")]
[DefaultValue(LineOrientation.Horizontal)]
[Description("")]
public LineOrientation LineOrientation
{
get { return _LineOrientation; }
set
{
_LineOrientation = value;
Invalidate();
}
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
//
// NiceLine
//
this.Name = "NiceLine";
this.Size = new System.Drawing.Size(100, this.Font.Height);
}
#endregion
// protected override CreateParams CreateParams
// {
// get
// {
// CreateParams cp = base.CreateParams;
// cp.ExStyle |= 0x20;
// return cp;
// }
// }
//
// protected override void OnMove(EventArgs e)
// {
// RecreateHandle();
// }
//
// protected override void OnPaintBackground(PaintEventArgs e)
// {
// // do nothing
// }
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
base.OnPaint(e);
int ym;
switch (LineVerticalAlign)
{
case LineVerticalAlign.Top:
ym = 0;
break;
case LineVerticalAlign.Middle:
ym = Convert.ToInt32(Math.Ceiling((decimal)Size.Height / 2)) - 1;
break;
case LineVerticalAlign.Bottom:
ym = Size.Height - 2;
break;
default:
ym = 0;
break;
}
SizeF captionSizeF = e.Graphics.MeasureString(Caption, this.Font, this.Width - CaptionMarginSpace * 2, StringFormat.GenericDefault);
int captionLength = Convert.ToInt32(captionSizeF.Width);
int beforeCaption;
int afterCaption;
if (Caption == "")
{
beforeCaption = CaptionMarginSpace;
afterCaption = CaptionMarginSpace;
}
else
{
switch (CaptionOrizontalAlign)
{
case CaptionOrizontalAlign.Left:
beforeCaption = CaptionMarginSpace;
afterCaption = CaptionMarginSpace + CaptionPadding * 2 + captionLength;
break;
case CaptionOrizontalAlign.Center:
beforeCaption = (Width - captionLength) / 2 - CaptionPadding;
afterCaption = (Width - captionLength) / 2 + captionLength + CaptionPadding;
break;
case CaptionOrizontalAlign.Right:
beforeCaption = Width - CaptionMarginSpace * 2 - captionLength;
afterCaption = Width - CaptionMarginSpace;
break;
default:
beforeCaption = CaptionMarginSpace;
afterCaption = CaptionMarginSpace;
break;
}
}
// Lines
if (_LineOrientation == LineOrientation.Horizontal)
{
// -------
// | ...caption...
e.Graphics.DrawLines(new Pen(SystemColors.ControlDark, 1),
new Point[] {
new Point(0, ym + 1),
new Point(0, ym),
new Point(beforeCaption, ym)
}
);
// -------
// ...caption...
e.Graphics.DrawLines(new Pen(SystemColors.ControlDark, 1),
new Point[] {
new Point(afterCaption, ym),
new Point(this.Width, ym)
}
);
// ...caption...
// -------
e.Graphics.DrawLines(new Pen(SystemColors.ControlLightLight, 1),
new Point[] {
new Point(0, ym + 1),
new Point(beforeCaption, ym + 1)
}
);
// ...caption... |
// -------
e.Graphics.DrawLines(new Pen(SystemColors.ControlLightLight, 1),
new Point[] {
new Point(afterCaption, ym + 1),
new Point(this.Width, ym + 1),
new Point(this.Width, ym)
}
);
}
else if (_LineOrientation == LineOrientation.Vertical)
{
System.Drawing.Drawing2D.HatchBrush aHatchBrush = new
System.Drawing.Drawing2D.HatchBrush
(System.Drawing.Drawing2D.HatchStyle.Vertical,
SystemColors.ControlLightLight,
SystemColors.ControlDark);
Pen myPen = new Pen(aHatchBrush, 2);
e.Graphics.DrawLine(myPen, beforeCaption / 2, 0, beforeCaption / 2, ClientRectangle.Height);
myPen.Dispose(); aHatchBrush.Dispose();
}
// Render caption
if (Caption != "")
{
switch (_LineOrientation)
{
case LineOrientation.Horizontal:
e.Graphics.FillRectangle(new SolidBrush(BackColor),
beforeCaption + CaptionPadding, 1,
e.Graphics.MeasureString(Caption, this.Font).Width,
e.Graphics.MeasureString(Caption, this.Font).Height
);
e.Graphics.DrawString(Caption, this.Font, new SolidBrush(this.ForeColor), beforeCaption + CaptionPadding, 1);
break;
case LineOrientation.Vertical:
e.Graphics.FillRectangle(new SolidBrush(BackColor),
0, beforeCaption + CaptionPadding,
e.Graphics.MeasureString(Caption, this.Font).Height,
e.Graphics.MeasureString(Caption, this.Font).Width
);
e.Graphics.DrawString(Caption, this.Font, new SolidBrush(this.ForeColor), 0, beforeCaption + CaptionPadding, new StringFormat(StringFormatFlags.DirectionVertical));
break;
}
}
// e.Graphics.DrawLines(new Pen(Color.Red, 1),
// new Point[] {
// new Point(0, 0),
// new Point(this.Width-1, 0),
// new Point(this.Width-1, this.Height-1),
// new Point(0, this.Height-1),
// new Point(0, 0)
// }
// );
}
protected override void OnResize(System.EventArgs e)
{
base.OnResize(e);
switch (_LineOrientation)
{
case LineOrientation.Horizontal:
this.Height = this.Font.Height + 2;
break;
case LineOrientation.Vertical:
this.Width = this.Font.Height + 4;
break;
}
this.Invalidate();
}
protected override void OnFontChanged(System.EventArgs e)
{
this.OnResize(e);
base.OnFontChanged(e);
}
}
public enum LineOrientation
{
Horizontal,
Vertical
}
public enum LineVerticalAlign
{
Top,
Middle,
Bottom
}
public enum CaptionOrizontalAlign
{
Left,
Center,
Right
}
}