一、前情回顾
小猿人在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
{
get { return enterForeColor; }
set
{
this.enterForeColor = value;
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);
}
}
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,明天见!