圆形边框,边框颜色为c#winforms form边框

圆形边框,边框颜色为c#winforms form边框,c#,winforms,C#,Winforms,是否可以创建具有圆形边框和边框颜色的窗体? 我尝试了以下方法: protected override void OnPaint(PaintEventArgs e) { ControlPaint.DrawBorder(e.Graphics, ClientRectangle, Color.NavajoWhite, ButtonBorderStyle.Solid); } 及 结果是: 我还尝试:

是否可以创建具有圆形边框和边框颜色的窗体? 我尝试了以下方法:


        protected override void OnPaint(PaintEventArgs e) {
            ControlPaint.DrawBorder(e.Graphics, ClientRectangle, Color.NavajoWhite, 
            ButtonBorderStyle.Solid);
        }

结果是:

我还尝试:


        protected override void OnPaint(PaintEventArgs e) {
            ControlPaint.DrawBorder(e.Graphics, this.ClientRectangle, Color.White, 1, ButtonBorderStyle.Solid, Color.White, 1, ButtonBorderStyle.Solid, Color.White, 2, ButtonBorderStyle.Solid, Color.White, 2, ButtonBorderStyle.Solid);
        }

结果是:


我离第二个很近,但是如何扩展圆角边上的颜色边框?

您需要手动为表单绘制边框。这里有一个简单的演示,你可以参考

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        this.FormBorderStyle = FormBorderStyle.None;
    }

    public void SetWindowRegion()
    {
        System.Drawing.Drawing2D.GraphicsPath FormPath;
        FormPath = new System.Drawing.Drawing2D.GraphicsPath();
        Rectangle rect = new Rectangle(0, 0, this.Width, this.Height);
        FormPath = GetRoundedRectPath(rect, 30);// 30 represents the size of the fillet angle
        this.Region = new Region(FormPath);
    }

    private GraphicsPath GetRoundedRectPath(Rectangle rect, int radius)
    {
        int diameter = radius;
        Rectangle arcRect = new Rectangle(rect.Location, new Size(diameter, diameter));
        GraphicsPath path = new GraphicsPath();

        path.AddArc(arcRect, 180, 90);// top left

        arcRect.X = rect.Right - diameter;//top right
        path.AddArc(arcRect, 270, 90);

        arcRect.Y = rect.Bottom - diameter;// buttom right
        path.AddArc(arcRect, 0, 90);

        arcRect.X = rect.Left;// button left
        path.AddArc(arcRect, 90, 90);
        path.CloseFigure();
        return path;
    }


    private static GraphicsPath GetRoundRectangle(Rectangle rectangle, int r)
    {
        int l = 2 * r;
        // Divide the rounded rectangle into a combination of straight lines and arcs, and add them to the path in turn
        GraphicsPath gp = new GraphicsPath();
        gp.AddLine(new Point(rectangle.X + r, rectangle.Y), new Point(rectangle.Right - r, rectangle.Y));
        gp.AddArc(new Rectangle(rectangle.Right - l, rectangle.Y, l, l), 270F, 90F);

        gp.AddLine(new Point(rectangle.Right, rectangle.Y + r), new Point(rectangle.Right, rectangle.Bottom - r));
        gp.AddArc(new Rectangle(rectangle.Right - l, rectangle.Bottom - l, l, l), 0F, 90F);

        gp.AddLine(new Point(rectangle.Right - r, rectangle.Bottom), new Point(rectangle.X + r, rectangle.Bottom));
        gp.AddArc(new Rectangle(rectangle.X, rectangle.Bottom - l, l, l), 90F, 90F);

        gp.AddLine(new Point(rectangle.X, rectangle.Bottom - r), new Point(rectangle.X, rectangle.Y + r));
        gp.AddArc(new Rectangle(rectangle.X, rectangle.Y, l, l), 180F, 90F);
        return gp;
    }

    public void FillRoundRectangle(Graphics g, Rectangle rectangle, Pen pen, int r)
    {
        rectangle = new Rectangle(rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height);
        g.DrawPath(pen, GetRoundRectangle(rectangle, r));
    }

    private void Form1_Paint(object sender, PaintEventArgs e)
    {
        Pen pen = new Pen(Color.Blue, 3);
        pen.DashStyle = DashStyle.Solid;
        Rectangle rectangle = new Rectangle(1, 1, this.Width - 2, this.Height - 2);
        FillRoundRectangle(e.Graphics, rectangle, pen, 14);
    }

    private void Form1_Resize(object sender, EventArgs e)
    {

        if (this.WindowState == FormWindowState.Normal)
        {
            SetWindowRegion();
        }
        else
        {
            this.Region = null;
        }
    }
}

