winform中button边框圆角
时间: 2023-08-01 08:09:57 浏览: 227
在 WinForms 应用程序中,如果需要将 Button 控件的边框变成圆角,可以通过以下步骤实现:
1. 在按钮控件的 `Paint` 事件中,使用 `GraphicsPath` 对象绘制一个圆角矩形,并使用 `Region` 对象将按钮的可见区域设置为这个圆角矩形。具体实现如下:
```csharp
private void button1_Paint(object sender, PaintEventArgs e)
{
// 绘制圆角矩形
GraphicsPath path = new GraphicsPath();
path.AddArc(0, 0, 20, 20, 180, 90);
path.AddLine(20, 0, button1.Width - 20, 0);
path.AddArc(button1.Width - 20, 0, 20, 20, 270, 90);
path.AddLine(button1.Width, 20, button1.Width, button1.Height - 20);
path.AddArc(button1.Width - 20, button1.Height - 20, 20, 20, 0, 90);
path.AddLine(button1.Width - 20, button1.Height, 20, button1.Height);
path.AddArc(0, button1.Height - 20, 20, 20, 90, 90);
path.AddLine(0, button1.Height - 20, 0, 20);
// 设置按钮的可见区域为圆角矩形
button1.Region = new Region(path);
}
```
在上面的代码中,我们使用了 `GraphicsPath` 对象绘制了一个圆角矩形,然后使用 `Region` 对象将按钮的可见区域设置为这个圆角矩形。其中,`AddArc` 方法用于绘制圆角,`AddLine` 方法用于绘制直线连接圆角,从而形成一个完整的圆角矩形。
2. 如果需要让按钮的背景色和边框颜色也变成圆角的形状,可以在按钮的 `MouseEnter` 和 `MouseLeave` 事件中分别设置按钮的背景色和边框颜色。具体实现如下:
```csharp
private void button1_MouseEnter(object sender, EventArgs e)
{
// 鼠标移入时设置按钮的背景色和边框颜色
button1.BackColor = Color.FromArgb(255, 240, 240, 240);
button1.FlatAppearance.BorderColor = Color.FromArgb(255, 180, 180, 180);
}
private void button1_MouseLeave(object sender, EventArgs e)
{
// 鼠标移出时恢复按钮的背景色和边框颜色
button1.BackColor = Color.FromArgb(255, 220, 220, 220);
button1.FlatAppearance.BorderColor = Color.FromArgb(255, 200, 200, 200);
}
```
在上面的代码中,我们使用了 `BackColor` 属性和 `FlatAppearance.BorderColor` 属性来设置按钮的背景色和边框颜色,从而让按钮的背景色和边框颜色也变成了圆角的形状。
阅读全文
相关推荐

















