美文网首页
Panel圆角处理

Panel圆角处理

作者: nickieeee | 来源:发表于2021-04-02 15:52 被阅读0次
    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();
    }
    

    如果控件是动态设置尺寸的,就在设置尺寸的方法里调用即可。

    相关文章

      网友评论

          本文标题:Panel圆角处理

          本文链接:https://www.haomeiwen.com/subject/fihqkltx.html