-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCommandButton.cs
249 lines (215 loc) · 8.68 KB
/
CommandButton.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Text;
using System.Windows.Forms;
namespace PSTaskDialog
{
public partial class CommandButton : Button
{
//--------------------------------------------------------------------------------
#region PRIVATE MEMBERS
//--------------------------------------------------------------------------------
Image imgArrow1 = null;
Image imgArrow2 = null;
const int LEFT_MARGIN = 10;
const int TOP_MARGIN = 10;
const int ARROW_WIDTH = 19;
enum eButtonState { Normal, MouseOver, Down }
eButtonState m_State = eButtonState.Normal;
#endregion
//--------------------------------------------------------------------------------
#region PUBLIC PROPERTIES
//--------------------------------------------------------------------------------
// Override this to make sure the control is invalidated (repainted) when 'Text' is changed
public override string Text
{
get { return base.Text; }
set
{
base.Text = value;
if (m_autoHeight)
this.Height = GetBestHeight();
this.Invalidate();
}
}
// SmallFont is the font used for secondary lines
Font m_smallFont;
public Font SmallFont { get { return m_smallFont; } set { m_smallFont = value; } }
// AutoHeight determines whether the button automatically resizes itself to fit the Text
bool m_autoHeight = true;
[Browsable(true)]
[Category("Behavior")]
[DefaultValue(true)]
public bool AutoHeight { get { return m_autoHeight; } set { m_autoHeight = value; if (m_autoHeight) this.Invalidate(); } }
#endregion
//--------------------------------------------------------------------------------
#region CONSTRUCTOR
//--------------------------------------------------------------------------------
public CommandButton()
{
InitializeComponent();
base.Font = new Font("Arial", 11.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, (byte)0);
m_smallFont = new Font("Arial", 8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, (byte)0);
}
#endregion
//--------------------------------------------------------------------------------
#region PUBLIC ROUTINES
//--------------------------------------------------------------------------------
public int GetBestHeight()
{
return (TOP_MARGIN * 2) + (int)GetSmallTextSizeF().Height + (int)GetLargeTextSizeF().Height;
}
#endregion
//--------------------------------------------------------------------------------
#region PRIVATE ROUTINES
//--------------------------------------------------------------------------------
string GetLargeText()
{
string[] lines = this.Text.Split(new char[] { '\n' });
return lines[0];
}
string GetSmallText()
{
if (this.Text.IndexOf('\n') < 0)
return "";
string s = this.Text;
string[] lines = s.Split(new char[] { '\n' });
s = "";
for (int i = 1; i < lines.Length; i++)
s += lines[i] + "\n";
return s.Trim(new char[] { '\n' });
}
SizeF GetLargeTextSizeF()
{
int x = LEFT_MARGIN + ARROW_WIDTH + 5;
SizeF mzSize = new SizeF(this.Width - x - LEFT_MARGIN, 5000.0F); // presume RIGHT_MARGIN = LEFT_MARGIN
Graphics g = Graphics.FromHwnd(this.Handle);
SizeF textSize = g.MeasureString(GetLargeText(), base.Font, mzSize);
return textSize;
}
SizeF GetSmallTextSizeF()
{
string s = GetSmallText();
if (s == "") return new SizeF(0, 0);
int x = LEFT_MARGIN + ARROW_WIDTH + 8; // <- indent small text slightly more
SizeF mzSize = new SizeF(this.Width - x - LEFT_MARGIN, 5000.0F); // presume RIGHT_MARGIN = LEFT_MARGIN
Graphics g = Graphics.FromHwnd(this.Handle);
SizeF textSize = g.MeasureString(s, m_smallFont, mzSize);
return textSize;
}
#endregion
//--------------------------------------------------------------------------------
#region OVERRIDEs
//--------------------------------------------------------------------------------
protected override void OnCreateControl()
{
base.OnCreateControl();
imgArrow1 = new Bitmap(this.GetType(), "green_arrow1.png");
imgArrow2 = new Bitmap(this.GetType(), "green_arrow2.png");
}
//--------------------------------------------------------------------------------
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
LinearGradientBrush brush;
LinearGradientMode mode = LinearGradientMode.Vertical;
Rectangle newRect = new Rectangle(ClientRectangle.X, ClientRectangle.Y, ClientRectangle.Width - 1, ClientRectangle.Height - 1);
Color text_color = SystemColors.WindowText;
Image img = imgArrow1;
if (Enabled)
{
switch (m_State)
{
case eButtonState.Normal:
e.Graphics.FillRectangle(Brushes.White, newRect);
if (base.Focused)
e.Graphics.DrawRectangle(new Pen(Color.SkyBlue, 1), newRect);
else
e.Graphics.DrawRectangle(new Pen(Color.White, 1), newRect);
text_color = Color.DarkBlue;
break;
case eButtonState.MouseOver:
brush = new LinearGradientBrush(newRect, Color.White, Color.WhiteSmoke, mode);
e.Graphics.FillRectangle(brush, newRect);
e.Graphics.DrawRectangle(new Pen(Color.Silver, 1), newRect);
img = imgArrow2;
text_color = Color.Blue;
break;
case eButtonState.Down:
brush = new LinearGradientBrush(newRect, Color.WhiteSmoke, Color.White, mode);
e.Graphics.FillRectangle(brush, newRect);
e.Graphics.DrawRectangle(new Pen(Color.DarkGray, 1), newRect);
text_color = Color.DarkBlue;
break;
}
}
else
{
brush = new LinearGradientBrush(newRect, Color.WhiteSmoke, Color.Gainsboro, mode);
e.Graphics.FillRectangle(brush, newRect);
e.Graphics.DrawRectangle(new Pen(Color.DarkGray, 1), newRect);
text_color = Color.DarkBlue;
}
string largetext = this.GetLargeText();
string smalltext = this.GetSmallText();
SizeF szL = GetLargeTextSizeF();
//e.Graphics.DrawString(largetext, base.Font, new SolidBrush(text_color), new RectangleF(new PointF(LEFT_MARGIN + imgArrow1.Width + 5, TOP_MARGIN), szL));
TextRenderer.DrawText(e.Graphics, largetext, base.Font, new Rectangle(LEFT_MARGIN + imgArrow1.Width + 5, TOP_MARGIN, (int)szL.Width, (int)szL.Height), text_color, TextFormatFlags.Default);
if (smalltext != "")
{
SizeF szS = GetSmallTextSizeF();
e.Graphics.DrawString(smalltext, m_smallFont, new SolidBrush(text_color), new RectangleF(new PointF(LEFT_MARGIN + imgArrow1.Width + 8, TOP_MARGIN + (int)szL.Height), szS));
}
e.Graphics.DrawImage(img, new Point(LEFT_MARGIN, TOP_MARGIN + (int)(szL.Height / 2) - (int)(img.Height / 2)));
}
//--------------------------------------------------------------------------------
protected override void OnMouseLeave(System.EventArgs e)
{
m_State = eButtonState.Normal;
this.Invalidate();
base.OnMouseLeave(e);
}
//--------------------------------------------------------------------------------
protected override void OnMouseEnter(System.EventArgs e)
{
m_State = eButtonState.MouseOver;
this.Invalidate();
base.OnMouseEnter(e);
}
//--------------------------------------------------------------------------------
protected override void OnMouseUp(System.Windows.Forms.MouseEventArgs e)
{
m_State = eButtonState.MouseOver;
this.Invalidate();
base.OnMouseUp(e);
}
//--------------------------------------------------------------------------------
protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e)
{
m_State = eButtonState.Down;
this.Invalidate();
base.OnMouseDown(e);
}
//--------------------------------------------------------------------------------
protected override void OnSizeChanged(EventArgs e)
{
if (m_autoHeight)
{
int h = GetBestHeight();
if (this.Height != h)
{
this.Height = h;
return;
}
}
base.OnSizeChanged(e);
}
#endregion
//--------------------------------------------------------------------------------
}
}