📄 imagebutton.cs
字号:
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace Custom
{
/// <summary>
/// 图形按钮
/// </summary>
public sealed class ImageButton : Control
{
#region "属性"
#region "受保护的属性"
/// <summary>
/// 按钮上的图像
/// </summary>
private Image _image;
/// <summary>
/// 记录按钮是否按下
/// </summary>
private bool bPushed;
/// <summary>
/// 透明点x位置
/// </summary>
private int _x;
/// <summary>
/// 透明点y位置
/// </summary>
private int _y;
/// <summary>
/// 背景颜色
/// </summary>
private Color _backgroundcolor;
/// <summary>
/// 前景颜色
/// </summary>
private Color _fillcolor;
/// <summary>
/// 线条颜色
/// </summary>
private Color _linecolor;
/// <summary>
/// 阴影颜色
/// </summary>
private Color _shadowcolor;
/// <summary>
/// 按钮区域点
/// </summary>
private Point[] _p;
/// <summary>
/// 阴影显示
/// </summary>
private bool _showshadow;
/// <summary>
/// 图像定位方式
/// </summary>
private PictureBoxSizeMode _sizemode;
/// <summary>
/// 屏幕绘图对象
/// </summary>
private Graphics _screen;
/// <summary>
/// 提示条定位方式
/// </summary>
private TipAnchorStyles _tipanchor;
#endregion
#region "公共属性"
#region "图像"
/// <summary>
/// 获取或设置按钮的图像
/// </summary>
public Image Image
{
get
{
return _image;
}
set
{
_image = value;
Invalidate();
}
}
#endregion
#region "背景色"
/// <summary>
/// 获取或设置背景色
/// </summary>
public override Color BackColor
{
get
{
return base.BackColor;
}
set
{
base.BackColor = value;
_backgroundcolor = value;
Invalidate();
}
}
#endregion
#region "前景色"
/// <summary>
/// 获取或设置前景色
/// </summary>
public Color FillColor
{
get
{
return _fillcolor;
}
set
{
_fillcolor = value;
Invalidate();
}
}
#endregion
#region "线条色"
/// <summary>
/// 获取或设置线条色
/// </summary>
public Color LineColor
{
get
{
return _linecolor;
}
set
{
_linecolor = value;
Invalidate();
}
}
#endregion
#region "阴影色"
/// <summary>
/// 获取或设置阴影色
/// </summary>
public Color ShadowColor
{
get
{
return _shadowcolor;
}
set
{
_shadowcolor = value;
Invalidate();
}
}
#endregion
#region "是否显示阴影"
/// <summary>
/// 是否显示阴影
/// </summary>
public bool ShowShadow
{
get
{
return _showshadow;
}
set
{
_showshadow = value;
if (_showshadow)
{
_p[0].X = 0;
_p[0].Y = 2;
_p[1].X = 2;
_p[1].Y = 0;
_p[2].X = Width - 6;
_p[2].Y = 0;
_p[3].X = Width - 4;
_p[3].Y = 2;
_p[4].X = Width - 4;
_p[4].Y = Height - 6;
_p[5].X = Width - 6;
_p[5].Y = Height - 4;
_p[6].X = 2;
_p[6].Y = Height - 4;
_p[7].X = 0;
_p[7].Y = Height - 6;
}
else
{
_p[0].X = 0;
_p[0].Y = 2;
_p[1].X = 2;
_p[1].Y = 0;
_p[2].X = Width - 3;
_p[2].Y = 0;
_p[3].X = Width - 1;
_p[3].Y = 2;
_p[4].X = Width - 1;
_p[4].Y = Height - 3;
_p[5].X = Width - 3;
_p[5].Y = Height - 1;
_p[6].X = 2;
_p[6].Y = Height - 1;
_p[7].X = 0;
_p[7].Y = Height - 3;
}
this.Invalidate();
}
}
#endregion
#region "背景像素点"
/// <summary>
/// 获取或设置背景像素点
/// </summary>
public Point BackgroundPoint
{
get
{
return new Point(_x, _y);
}
set
{
_x = value.X;
_y = value.Y;
Invalidate();
}
}
#endregion
#region "图像定位方式"
/// <summary>
/// 获取或设置图像在按钮中的定位方式
/// </summary>
public PictureBoxSizeMode SizeMode
{
get
{
return _sizemode;
}
set
{
_sizemode = value;
this.Invalidate();
}
}
#endregion
#region "提示条定位方式"
/// <summary>
/// 获取或设置提示条的定位方式
/// </summary>
public TipAnchorStyles TipAnchor
{
get
{
return _tipanchor;
}
set
{
_tipanchor = value;
this.Invalidate();
}
}
#endregion
#region "屏幕绘图对象"
/// <summary>
/// 设置屏幕绘图对象
/// </summary>
public Graphics Screen
{
set
{
_screen = value;
}
}
#endregion
#endregion
#endregion
#region "构造函数"
/// <summary>
/// 构造函数
/// </summary>
public ImageButton()
{
bPushed = false;
_x = 0;
_y = 0;
_fillcolor = Color.White;
_linecolor = Color.FromArgb((int)(_fillcolor.R * 0.5), (int)(_fillcolor.G * 0.5), (int)(_fillcolor.B * 0.5));
_shadowcolor = Color.Gray;
_p = new Point[8];
_showshadow = false;
Image = null;
_sizemode = PictureBoxSizeMode.StretchImage;
_tipanchor = TipAnchorStyles.Bottom;
Size = new Size(16, 16);
}
#endregion
#region "方法"
#region "受保护的方法"
#region "尺寸改变"
/// <summary>
/// 尺寸改变
/// </summary>
/// <param name="e"></param>
protected override void OnResize(EventArgs e)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -