1.概述
winform 自带的按键button 美观上面的话,完全不够看
那么?
想制作一个美观 的自定义圆角按键,如何实现呢?
2.效果展示
①仅显示文字
②仅显示图像
③显示图像与文字
⑤带有边界的圆角按键
3.自定义属性参数
4.实质运用
方便观看进一步的效果就自制一个自定义窗体
使用上圆角按键
5.核心代码
主要是对原来的按键进行了重绘设计
protected override void OnPaint(PaintEventArgs pevent)
{
base.OnPaint(pevent);
if (IsSolid)
{
pevent.Graphics.SmoothingMode = SmoothingMode.AntiAlias;//绘画图形的平滑模式,抗锯齿
RectangleF rectBorder = new RectangleF(0, 0, this.Width - 0.5f, this.Height - 0.5f);
Pen penBorder = new Pen(this.BackColor, 3);
GraphicsPath pathBorder = GetFigurePath(rectBorder);
this.Region = new Region(pathBorder);//边界
pevent.Graphics.DrawPath(penBorder, pathBorder);
//在内层加一个真正的区域--显示的也是这个不会右region的锯齿问题
RectangleF rectSurface = new RectangleF(1f, 1f, this.Width - 1f, this.Height - 1f);
GraphicsPath pathSurface = GetFigurePath(rectSurface);
Color sfColor = SurfaceColor;
pevent.Graphics.FillPath(new SolidBrush(sfColor), pathSurface);//遮盖层,遮挡住原有的图像与文字
sfColor = hover ? GetArgbColor(press ? 128 : 255, HoverColor) : SurfaceColor;
pevent.Graphics.FillPath(new SolidBrush(sfColor), pathSurface);//鼠标事件响应层
//绘制背景图像
if (BackgroundImage != null)
{
DrawBackgroundImage(
pevent.Graphics,
this.BackgroundImage,
rectSurface,
this.BackgroundImageLayout,
BackImageOffsetX,
BackImageOffsetY
);
//不加的话,填充的时候,出现锯齿
penBorder.Width = 1;
pevent.Graphics.DrawPath(penBorder, pathBorder);
}
if (TextVisible)
{
//绘制文字
RectangleF rectText = new RectangleF(0 + TextOffsetX, 0 + TextOffsetY
, this.Width - 0.5f + TextOffsetX, this.Height - 0.5f + TextOffsetY);
StringFormat format = new StringFormat();//控制文字方向
format.LineAlignment = strAlignV;//文字垂直方向
format.Alignment = strAlignH;//文字水平方向
pevent.Graphics.DrawString(
this.Text,
this.Font,
new SolidBrush(this.ForeColor),
rectText,
format);
}
}
else
{
pevent.Graphics.SmoothingMode = SmoothingMode.AntiAlias;//绘画图形的平滑模式,抗锯齿
RectangleF rectBorder = new RectangleF(0, 0, this.Width - 0.5f, this.Height - 0.5f);
Pen penBorder = new Pen(this.BackColor, 3);
GraphicsPath pathBorder = GetFigurePath(rectBorder);
this.Region = new Region(pathBorder);//边界
pevent.Graphics.DrawPath(penBorder, pathBorder);
//在内层加一个真正的区域--显示的也是这个不会右region的锯齿问题
RectangleF rectSurface = new RectangleF(1f, 1f, this.Width - 1f, this.Height - 1f);
GraphicsPath pathSurface = GetFigurePath(rectSurface);
Color sfColor = hover ? GetArgbColor(64, hoverColor)
: (IsSelected ? GetArgbColor(64, hoverColor) : GetArgbColor(0, this.BackColor));
pevent.Graphics.FillPath(new SolidBrush(sfColor), pathSurface);
}
if (BorderVisible)
{
RectangleF rectBorder = new RectangleF(1f, 1f, this.Width - 1f, this.Height - 1f);
GraphicsPath pathBorder = GetFigurePath(rectBorder);
Pen penBorder = new Pen(BorderColor, BorderWidth);
pevent.Graphics.DrawPath(penBorder, pathBorder);
}
}
6.代码下载
https://download.youkuaiyun.com/download/adsd1233123/85924224