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://img-blog.csdnimg.cn/2020041010552880.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM0MjgwNjgx,size_16,color_FFFFFF,t_70)
点击下载,源码在这