-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSystemIconButton.cs
More file actions
93 lines (80 loc) · 4.1 KB
/
SystemIconButton.cs
File metadata and controls
93 lines (80 loc) · 4.1 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
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
namespace PersonalFolder {
[DefaultEvent("Click")]
class SystemIconButton : Control {
private Image icon;
public SystemIconButton () {
Width = 20;
Height = 20;
BackColor = Color.White;
}
private bool isHovered = false;
protected override void OnMouseEnter (EventArgs e) {
isHovered = true;
Refresh();
if (Text.Length > 0) {
new ToolTip().Show(Text, Form.ActiveForm, 8 + Location.X, 32 + Location.Y + Size.Height, 1200);
}
}
protected override void OnMouseLeave (EventArgs e) {
isHovered = false;
Refresh();
}
private bool isPressed = false;
protected override void OnMouseDown (MouseEventArgs e) {
isPressed = true;
Refresh();
}
public new event EventHandler Click = (sender, e) => { };
protected override void OnMouseUp (MouseEventArgs e) {
if (isPressed) {
Click(this, e);
}
isPressed = false;
Refresh();
}
private static Pen BORDER_HOVER_PEN = new Pen(Color.FromArgb(64, 0, 64, 160));
private static Brush BACK_HOVER_BRUSH = new SolidBrush(Color.FromArgb(32, 0, 64, 160));
private static Pen BORDER_PRESSED_PEN = new Pen(Color.FromArgb(72, 0, 64, 160));
private static Brush BACK_PRESSED_BRUSH = new SolidBrush(Color.FromArgb(46, 0, 64, 160));
protected override void OnPaint (PaintEventArgs e) {
if (isPressed) {
e.Graphics.FillRectangle(BACK_PRESSED_BRUSH, e.ClipRectangle);
e.Graphics.DrawLine(BORDER_PRESSED_PEN, e.ClipRectangle.Left, e.ClipRectangle.Top, e.ClipRectangle.Right - 1, e.ClipRectangle.Top);
e.Graphics.DrawLine(BORDER_PRESSED_PEN, e.ClipRectangle.Right - 1, e.ClipRectangle.Top, e.ClipRectangle.Right - 1, e.ClipRectangle.Bottom - 1);
e.Graphics.DrawLine(BORDER_PRESSED_PEN, e.ClipRectangle.Right - 1, e.ClipRectangle.Bottom - 1, e.ClipRectangle.Left, e.ClipRectangle.Bottom - 1);
e.Graphics.DrawLine(BORDER_PRESSED_PEN, e.ClipRectangle.Left, e.ClipRectangle.Bottom - 1, e.ClipRectangle.Left, e.ClipRectangle.Top);
} else if (isHovered) {
e.Graphics.FillRectangle(BACK_HOVER_BRUSH, e.ClipRectangle);
e.Graphics.DrawLine(BORDER_HOVER_PEN, e.ClipRectangle.Left, e.ClipRectangle.Top, e.ClipRectangle.Right - 1, e.ClipRectangle.Top);
e.Graphics.DrawLine(BORDER_HOVER_PEN, e.ClipRectangle.Right - 1, e.ClipRectangle.Top, e.ClipRectangle.Right - 1, e.ClipRectangle.Bottom - 1);
e.Graphics.DrawLine(BORDER_HOVER_PEN, e.ClipRectangle.Right - 1, e.ClipRectangle.Bottom - 1, e.ClipRectangle.Left, e.ClipRectangle.Bottom - 1);
e.Graphics.DrawLine(BORDER_HOVER_PEN, e.ClipRectangle.Left, e.ClipRectangle.Bottom - 1, e.ClipRectangle.Left, e.ClipRectangle.Top);
}
var tmpIcon = Enabled ? icon : Utils.GrayscaleImage(icon, 1.5f);
e.Graphics.DrawImage(tmpIcon, e.ClipRectangle.X + 2, e.ClipRectangle.Y + (isPressed ? 3 : 2));
}
[Category("Параметры"), Description("Изображение кнопки")]
public Image Icon {
get { return icon; }
set { icon = (Image) value.Clone(); UpdateIcon(); }
}
private RotateFlipType rotation;
[Category("Параметры"), Description("Изменить изображение"), DefaultValue(RotateFlipType.RotateNoneFlipNone)]
public RotateFlipType Rotation {
get { return rotation; }
set { rotation = value; UpdateIcon(); }
}
public void UpdateIcon () {
if (icon == null) return;
icon.RotateFlip(rotation);
}
protected override void OnEnabledChanged (EventArgs e) {
base.OnEnabledChanged(e);
Refresh();
}
}
}