Panel圆角处理
2021-04-02 本文已影响0人
nickieeee
private void SetWindowR()
{
System.Drawing.Drawing2D.GraphicsPath gPath = new System.Drawing.Drawing2D.GraphicsPath();
Rectangle rect = new Rectangle(0,0, this.panel1.Width, this.panel1.Height);
gPath = GetRoundedRP(rect, 10);
this.panel1.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;
}
如果要设置圆角的控件不需要动态设置尺寸,可以直接复写OnResize()
, 如下:
protected override void OnResize(EventArgs e)
{
base.OnResize(e);
this.panel1.Region = null;
SetWindowR();
}
如果控件是动态设置尺寸的,就在设置尺寸的方法里调用即可。