-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCommandLink.cs
298 lines (266 loc) · 8.05 KB
/
CommandLink.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
using System;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace TaskSchedulerConfig
{
internal class CommandLink : Button
{
private const int BCM_SETNOTE = 0x00001609;
private const int BCM_SETSHIELD = 0x0000160C;
private const int BS_COMMANDLINK = 0x0000000E;
private Color activeTextColor;
private string description;
private Label lblDescription;
private Label lblText;
private bool mActivated;
private DisplayStyle mDisplayStyle = DisplayStyle.Arrow;
private bool mMouseOver;
private PictureBox picIcon;
private string text;
private Color textColor;
public CommandLink()
{
if (IsVistaOrLater)
{
FlatStyle = FlatStyle.System;
}
else
{
BackColor = Parent?.BackColor ?? Color.White;
FlatStyle = FlatStyle.Standard;
Size = new Size(400, 72);
TabStop = false;
UseVisualStyleBackColor = false;
textColor = Color.FromArgb(21, 28, 85);
activeTextColor = Color.FromArgb(7, 74, 229);
lblText = new Label { AutoSize = true, BackColor = Color.Transparent, ForeColor = textColor, Location = new Point(27, 10), Name = "lblText", Size = new Size(0, 21), TabIndex = 0 };
lblText.Font = new Font("Segoe UI", 12f, System.Drawing.FontStyle.Regular, GraphicsUnit.Point, (byte)0);
lblText.ForeColor = Color.FromArgb((int)(byte)21, (int)(byte)28, (int)(byte)85);
lblText.Click += (o, e) => OnClick(e);
lblText.MouseLeave += CommandLink_MouseLeave;
Controls.Add(lblText);
lblDescription = new Label { BackColor = Color.Transparent, ForeColor = textColor, Location = new Point(33, 36), Name = "lblDescription", Size = new Size(364, 32), TabIndex = 1, UseCompatibleTextRendering = true };
lblDescription.Font = new Font("Segoe UI", 9f, System.Drawing.FontStyle.Regular, GraphicsUnit.Point, (byte)0);
lblDescription.ForeColor = Color.FromArgb((int)(byte)21, (int)(byte)28, (int)(byte)85);
lblDescription.Click += (o, e) => OnClick(e);
lblDescription.MouseLeave += CommandLink_MouseLeave;
Controls.Add(lblDescription);
picIcon = new PictureBox { BackColor = Color.Transparent, Location = new Point(10, 13), Name = "picIcon", Size = new Size(16, 16), TabIndex = 2, TabStop = false };
picIcon.Image = Properties.Resources.restarrow;
picIcon.Click += (o, e) => OnClick(e);
picIcon.MouseLeave += CommandLink_MouseLeave;
Controls.Add(picIcon);
Click += (o, e) => Selected?.Invoke(this, EventArgs.Empty);
MouseEnter += (o, e) => MouseOver = true;
MouseLeave += CommandLink_MouseLeave;
MouseOver = false;
Activated = false;
}
}
public CommandLink(DisplayStyle style, string text = null, string description = null) : this()
{
Style = style;
Text = text;
Description = description;
}
public event EventHandler Selected;
public enum DisplayStyle
{
Arrow,
Shield,
}
[Bindable(true)]
[DefaultValue(null)]
public string Description
{
get { return description; }
set
{
if (IsVistaOrLater)
SendMessage(Handle, BCM_SETNOTE, IntPtr.Zero, value);
else
lblDescription.Text = value;
description = value;
}
}
[Bindable(true)]
[DefaultValue(DisplayStyle.Arrow)]
public DisplayStyle Style
{
get { return mDisplayStyle; }
set
{
bool bInv = false;
if (mDisplayStyle != value)
{
bInv = true;
}
mDisplayStyle = value;
if (bInv)
{
if (IsVistaOrLater)
{
if (mDisplayStyle == DisplayStyle.Shield)
SendMessage(Handle, BCM_SETSHIELD, 0, 1);
}
else
{
base.FlatStyle = FlatStyle.Standard;
switch (mDisplayStyle)
{
case DisplayStyle.Arrow:
picIcon.Image = mMouseOver ? Properties.Resources.selarrow : Properties.Resources.restarrow;
break;
case DisplayStyle.Shield:
picIcon.Image = Properties.Resources.shield;
break;
default:
picIcon.Image = null;
break;
}
Invalidate();
}
}
}
}
[Bindable(true)]
[DefaultValue(null)]
public override string Text
{
get { return text; }
set
{
if (IsVistaOrLater)
base.Text = value;
else
lblText.Text = value;
text = value;
}
}
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
if (IsVistaOrLater)
cp.Style |= BS_COMMANDLINK;
return cp;
}
}
private bool Activated
{
get { return mActivated; }
set
{
bool bInv = false;
if (mActivated != value)
bInv = true;
mActivated = value;
if (bInv)
Invalidate();
}
}
private bool Default => (object.ReferenceEquals(FindForm().AcceptButton, this));
private static bool IsVistaOrLater => System.Environment.OSVersion.Version.Major > 5;
private bool MouseOver
{
get { return mMouseOver; }
set
{
bool bInv = false;
if (mMouseOver != value)
bInv = true;
mMouseOver = value;
if (!IsVistaOrLater && bInv)
{
if (mMouseOver == true)
{
lblText.ForeColor = activeTextColor;
lblDescription.ForeColor = activeTextColor;
if (Style == DisplayStyle.Arrow)
picIcon.Image = Properties.Resources.selarrow;
}
else
{
lblText.ForeColor = textColor;
lblDescription.ForeColor = textColor;
if (Style == DisplayStyle.Arrow)
{
picIcon.Image = Properties.Resources.restarrow;
}
}
Invalidate();
}
}
}
public void ActivateChanged(bool activate)
{
Activated = activate;
}
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
if (IsVistaOrLater)
{
base.OnPaint(e);
}
else
{
if (base.FlatStyle != FlatStyle.Standard)
base.FlatStyle = FlatStyle.Standard;
Pen oPen = null;
Rectangle r = ClientRectangle;
Brush oBrush = null;
r.Width -= 1;
r.Height -= 1;
if (MouseOver)
{
//the mouse is over is, draw the hover border
oPen = new Pen(Color.FromArgb(198, 198, 198));
oBrush = new System.Drawing.Drawing2D.LinearGradientBrush(ClientRectangle, BackColor, Color.FromArgb(246, 246, 246), System.Drawing.Drawing2D.LinearGradientMode.Vertical);
}
else if (Activated && Default)
{
//draw the blue border
oPen = new Pen(Color.FromArgb(198, 244, 255));
r.Width -= 2;
r.Height -= 2;
r.X += 1;
r.Y += 1;
}
if (oBrush == null)
{
e.Graphics.FillRectangle(new SolidBrush(BackColor), ClientRectangle);
}
else
{
e.Graphics.FillRectangle(oBrush, ClientRectangle);
oBrush.Dispose();
}
if (oPen != null)
{
oPen.LineJoin = System.Drawing.Drawing2D.LineJoin.Round;
e.Graphics.DrawLine(oPen, r.X + 2, r.Y, r.X + r.Width - 2, r.Y);
e.Graphics.DrawLine(oPen, r.X + r.Width - 2, r.Y, r.X + r.Width, r.Y + 2);
e.Graphics.DrawLine(oPen, r.X + r.Width, r.Y + 2, r.X + r.Width, r.Y + r.Height - 2);
e.Graphics.DrawLine(oPen, r.X + r.Width, r.Y + r.Height - 2, r.X + r.Width - 2, r.Y + r.Height);
e.Graphics.DrawLine(oPen, r.X + r.Width - 2, r.Y + r.Height, r.X + 2, r.Y + r.Height);
e.Graphics.DrawLine(oPen, r.X + 2, r.Y + r.Height, r.X, r.Y + r.Height - 2);
e.Graphics.DrawLine(oPen, r.X, r.Y + r.Height - 2, r.X, r.Y + 2);
e.Graphics.DrawLine(oPen, r.X, r.Y + 2, r.X + 2, r.Y);
oPen.Dispose();
}
}
}
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
private static extern int SendMessage(IntPtr hWnd, UInt32 Msg, int wParam, int lParam);
[DllImport("user32", CharSet = CharSet.Unicode)]
private static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, string lParam);
private void CommandLink_MouseLeave(object sender, System.EventArgs e)
{
MouseOver = ClientRectangle.Contains(PointToClient(Control.MousePosition));
Invalidate();
}
}
}