C#GDI画圆角Panel
时间: 2023-10-16 18:07:32 浏览: 51
要在C#中使用GDI绘制圆角Panel,您可以按照以下步骤进行操作:
1. 创建一个自定义控件,继承自Panel。
```csharp
public class RoundPanel : Panel
{
public RoundPanel()
{
this.BackColor = Color.Transparent;
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics g = e.Graphics;
Rectangle rect = new Rectangle(0, 0, this.Width - 1, this.Height - 1);
int radius = 20;
// 绘制圆角矩形
GraphicsPath path = GetRoundedRect(rect, radius);
g.FillPath(new SolidBrush(this.BackColor), path);
g.DrawPath(new Pen(this.BackColor), path);
}
// 获取圆角矩形的路径
private GraphicsPath GetRoundedRect(Rectangle rect, int radius)
{
GraphicsPath path = new GraphicsPath();
path.StartFigure();
// 左上角
path.AddArc(rect.X, rect.Y, radius * 2, radius * 2, 180, 90);
// 右上角
path.AddArc(rect.X + rect.Width - radius * 2, rect.Y, radius * 2, radius * 2, 270, 90);
// 右下角
path.AddArc(rect.X + rect.Width - radius * 2, rect.Y + rect.Height - radius * 2, radius * 2, radius * 2, 0, 90);
// 左下角
path.AddArc(rect.X, rect.Y + rect.Height - radius * 2, radius * 2, radius * 2, 90, 90);
path.CloseFigure();
return path;
}
}
```
2. 在自定义控件的OnPaint方法中,使用Graphics类绘制圆角矩形。
3. 创建一个GetRoundedRect方法,该方法返回一个GraphicsPath对象,包含指定大小和半径的圆角矩形的路径。
4. 在GetRoundedRect方法中,添加四个圆弧来绘制矩形的四个角。
现在,您可以使用RoundPanel控件来替换标准Panel控件,并在其中绘制圆角矩形。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)