-
Notifications
You must be signed in to change notification settings - Fork 0
/
Class Xbutton.cs
executable file
·512 lines (424 loc) · 17.4 KB
/
Class Xbutton.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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
namespace ManiXButton
{
public partial class XButton : Button
{
#region Fields
private ManiXButton.Theme thm = ManiXButton.Theme.MSOffice2010_BLUE;
private enum MouseState { None = 1, Down = 2, Up = 3, Enter = 4, Leave = 5, Move = 6 }
private MouseState MState = MouseState.None;
#endregion
#region Constructor
public XButton()
{
this.SetStyle(ControlStyles.SupportsTransparentBackColor |
ControlStyles.Opaque |
ControlStyles.ResizeRedraw |
ControlStyles.OptimizedDoubleBuffer |
ControlStyles.CacheText, // We gain about 2% in painting by avoiding extra GetWindowText calls
true);
this.colorTable = new Colortable();
this.MouseLeave += new EventHandler(_MouseLeave);
this.MouseDown += new MouseEventHandler(_MouseDown);
this.MouseUp += new MouseEventHandler(_MouseUp);
this.MouseMove += new MouseEventHandler(_MouseMove);
}
#endregion
#region Events
#region Paint Transparent Background
protected void PaintTransparentBackground(Graphics g, Rectangle clipRect)
{
// check if we have a parent
if (this.Parent != null)
{
// convert the clipRects coordinates from ours to our parents
clipRect.Offset(this.Location);
PaintEventArgs e = new PaintEventArgs(g, clipRect);
// save the graphics state so that if anything goes wrong
// we're not fubar
GraphicsState state = g.Save();
try
{
// move the graphics object so that we are drawing in
// the correct place
g.TranslateTransform((float)-this.Location.X, (float)-this.Location.Y);
// draw the parents background and foreground
this.InvokePaintBackground(this.Parent, e);
this.InvokePaint(this.Parent, e);
return;
}
finally
{
// reset everything back to where they were before
g.Restore(state);
clipRect.Offset(-this.Location.X, -this.Location.Y);
}
}
// we don't have a parent, so fill the rect with
// the default control color
g.FillRectangle(SystemBrushes.Control, clipRect);
}
#endregion
#region Mouse Events
private void _MouseDown(object sender, MouseEventArgs mevent)
{
MState = MouseState.Down;
Invalidate();
}
private void _MouseUp(object sender, MouseEventArgs mevent)
{
MState = MouseState.Up;
Invalidate();
}
private void _MouseMove(object sender, MouseEventArgs mevent)
{
MState = MouseState.Move;
Invalidate();
}
private void _MouseLeave(object sender, EventArgs e)
{
MState = MouseState.Leave;
Invalidate();
}
#endregion
#region Path
public static GraphicsPath GetRoundedRect(RectangleF r, float radius)
{
GraphicsPath gp = new GraphicsPath();
gp.StartFigure();
r = new RectangleF(r.Left, r.Top, r.Width, r.Height);
if (radius <= 0.0F || radius <= 0.0F)
{
gp.AddRectangle(r);
}
else
{
gp.AddArc((float)r.X, (float)r.Y, radius, radius, 180, 90);
gp.AddArc((float)r.Right - radius, (float)r.Y, radius - 1, radius, 270, 90);
gp.AddArc((float)r.Right - radius, ((float)r.Bottom) - radius - 1, radius - 1, radius, 0, 90);
gp.AddArc((float)r.X, ((float)r.Bottom) - radius - 1, radius - 1, radius, 90, 90);
}
gp.CloseFigure();
return gp;
}
#endregion
protected override void OnPaint(PaintEventArgs e)
{
this.PaintTransparentBackground(e.Graphics, this.ClientRectangle);
#region Painting
//now let's we begin painting
Graphics g = e.Graphics;
g.SmoothingMode = SmoothingMode.HighQuality;
g.SmoothingMode = SmoothingMode.AntiAlias;
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
#endregion
#region Color
Color tTopColorBegin = this.colorTable.ButtonNormalColor1;
Color tTopColorEnd = this.colorTable.ButtonNormalColor2;
Color tBottomColorBegin = this.colorTable.ButtonNormalColor3;
Color tBottomColorEnd = this.colorTable.ButtonNormalColor4;
Color Textcol = this.colorTable.TextColor;
if (!this.Enabled)
{
tTopColorBegin = Color.FromArgb((int)(tTopColorBegin.GetBrightness() * 255),
(int)(tTopColorBegin.GetBrightness() * 255),
(int)(tTopColorBegin.GetBrightness() * 255));
tBottomColorEnd = Color.FromArgb((int)(tBottomColorEnd.GetBrightness() * 255),
(int)(tBottomColorEnd.GetBrightness() * 255),
(int)(tBottomColorEnd.GetBrightness() * 255));
}
else
{
if (MState == MouseState.None || MState == MouseState.Leave)
{
tTopColorBegin = this.colorTable.ButtonNormalColor1;
tTopColorEnd = this.colorTable.ButtonNormalColor2;
tBottomColorBegin = this.colorTable.ButtonNormalColor3;
tBottomColorEnd = this.colorTable.ButtonNormalColor4;
Textcol = this.colorTable.TextColor;
}
else if (MState == MouseState.Down)
{
tTopColorBegin = this.colorTable.ButtonSelectedColor1;
tTopColorEnd = this.colorTable.ButtonSelectedColor2;
tBottomColorBegin = this.colorTable.ButtonSelectedColor3;
tBottomColorEnd = this.colorTable.ButtonSelectedColor4;
Textcol = this.colorTable.SelectedTextColor;
}
else if (MState == MouseState.Move || MState == MouseState.Up)
{
tTopColorBegin = this.colorTable.ButtonMouseOverColor1;
tTopColorEnd = this.colorTable.ButtonMouseOverColor2;
tBottomColorBegin = this.colorTable.ButtonMouseOverColor3;
tBottomColorEnd = this.colorTable.ButtonMouseOverColor4;
Textcol = this.colorTable.HoverTextColor;
}
}
#endregion
#region Theme 2010
if (thm == ManiXButton.Theme.MSOffice2010_BLUE || thm == ManiXButton.Theme.MSOffice2010_Green || thm == ManiXButton.Theme.MSOffice2010_Yellow || thm == ManiXButton.Theme.MSOffice2010_Publisher ||
thm == ManiXButton.Theme.MSOffice2010_RED || thm == ManiXButton.Theme.MSOffice2010_WHITE || thm == ManiXButton.Theme.MSOffice2010_Pink)
{
Paint2010Background(e, g, tTopColorBegin, tTopColorEnd, tBottomColorBegin, tBottomColorEnd);
TEXTandIMAGE(this.ClientRectangle, g, Textcol);
}
#endregion
}
#region Paint 2010 Background
protected void Paint2010Background(PaintEventArgs e, Graphics g, Color tTopColorBegin, Color tTopColorEnd, Color tBottomColorBegin, Color tBottomColorEnd)
{
int _roundedRadiusX = 6;
Rectangle r = new Rectangle(this.ClientRectangle.Left, this.ClientRectangle.Top, this.ClientRectangle.Width, this.ClientRectangle.Height);
Rectangle j = r;
Rectangle r2 = r;
r2.Inflate(-1, -1);
Rectangle r3 = r2;
r3.Inflate(-1, -1);
//rectangle for gradient, half upper and lower
RectangleF halfup = new RectangleF(r.Left, r.Top, r.Width, r.Height);
RectangleF halfdown = new RectangleF(r.Left, r.Top + (r.Height / 2) - 1, r.Width, r.Height);
//BEGIN PAINT BACKGROUND
//for half upper, we paint using linear gradient
using (GraphicsPath thePath = GetRoundedRect(r, _roundedRadiusX))
{
LinearGradientBrush lgb = new LinearGradientBrush(halfup, tBottomColorEnd, tBottomColorBegin, 90f, true);
Blend blend = new Blend(4);
blend.Positions = new float[] { 0, 0.18f, 0.35f, 1f };
blend.Factors = new float[] { 0f, .4f, .9f, 1f };
lgb.Blend = blend;
g.FillPath(lgb, thePath);
lgb.Dispose();
//for half lower, we paint using radial gradient
using (GraphicsPath p = new GraphicsPath())
{
p.AddEllipse(halfdown); //make it radial
using (PathGradientBrush gradient = new PathGradientBrush(p))
{
gradient.WrapMode = WrapMode.Clamp;
gradient.CenterPoint = new PointF(Convert.ToSingle(halfdown.Left + halfdown.Width / 2), Convert.ToSingle(halfdown.Bottom));
gradient.CenterColor = tBottomColorEnd;
gradient.SurroundColors = new Color[] { tBottomColorBegin };
blend = new Blend(4);
blend.Positions = new float[] { 0, 0.15f, 0.4f, 1f };
blend.Factors = new float[] { 0f, .3f, 1f, 1f };
gradient.Blend = blend;
g.FillPath(gradient, thePath);
}
}
//END PAINT BACKGROUND
//BEGIN PAINT BORDERS
using (GraphicsPath gborderDark = thePath)
{
using (Pen p = new Pen(tTopColorBegin, 1))
{
g.DrawPath(p, gborderDark);
}
}
using (GraphicsPath gborderLight = GetRoundedRect(r2, _roundedRadiusX))
{
using (Pen p = new Pen(tTopColorEnd, 1))
{
g.DrawPath(p, gborderLight);
}
}
using (GraphicsPath gborderMed = GetRoundedRect(r3, _roundedRadiusX))
{
SolidBrush bordermed = new SolidBrush(Color.FromArgb(50, tTopColorEnd));
using (Pen p = new Pen(bordermed, 1))
{
g.DrawPath(p, gborderMed);
}
}
//END PAINT BORDERS
}
}
#endregion
#region Paint TEXT AND IMAGE
protected void TEXTandIMAGE(Rectangle Rec, Graphics g, Color textColor)
{
//BEGIN PAINT TEXT
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;
#region Top
if (this.TextAlign == ContentAlignment.TopLeft)
{
sf.LineAlignment = StringAlignment.Near;
sf.Alignment = StringAlignment.Near;
}
else if (this.TextAlign == ContentAlignment.TopCenter)
{
sf.LineAlignment = StringAlignment.Near;
sf.Alignment = StringAlignment.Center;
}
else if (this.TextAlign == ContentAlignment.TopRight)
{
sf.LineAlignment = StringAlignment.Near;
sf.Alignment = StringAlignment.Far;
}
#endregion
#region Middle
else if (this.TextAlign == ContentAlignment.MiddleLeft)
{
sf.LineAlignment = StringAlignment.Center;
sf.Alignment = StringAlignment.Near;
}
else if (this.TextAlign == ContentAlignment.MiddleCenter)
{
sf.LineAlignment = StringAlignment.Center;
sf.Alignment = StringAlignment.Center;
}
else if (this.TextAlign == ContentAlignment.MiddleRight)
{
sf.LineAlignment = StringAlignment.Center;
sf.Alignment = StringAlignment.Far;
}
#endregion
#region Bottom
else if (this.TextAlign == ContentAlignment.BottomLeft)
{
sf.LineAlignment = StringAlignment.Far;
sf.Alignment = StringAlignment.Near;
}
else if (this.TextAlign == ContentAlignment.BottomCenter)
{
sf.LineAlignment = StringAlignment.Far;
sf.Alignment = StringAlignment.Center;
}
else if (this.TextAlign == ContentAlignment.BottomRight)
{
sf.LineAlignment = StringAlignment.Far;
sf.Alignment = StringAlignment.Far;
}
#endregion
if (this.ShowKeyboardCues)
sf.HotkeyPrefix = System.Drawing.Text.HotkeyPrefix.Show;
else
sf.HotkeyPrefix = System.Drawing.Text.HotkeyPrefix.Hide;
g.DrawString(this.Text, this.Font, new SolidBrush(textColor), Rec, sf);
}
#endregion
#endregion
#region Properties
#region ColorTable
Colortable colorTable = null;
[DefaultValue(typeof(Colortable), "Office2010Blue")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public Colortable ColorTable
{
get
{
if (colorTable == null)
colorTable = new Colortable();
return colorTable;
}
set
{
if (value == null)
value = Colortable.Office2010Blue;
colorTable = (Colortable)value;
this.Invalidate();
}
}
public ManiXButton.Theme Theme
{
get
{
if (this.colorTable == Colortable.Office2010Green)
{
return ManiXButton.Theme.MSOffice2010_Green;
}
else if (this.colorTable == Colortable.Office2010Red)
{
return ManiXButton.Theme.MSOffice2010_RED;
}
else if (this.colorTable == Colortable.Office2010Pink)
{
return ManiXButton.Theme.MSOffice2010_Pink;
}
else if (this.colorTable == Colortable.Office2010Yellow)
{
return ManiXButton.Theme.MSOffice2010_Yellow;
}
else if (this.colorTable == Colortable.Office2010White)
{
return ManiXButton.Theme.MSOffice2010_WHITE;
}
else if (this.colorTable == Colortable.Office2010Publisher)
{
return ManiXButton.Theme.MSOffice2010_Publisher;
}
return ManiXButton.Theme.MSOffice2010_BLUE;
}
set
{
this.thm = value;
if (thm == ManiXButton.Theme.MSOffice2010_Green)
{
this.colorTable = Colortable.Office2010Green;
}
else if (thm == ManiXButton.Theme.MSOffice2010_RED)
{
this.colorTable = Colortable.Office2010Red;
}
else if (thm == ManiXButton.Theme.MSOffice2010_Pink)
{
this.colorTable = Colortable.Office2010Pink;
}
else if (thm == ManiXButton.Theme.MSOffice2010_WHITE)
{
this.colorTable = Colortable.Office2010White;
}
else if (thm == ManiXButton.Theme.MSOffice2010_Yellow)
{
this.colorTable = Colortable.Office2010Yellow;
}
else if (thm == ManiXButton.Theme.MSOffice2010_Publisher)
{
this.colorTable = Colortable.Office2010Publisher;
}
else
{
this.colorTable = Colortable.Office2010Blue;
}
}
}
#endregion
#region Background Image
[Browsable(false)]
public override Image BackgroundImage
{
get
{
return base.BackgroundImage;
}
set
{
base.BackgroundImage = value;
}
}
[Browsable(false)]
public override ImageLayout BackgroundImageLayout
{
get
{
return base.BackgroundImageLayout;
}
set
{
base.BackgroundImageLayout = value;
}
}
#endregion
private void InitializeComponent()
{
this.SuspendLayout();
this.ResumeLayout(false);
}
#endregion
}
}