C sharp中button如何设置圆角
时间: 2023-10-19 10:07:25 浏览: 127
在C#中,可以使用以下代码来设置Button控件的圆角:
```csharp
button1.FlatStyle = FlatStyle.Flat;
button1.FlatAppearance.BorderSize = 0;
button1.FlatAppearance.MouseDownBackColor = Color.Transparent;
button1.FlatAppearance.MouseOverBackColor = Color.Transparent;
button1.BackColor = Color.Red;
button1.Region = System.Drawing.Region.FromHrgn(CreateRoundRectRgn(0, 0, button1.Width, button1.Height, 20, 20));
```
其中,CreateRoundRectRgn() 是一个Win32 API函数,需要通过DllImport导入,代码如下:
```csharp
[DllImport("Gdi32.dll", EntryPoint = "CreateRoundRectRgn")]
private static extern IntPtr CreateRoundRectRgn
(
int nLeftRect, // x-coordinate of upper-left corner
int nTopRect, // y-coordinate of upper-left corner
int nRightRect, // x-coordinate of lower-right corner
int nBottomRect, // y-coordinate of lower-right corner
int nWidthEllipse, // height of ellipse
int nHeightEllipse // width of ellipse
);
```
这段代码将Button控件的边框去掉,并且设置了Button的背景色和圆角。其中,Region属性是用来设置控件的形状的,通过FromHrgn方法将Win32 API函数创建的圆角区域设置为Button的形状。
阅读全文