C#winform之⾃定义按钮形状(初级版)⼀、前情回顾
⼩猿⼈在2020-08-13 00:59:53发过⼀篇没写完的博客,
如今突然想起,
想码码
也好不留遗憾,
就像爱⼀个姑娘,
总感觉爱得不够
总感觉爱得不深
时间很短
等待很长
岁⽉不饶程序⼈
啊~~~~
也罢,开始码代码:
⼆、实现效果
注:蓝⾊的roundButton1和红⾊showButton1
三、实现过程
1:具体实现如下:
原理:通过继承Button类,重写OnPaint()⽅法,使⽤画笔⼯具重绘控件样式。
public class myButton : System.Windows.Forms.Button
{
private Color enterForeColor = Color.White;
private Color leftForeColor = Color.Black;
private bool Isleft =true;
public bool IsLEFT
{
get{return Isleft;}
set
{
this.Isleft =value;
}
}
public Color EnterForeColor
public Color EnterForeColor
{
get{return enterForeColor;}
set
{
this.ForeColor =value;
}
}
public Color LeftForeColor
{
get{return leftForeColor;}
set
{
this.leftForeColor =value;
this.ForeColor =value;
}
}
[DefaultValue(typeof(Color),"51, 161, 224")]
//  [DefaultValue(typeof(Color), "220, 80, 80")]
//  [DefaultValue(typeof(Color), "251, 161, 0")]
protected override void OnMouseEnter(EventArgs e)//⿏标进⼊时
{
base.OnMouseEnter(e);
this.ForeColor =this.EnterForeColor;
}
protected override void OnMouseLeave(EventArgs e)//⿏标离开
{
base.OnMouseLeave(e);
this.ForeColor =this.LeftForeColor;
}
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
base.OnPaint(e);
base.OnPaintBackground(e);
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
e.Graphics.CompositingQuality = CompositingQuality.HighQuality;
e.Graphics.InterpolationMode = InterpolationMode.HighQualityBilinear;
e.Graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
Rectangle rect =new Rectangle(0,0,this.Width,this.Height);
var path =GetRoundedRectPath(rect);
this.Region =new Region(path);
//var pa = RectPath(rect);
//this.Region = new Region(pa);
Color baseColor =this.BackColor;
using(SolidBrush b =new SolidBrush(baseColor))
{
e.Graphics.FillPath(b, path);
//e.Graphics.FillPath(b, pa);
System.Drawing.Font fo =new System.Drawing.Font(this.Font.Name,this.Font.Size);
Brush brush =new SolidBrush(this.ForeColor);
Pen penn =new Pen(brush,3);
StringFormat gs =new StringFormat();
gs.Alignment = StringAlignment.Center;//居中
gs.LineAlignment = StringAlignment.Center;//垂直居中
e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
e.Graphics.DrawString(this.Text, fo, brush, rect, gs);
改变button按钮的形状}
}
private GraphicsPath RectPath(Rectangle re)
private GraphicsPath RectPath(Rectangle re)
{
GraphicsPath path =new GraphicsPath();
Point[] ps =new Point[4];
ps[0]=new Point(this.Width /5,this.Height /5);
ps[1]=new Point(4*this.Width /5,this.Height /5);
ps[2]=new Point(this.Width /5,4*this.Height /5);
ps[3]=new Point(4*this.Width /5,4*this.Height /5);
path.AddLines(ps);
path.CloseFigure();
return path;
}
private GraphicsPath GetRoundedRectPath(Rectangle rect)
{
Rectangle arcRect =new Rectangle(rect.Location,new System.Drawing.Size(this.Height,this.Height));
GraphicsPath path =new GraphicsPath();
Point[] p =new Point[12];
if(Isleft ==true)
{
p[0]=new Point(2*this.Width /5,0);
p[1]=new Point(0,this.Height /2);
p[2]=new Point(2*this.Width /5,this.Height);
p[3]=new Point(this.Width,0);
p[4]=new Point(4*this.Width /5,this.Height /4);
p[5]=new Point(4*this.Width /5,3*this.Height /4);
p[6]=new Point(this.Width,this.Height);
}
else
{
p[0]=new Point(3*this.Width /5,0);
p[1]=new Point(this.Width,this.Height /2);
p[2]=new Point(3*this.Width /5,this.Height);
p[3]=new Point(0,0);
p[4]=new Point(1*this.Width /5,this.Height /4);
p[5]=new Point(1*this.Width /5,3*this.Height /4);
p[6]=new Point(0,this.Height);
}
path.AddLine(p[0], p[1]);
path.AddLine(p[1], p[2]);
path.AddBezier(p[6], p[5], p[4], p[3]);
path.CloseFigure();
return path;
}
}
注意:以上是实现如图showButton5所⽰形状的控件,以下是实现roundButton1圆⾓按钮
2::同样,我们可以更改OnPaint()的内容,将控件改成圆⾓形状,这⾥我们需要给⾃定义控件添加⼀个新属性radius,表⽰圆⾓曲度,曲度越⼤控件越趋近于圆形。
属性设置:
private int radius;//半径
public int Radius
{
set
{
radius =value;
this.Invalidate();
}
get
{
return radius;
}
}
3:重新更改的OnPaint():
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
base.OnPaint(e);
base.OnPaintBackground(e);
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
e.Graphics.CompositingQuality = CompositingQuality.HighQuality;
e.Graphics.InterpolationMode = InterpolationMode.HighQualityBilinear;
Rectangle rect =new Rectangle(0,0,this.Width,this.Height);
var path =GetRoundedRectPath(rect, radius);
this.Region =new Region(path);
using(SolidBrush b =new SolidBrush(this.backColor))
{
e.Graphics.FillPath(b, path);
System.Drawing.Font fo =new System.Drawing.Font(this.Font.Name,this.Font.Size);
Brush brush =new SolidBrush(this.ForeColor);
StringFormat gs =new StringFormat();
gs.Alignment = StringAlignment.Center;//居中
gs.LineAlignment = StringAlignment.Center;//垂直居中
e.Graphics.DrawString(this.Text, fo, brush, rect, gs);
}
}
private GraphicsPath GetRoundedRectPath(Rectangle rect,int radius)
{
int diameter = radius;
Rectangle arcRect =new Rectangle(rect.Location,new System.Drawing.Size(diameter, diameter));            GraphicsPath path =new GraphicsPath();
path.AddArc(arcRect,180,90);
arcRect.X = rect.Right - diameter;
path.AddArc(arcRect,270,90);
arcRect.Y = rect.Bottom - diameter;
path.AddArc(arcRect,0,90);
arcRect.X = rect.Left;
path.AddArc(arcRect,90,90);
path.CloseFigure();
return path;
}
程序写到这样,也算是到此为⽌了吧!
everybody,明天见!

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。