简体   繁体   中英

Rounded border with border color in c# winforms form border

Is it possible to create a form that has rounded borders and have a border color? I have tried the following:


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

and

        [DllImport("Gdi32.dll", EntryPoint = "CreateRoundRectRgn")]
        private static extern IntPtr CreateRoundRectRgn
        (
            int nLeftRect,     // x-coordinate of upper-left corner
            int nTopRect,      // y-coordinate of upper-left corner
            int nRightRect,    // x-coordinate of lower-right corner
            int nBottomRect,   // y-coordinate of lower-right corner
            int nWidthEllipse, // height of ellipse
            int nHeightEllipse // width of ellipse
        );


        public Main() {
            InitializeComponent();
            this.FormBorderStyle = FormBorderStyle.None;
            Region = System.Drawing.Region.FromHrgn(CreateRoundRectRgn(0, 0, Width, Height, 18, 18));
        }

This is the result:

在此处输入图像描述

I also tried:


        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);
        }

This is the result:

在此处输入图像描述

I am so close to the 2nd one, but how can I extend the color borders on the rounded edges?

You need to draw a border for the form manually. And here is a simple demo you can refer to.

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;
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM