winform 自定义button如何变成圆角
要将Winform自定义按钮变成圆角,可以通过以下步骤操作:
-
创建一个自定义控件类,继承自Button类。
-
在该类的构造函数中,设置控件的样式为UserPaint,并启用双缓冲。
public class RoundButton : Button
{
public RoundButton()
{
this.SetStyle(ControlStyles.UserPaint, true);
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.DoubleBuffer, true);
}
}
- 重写OnPaint方法,在该方法中绘制圆角按钮的外观。
protected override void OnPaint(PaintEventArgs e)
{
GraphicsPath path = new GraphicsPath();
path.AddEllipse(new Rectangle(0, 0, this.Width, this.Height));
this.Region = new Region(path);
using (Brush brush = new SolidBrush(this.BackColor))
{
e.Graphics.FillEllipse(brush, new Rectangle(0, 0, this.Width, this.Height));
}
using (Pen pen = new Pen(this.ForeColor, 2))
{
e.Graphics.DrawEllipse(pen, new Rectangle(0, 0, this.Width, this.Height));
}
TextRenderer.DrawText(e.Graphics, this.Text, this.Font, new Rectangle(0, 0, this.Width, this.Height), this.ForeColor, TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter);
}
- 在Winform窗体中使用该自定义按钮控件。
RoundButton roundButton1 = new RoundButton();
roundButton1.Text = "圆角按钮";
roundButton1.Size = new Size(100, 40);
this.Controls.Add(roundButton1);
通过以上步骤,可以将Winform自定义按钮控件变成圆角形状
原文地址: https://www.cveoy.top/t/topic/iE7M 著作权归作者所有。请勿转载和采集!