-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImageButton.cs
265 lines (233 loc) · 9.38 KB
/
ImageButton.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
using System.ComponentModel;
using System.Drawing.Drawing2D;
namespace KControlsLib
{
public class ImageButton : PictureBox, IButtonControl, IDisposable
{
#region 新建属性
[Description("基本图像"), Category("自定义属性")]
public Image ImageNormal { get { return this.imageNormal; } set { base.Image = this.imageNormal = value; } }
[Description("悬停后图像"), Category("自定义属性")]
public Image? ImageNormalHover { get; set; } = null;
[Description("按下后图像"), Category("自定义属性")]
public Image? ImageDown { get; set; } = null;
[Description("Disable后图像"), Category("自定义属性")]
public Image? ImageDisabled { get; set; } = null;
[Description("悬停ToolTip文字"), Category("自定义属性")]
public string HoverText { get; set; } = "";
[Description("Toggle 模式"), Category("自定义属性")]
public bool ToggleMode { get; set; } = false;
[Description("Toggle 模式按下后悬停ToolTip文字"), Category("自定义属性")]
public string HoverTextToggle { get; set; } = "";
[Description("Toggle 模式按下后悬停图像"), Category("自定义属性")]
public Image? ImageDownHover { get; set; } = null;
[Description("当前是否按下状态"), Category("自定义属性")]
public bool Down { get { return _down; } set { _down = value; UpdateImage(this.Down ? this.ImageDown : this.ImageNormal); } }
#endregion
#region 隐藏无用属性
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public new Image Image { get { return base.Image; } set { base.Image = value; } }
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public new ImageLayout BackgroundImageLayout { get { return base.BackgroundImageLayout; } set { base.BackgroundImageLayout = value; } }
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public new Image BackgroundImage { get { return base.BackgroundImage; } set { base.BackgroundImage = value; } }
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public new String ImageLocation { get { return base.ImageLocation; } set { base.ImageLocation = value; } }
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public new Image ErrorImage { get { return base.ErrorImage; } set { base.ErrorImage = value; } }
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public new Image InitialImage { get { return base.InitialImage; } set { base.InitialImage = value; } }
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public new bool WaitOnLoad { get { return base.WaitOnLoad; } set { base.WaitOnLoad = value; } }
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public new string Text { get { return base.Text; } set { base.Text = value; } }
#endregion
#region 内部变量等
private System.ComponentModel.IContainer components = null;
private Image imageNormal;
private ToolTip tip = new ToolTip();
private bool _down = false;
#endregion
#region 覆盖基类函数
protected override void OnBackgroundImageChanged(EventArgs e) { } // 什么都不做
protected override void OnTextChanged(EventArgs e) { } // 什么都不做
#endregion
#region 构造函数
public ImageButton() : base()
{
this.InitializeComponent();
base.SizeMode = PictureBoxSizeMode.StretchImage;
base.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.Margin = new Padding(0, 0, 0, 0);
this.MouseUp += ImageButton_MouseUp;
this.MouseDown += ImageButton_MouseDown;
this.MouseEnter += ImageButton_MouseEnter;
this.MouseLeave += ImageButton_MouseLeave;
this.EnabledChanged += ImageButton_EnabledChanged;
this.MouseHover += ImageButton_MouseHover;
}
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
// this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.AllPaintingInWmPaint, true); // 禁止擦除背景.
SetStyle(ControlStyles.DoubleBuffer, true); // 双缓冲
SetStyle(ControlStyles.OptimizedDoubleBuffer, true);//解决闪烁
UpdateStyles();
}
#endregion
#region 方法实现
private void UpdateImage(Image? img)
{
if (img != null) base.Image = img;
}
private void UpdateTip(string? str)
{
if (str == "") return;
tip.SetToolTip(this, str);
}
private void ImageButton_MouseHover(object? sender, EventArgs e)
{
if (this.ToggleMode)
UpdateTip(this.Down ? this.HoverTextToggle : this.HoverText);
else
UpdateTip(this.HoverText);
}
private void ImageButton_MouseUp(object? sender, MouseEventArgs e)
{
if (!this.ToggleMode) this.Down = false;
}
private void ImageButton_MouseDown(object? sender, MouseEventArgs e)
{
base.Focus();
if (e.Button != MouseButtons.Left) return;
if (this.ToggleMode) this.Down = !this.Down;
else this.Down = true;
}
private void ImageButton_MouseEnter(object? sender, EventArgs e)
{
if (this.ToggleMode)
{
UpdateImage(this.Down ? this.ImageDownHover : this.ImageNormalHover);
}
else
{
UpdateImage(this.ImageNormalHover);
}
}
private void ImageButton_MouseLeave(object? sender, EventArgs e)
{
if (this.ToggleMode)
{
UpdateImage(this.Down ? this.ImageDown : this.ImageNormal);
}
else
{
UpdateImage(this.ImageNormal);
}
}
private void ImageButton_EnabledChanged(object? sender, EventArgs e)
{
this.Down = false;
UpdateImage(this.Enabled ? this.imageNormal : this.ImageDisabled);
}
#endregion
#region IDisposable 接口
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#endregion
#region IButtonControl 接口
private DialogResult dialogResult;
private bool IsDefault;
public DialogResult DialogResult
{
get
{
return this.dialogResult;
}
set
{
if (Enum.IsDefined(typeof(DialogResult), value))
{
this.dialogResult = value;
}
}
}
public void NotifyDefault(bool value)
{
this.IsDefault = value;
}
public void PerformClick()
{
base.OnClick(EventArgs.Empty);
}
#endregion
#region 键盘处理
/// <summary>
/// Enter 直接触发
/// Space 按下作为鼠标按下,松开作为鼠标松开,触发
/// Space 按下后按Esc或Tab取消触发,但执行鼠标松开
/// </summary>
private const int WM_KEYDOWN = 0x0100;
private const int WM_KEYUP = 0x0101;
private bool holdingSpace = false;
public override bool PreProcessMessage(ref Message msg)
{
if (msg.Msg == WM_KEYUP)
{
if (holdingSpace)
{
if ((int)msg.WParam == (int)Keys.Space)
{
OnMouseUp(null);
PerformClick();
}
else if ((int)msg.WParam == (int)Keys.Escape
|| (int)msg.WParam == (int)Keys.Tab)
{
holdingSpace = false;
OnMouseUp(null);
}
}
return true;
}
else if (msg.Msg == WM_KEYDOWN)
{
if ((int)msg.WParam == (int)Keys.Space)
{
holdingSpace = true;
OnMouseDown(null);
}
else if ((int)msg.WParam == (int)Keys.Enter)
{
PerformClick();
}
return true;
}
else
return base.PreProcessMessage(ref msg);
}
protected override void OnLostFocus(EventArgs e)
{
holdingSpace = false;
OnMouseUp(null);
base.OnLostFocus(e);
}
#endregion
}
}