面板圆边

时间:2017-09-28 16:29:55

标签: c# visual-studio

public void DrawRoundRect(Graphics g, Pen p, float X, float Y, float width, float height, float radius)
    {
        GraphicsPath gp = new GraphicsPath();
        gp.AddLine(X + radius, Y, X + width - (radius * 2), Y);
        gp.AddArc(X + width - (radius * 2), Y, radius * 2, radius * 2, 270, 90);
        gp.AddLine(X + width, Y + radius, X + width, Y + height - (radius * 2));
        gp.AddArc(X + width - (radius * 2), Y + height - (radius * 2), radius * 2, radius * 2, 0, 90);
        gp.AddLine(X + width - (radius * 2), Y + height, X + radius, Y + height);
        gp.AddArc(X, Y + height - (radius * 2), radius * 2, radius * 2, 90, 90);
        gp.AddLine(X, Y + height - (radius * 2), X, Y + radius);
        gp.AddArc(X, Y, radius * 2, radius * 2, 180, 90);
        gp.CloseFigure();
        g.DrawPath(p, gp);
    }

    private void _pnlLogIn_Paint(object sender, PaintEventArgs e)
    {
        Graphics v = e.Graphics;
        DrawRoundRect(v, Pens.Blue, e.ClipRectangle.Left, e.ClipRectangle.Top, e.ClipRectangle.Width - 1, e.ClipRectangle.Height - 1, 10);
        //Without rounded corners
        //e.Graphics.DrawRectangle(Pens.Blue, e.ClipRectangle.Left, e.ClipRectangle.Top, e.ClipRectangle.Width - 1, e.ClipRectangle.Height - 1);
        base.OnPaint(e);
    }

你好,我的代码出了什么问题。我的面板已经有一个圆边,但矩形面板的边框仍然存在。我应该添加或修改哪些代码?谢谢..

Image

enter image description here

1 个答案:

答案 0 :(得分:1)

将面板区域设置为图形路径。

在您发布的代码中,请在DrawPath方法中调用DrawRoundRect后执行此操作。

这样的事情:

_pnlLogIn.Region = new System.Drawing.Region(gp);

在我上面的代码更改后,我看到我的窗口如下所示。

我尝试了更大的半径(40)..

enter image description here