RibbonButton.cs
12.9 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
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Drawing.Imaging;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using RibbonStyle;
namespace RibbonStyle
{
public partial class RibbonButton : System.Windows.Forms.Button
{
//Timer
private System.Windows.Forms.Timer timer1 = new System.Windows.Forms.Timer();
private System.Windows.Forms.Timer timer2 = new System.Windows.Forms.Timer();
//Images
private Image _img_on;
private Image _img_click;
private Image _img_back;
private Image _img;
private Image _img_fad;
private String s_folder;
private String s_filename;
private String _infotitle = "";
private String _infocomment = "";
private String _infoimage = "";
private Color _infocolor = Color.FromArgb(201, 217, 239);
private Color _TextColor = Color.Black;
private ContentAlignment _TextAlign = ContentAlignment.MiddleRight;
private ContentAlignment _ImageAlign = ContentAlignment.MiddleLeft;
private Image _toshow;
//Fading
bool b_fad = false;
int i_fad = 0; //0 nothing, 1 entering, 2 leaving
int i_value = 255; //Level of transparency
//InfoForm
InfoForm info;
//Constructor
public RibbonButton()
{
this.SetStyle(ControlStyles.ResizeRedraw |
ControlStyles.SupportsTransparentBackColor |
ControlStyles.UserPaint |
ControlStyles.AllPaintingInWmPaint |
ControlStyles.DoubleBuffer, true);
this.BackColor = Color.Transparent;
this.FlatStyle = System.Windows.Forms.FlatStyle.Standard;
this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.None;
this.FlatAppearance.BorderSize = 0;
this.TextAlign = ContentAlignment.MiddleRight;
this.ImageAlign = ContentAlignment.MiddleLeft;
this.FlatAppearance.BorderColor = Color.FromArgb(100, 255, 255, 255);
this.FlatAppearance.MouseDownBackColor = Color.FromArgb(100, 255, 255, 255);
this.FlatAppearance.MouseOverBackColor = Color.FromArgb(100, 255, 255, 255);
this._toshow = this._img_back;
timer1.Interval = 10;
timer1.Tick += new EventHandler(timer1_Tick);
timer2.Interval = 10;
timer2.Tick += new EventHandler(timer2_Tick);
}
//Properties
public ContentAlignment textAlign
{
get { return _TextAlign; }
set
{
_TextAlign = value;
this.TextAlign = _TextAlign;
}
}
public ContentAlignment imageAlign
{
get { return _ImageAlign; }
set
{
_ImageAlign = value;
this.ImageAlign = _ImageAlign;
}
}
public Image img_on
{
get { return _img_on; }
set { _img_on = value; }
}
public Image img_click
{
get { return _img_click; }
set { _img_click = value; }
}
public Image img_back
{
get { return _img_back; }
set
{
_img_back = value;
}
}
public Image img
{
get { return _img; }
set
{
_img = value;
this.Image = _img;
}
}
public string folder
{
get { return s_folder; }
set
{
if (value != null)
{
if ((char)value[value.Length - 1] != '\\')
{
s_folder = value + "\\";
}
else
{
s_folder = value;
}
}
}
}
public string filename
{
get { return s_filename; }
set
{
s_filename = value;
if (s_folder != null & s_filename != null)
{
_img = Image.FromFile(s_folder + s_filename);
this.Image = _img;
}
}
}
public string InfoTitle
{
get { return _infotitle; }
set
{
_infotitle = value;
}
}
public string InfoComment
{
get { return _infocomment; }
set
{
_infocomment = value;
}
}
public string InfoImage
{
get { return _infoimage; }
set
{
_infoimage = value;
}
}
public Color InfoColor
{
get { return _infocolor; }
set { _infocolor = value; }
}
//Methods
public void PaintBackground()
{
if (b_fad)
{
object _img_temp = new object();
if (i_fad == 1)
{
_img_temp = _img_on.Clone();
}
else if (i_fad == 2)
{
_img_temp = _img_back.Clone();
}
_img_fad = (Image)_img_temp;
Graphics _grf = Graphics.FromImage(_img_fad);
SolidBrush brocha = new SolidBrush(Color.FromArgb(i_value, 255, 255, 255));
_grf.FillRectangle(brocha, 0, 0, _img_fad.Width, _img_fad.Height);
this.BackgroundImage = _img_fad;
}
}
int t = 0, t_end = 100;
private void timer2_Tick(object sender, EventArgs e)
{
if (t < t_end)
{
t++;
}
else
{
timer2.Stop();
t = 0;
ShowInfo();
}
}
private void timer1_Tick(object sender, EventArgs e)
{
switch (i_fad)
{
case 1:
{
if (i_value == 0)
{
i_value = 255;
}
if (i_value > -1)
{
PaintBackground();
i_value -= 10;
}
else
{
i_value = 0;
PaintBackground();
timer1.Stop();
}
break;
}
case 2:
{
if (i_value == 0)
{
i_value = 255;
}
if (i_value > -1)
{
PaintBackground();
i_value -= 10;
}
else
{
i_value = 0;
PaintBackground();
timer1.Stop();
}
break;
}
}
}
protected override void OnMouseEnter(EventArgs e)
{
if (b_fad)
{
i_fad = 1;
timer1.Start();
}
else
{
this.BackgroundImage = _img_on;
_toshow = _img_on;
}
base.OnMouseEnter(e);
}
protected override void OnMouseLeave(EventArgs e)
{
if (b_fad) { i_fad = 2; timer1.Start(); }
else
{
this.BackgroundImage = _img_back;
_toshow = _img_back;
}
//Close the info form
if (info != null)
{
info.Close();
}
timer2.Stop();
base.OnMouseLeave(e);
}
protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs mevent)
{
this.BackgroundImage = _img_click;
this._toshow = _img_click;
base.OnMouseDown(mevent);
}
protected override void OnMouseUp(System.Windows.Forms.MouseEventArgs mevent)
{
this.BackgroundImage = _img_on;
this._toshow = _img_on;
base.OnMouseUp(mevent);
}
public enum Side { UpLeft, UpRight, DownLeft, DownRight }
protected override void OnMouseHover(EventArgs e)
{
timer2.Start();
base.OnMouseHover(e);
}
public void ShowInfo()
{
info = new InfoForm();
info.Title = _infotitle;
info.Comment = _infocomment;
info.Picture = _infoimage;
info.FillColor = _infocolor;
if (GetInfoLocation() == Side.UpLeft)
{
info.Location = new Point(Cursor.Position.X, Application.OpenForms[0].Location.Y + this.Bottom + 80);
}
else
{
info.Location = new Point(Cursor.Position.X - info.Width, Application.OpenForms[0].Location.Y + this.Bottom + 80);
}
info.Show();
}
public Side GetInfoLocation()
{
int CPX = Cursor.Position.X - Application.OpenForms[0].Location.X;
int HSX = Application.OpenForms[0].Width / 2;
if (CPX < HSX)
{
return Side.UpLeft;
}
else
{
return Side.UpRight;
}
}
protected override void OnPaintBackground(PaintEventArgs pevent)
{
//para pintar el fondo hay que tener en cuenta el
//desplazamiento con el control contenedor
if (this.Parent != null)
{
GraphicsContainer cstate = pevent.Graphics.BeginContainer();
pevent.Graphics.TranslateTransform(-this.Left, -this.Top);
Rectangle clip = pevent.ClipRectangle;
clip.Offset(this.Left, this.Top);
PaintEventArgs pe = new PaintEventArgs(pevent.Graphics, clip);
//pinta el fondo del contenedor
InvokePaintBackground(this.Parent, pe);
//pinta el resto del contenedor
InvokePaint(this.Parent, pe);
//restaura el Graphics a su estado original
pevent.Graphics.EndContainer(cstate);
}
else
base.OnPaintBackground(pevent);
}
protected override void OnPaint(PaintEventArgs pevent)
{
if (this.Parent != null)
{
GraphicsContainer cstate = pevent.Graphics.BeginContainer();
pevent.Graphics.TranslateTransform(-this.Left, -this.Top);
Rectangle clip = pevent.ClipRectangle;
clip.Offset(this.Left, this.Top);
PaintEventArgs pe = new PaintEventArgs(pevent.Graphics, clip);
//pinta el fondo del contenedor
// InvokePaintBackground(this.Parent, pe);
//pinta el resto del contenedor
InvokePaint(this.Parent, pe);
//restaura el Graphics a su estado original
pevent.Graphics.EndContainer(cstate);
Graphics g = pevent.Graphics;
try { g.DrawImage(_toshow, pevent.ClipRectangle); }
catch { }
Rectangle rect = pevent.ClipRectangle;
int X = 4;
try
{
int newwidth = (rect.Width - 8);
int newheigth = newwidth * _img.Height / _img.Width;
Point _imgpos = new Point(X, 4);
Rectangle r = new Rectangle(_imgpos, new Size(newwidth, newheigth));
//g.DrawImageUnscaled(_img, _imgpos);
g.DrawImage(_img, r);
}
catch { }
SizeF textsize = g.MeasureString(this.Text, this.Font);
X = rect.X + rect.Width; X = (X - (int)textsize.Width) / 2;
int Y = rect.Y + rect.Height; Y = Y - (int)textsize.Height - 2;
Point _textpos = new Point(X, Y);
Pen PForeColor = new Pen(this.ForeColor);
//g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
g.DrawString(this.Text, this.Font, PForeColor.Brush, _textpos);
}
else
base.OnPaint(pevent);
}
}
}