winform窗体变成圆角
以下是代码,直接复制到窗体代码中,就能实现类似图片的效果。
gPath = GetRoundedRP(rect,30); //后面的30是圆的角度,数值越大圆角度越大
protected override void OnResize(EventArgs e)
{
this.Region = null;
SetWindowR();
}
private void SetWindowR()
{
System.Drawing.Drawing2D.GraphicsPath gPath = new System.Drawing.Drawing2D.GraphicsPath();
Rectangle rect = new Rectangle(0, 5, this.Width, this.Height - 5);
gPath = GetRoundedRP(rect,30); //后面的30是圆的角度,数值越大圆角度越大
this.Region = new Region(gPath);
}
private System.Drawing.Drawing2D.GraphicsPath GetRoundedRP(Rectangle rect, int a)
{
int diameter = a;
Rectangle arcRect = new Rectangle(rect.Location, new Size(diameter, diameter));
System.Drawing.Drawing2D.GraphicsPath gp = new System.Drawing.Drawing2D.GraphicsPath();
gp.AddArc(arcRect, 180, 90);
arcRect.X = rect.Right - diameter;
gp.AddArc(arcRect, 270, 90);
arcRect.Y = rect.Bottom - diameter;
gp.AddArc(arcRect, 0, 90);
arcRect.X = rect.Left;
gp.AddArc(arcRect, 90, 90);
gp.CloseFigure();
return gp;
}
以下是图片
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/855d2f4174b6bec2cb3f7520e2e76197.png)
点击下载,源码在这