我想你需要画一条与圆形边缘相匹配的路径。哇!它真的很有效。。。但是,如果我将值30更改为某个数字,则边框颜色不适合窗体:FormPath=GetRoundedRectPath(rect,30);我刚刚遇到这个解决方案,对此我是新手。@Hacki您可以修改
FillRoundRectangle
的最后一个参数以适应它。public void FillRoundRectangle(图形g,矩形矩形矩形,钢笔,int r){Rectangle=new Rectangle(矩形.X,矩形.Y,矩形.宽度,矩形.高度);g.DrawPath(钢笔,GetRoundRectangle(矩形,r));}这一个?我要在哪里更改它?这对我来说真的很新;(@Hacki在演示中,我在
Form1\u Paint
中调用了该方法。您只需将
14
修改为适当的值。虽然这不是一个完美的剪切,但答案很好,我还是接受了。我没有看到任何平滑的结果
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        this.FormBorderStyle = FormBorderStyle.None;
    }

    public void SetWindowRegion()
    {
        System.Drawing.Drawing2D.GraphicsPath FormPath;
        FormPath = new System.Drawing.Drawing2D.GraphicsPath();
        Rectangle rect = new Rectangle(0, 0, this.Width, this.Height);
        FormPath = GetRoundedRectPath(rect, 30);// 30 represents the size of the fillet angle
        this.Region = new Region(FormPath);
    }

    private GraphicsPath GetRoundedRectPath(Rectangle rect, int radius)
    {
        int diameter = radius;
        Rectangle arcRect = new Rectangle(rect.Location, new Size(diameter, diameter));
        GraphicsPath path = new GraphicsPath();

        path.AddArc(arcRect, 180, 90);// top left

        arcRect.X = rect.Right - diameter;//top right
        path.AddArc(arcRect, 270, 90);

        arcRect.Y = rect.Bottom - diameter;// buttom right
        path.AddArc(arcRect, 0, 90);

        arcRect.X = rect.Left;// button left
        path.AddArc(arcRect, 90, 90);
        path.CloseFigure();
        return path;
    }


    private static GraphicsPath GetRoundRectangle(Rectangle rectangle, int r)
    {
        int l = 2 * r;
        // Divide the rounded rectangle into a combination of straight lines and arcs, and add them to the path in turn
        GraphicsPath gp = new GraphicsPath();
        gp.AddLine(new Point(rectangle.X + r, rectangle.Y), new Point(rectangle.Right - r, rectangle.Y));
        gp.AddArc(new Rectangle(rectangle.Right - l, rectangle.Y, l, l), 270F, 90F);

        gp.AddLine(new Point(rectangle.Right, rectangle.Y + r), new Point(rectangle.Right, rectangle.Bottom - r));
        gp.AddArc(new Rectangle(rectangle.Right - l, rectangle.Bottom - l, l, l), 0F, 90F);

        gp.AddLine(new Point(rectangle.Right - r, rectangle.Bottom), new Point(rectangle.X + r, rectangle.Bottom));
        gp.AddArc(new Rectangle(rectangle.X, rectangle.Bottom - l, l, l), 90F, 90F);

        gp.AddLine(new Point(rectangle.X, rectangle.Bottom - r), new Point(rectangle.X, rectangle.Y + r));
        gp.AddArc(new Rectangle(rectangle.X, rectangle.Y, l, l), 180F, 90F);
        return gp;
    }

    public void FillRoundRectangle(Graphics g, Rectangle rectangle, Pen pen, int r)
    {
        rectangle = new Rectangle(rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height);
        g.DrawPath(pen, GetRoundRectangle(rectangle, r));
    }

    private void Form1_Paint(object sender, PaintEventArgs e)
    {
        Pen pen = new Pen(Color.Blue, 3);
        pen.DashStyle = DashStyle.Solid;
        Rectangle rectangle = new Rectangle(1, 1, this.Width - 2, this.Height - 2);
        FillRoundRectangle(e.Graphics, rectangle, pen, 14);
    }

    private void Form1_Resize(object sender, EventArgs e)
    {

        if (this.WindowState == FormWindowState.Normal)
        {
            SetWindowRegion();
        }
        else
        {
            this.Region = null;
        }
    }
}