-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCustomButton.cs
158 lines (134 loc) · 4.22 KB
/
CustomButton.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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Text;
using System.Windows.Forms;
namespace listen2me
{
class CustomButton : UserControl
{
public CustomButton()
{
BackColor = Color.Transparent;
InitializeComponents();
}
protected override void OnMouseEnter(EventArgs e)
{
isHover = true;
Refresh();
}
protected override void OnMouseLeave(EventArgs e)
{
isHover = false;
Refresh();
}
protected override void OnMouseMove(MouseEventArgs e)
{
bool newHoverState = !(Math.Min(e.Location.X, e.Location.Y) < 0);
if (isHover != newHoverState)
{
isHover = newHoverState;
//HoverChanged?.Invoke(this, isHover);
Refresh();
}
}
protected override void OnMouseDown(MouseEventArgs e)
{
IsDown = true;
clickStarted = DateTime.Now;
Refresh();
}
protected override void OnMouseUp(MouseEventArgs e)
{
clickFinished = DateTime.Now;
IsDown = false;
ClickEventArgs args = new ClickEventArgs(
new System.Windows.Point(e.Location.X, e.Location.Y),
new System.Windows.Duration(clickFinished - clickStarted)
);
if (isHover)
LeftClick?.Invoke(this, args);
Refresh();
}
private void InitializeComponents()
{
ImageChanged += OnImageChanged;
IconSizeChanged += OnIconSizeChanged;
}
protected override void OnPaint(PaintEventArgs e)
{
if (Image == null)
return;
float canvasMin = IconSize;// * Image.VerticalResolution / e.Graphics.DpiX;
float imageMin = (float)Image.Height;
Bitmap newImg = ImageHelper.ResizeImage(Image, (float)imageMin / (float)canvasMin);
e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
PointF offset = new PointF((e.ClipRectangle.Width - newImg.Width) / 2.0f, (e.ClipRectangle.Height - newImg.Height) / 2.0f);
e.Graphics.DrawImage(newImg, offset);
}
protected override void OnPaintBackground(PaintEventArgs e)
{
Color clr;
if (IsDown && isHover)
{
clr = MouseDownBackColor;
}
else if (IsDown || isHover)
{
clr = MouseOverBackColor;
}
else
{
clr = BackColor;
}
e.Graphics.Clear(clr);
}
private void OnImageChanged(object sender, Image img)
{
Refresh();
}
private void OnIconSizeChanged(object sender, int size)
{
Refresh();
}
public event EventHandler<ClickEventArgs> LeftClick;
public event EventHandler<Image> ImageChanged;
public event EventHandler<int> IconSizeChanged;
//public event EventHandler<bool> HoverChanged;
public Image Image
{
get { return __image; }
set
{
if (__image != value)
{
__image = value;
ImageChanged?.Invoke(this, __image);
}
}
}
public int IconSize
{
get { return __iconSize; }
set
{
if (__iconSize != value)
{
__iconSize = value;
IconSizeChanged?.Invoke(this, __iconSize);
}
}
}
private Image __image;
private int __iconSize = 16;
public Color MouseDownBackColor = Color.FromArgb(32, 255, 255, 255);
public Color MouseOverBackColor = Color.FromArgb(50, 255, 255, 255);
private bool isHover = false;
private bool IsDown = false;
private DateTime clickStarted;
private DateTime clickFinished;
}
}