繁体   English   中英

c# winforms form边框中带有边框颜色的圆角边框

[英]Rounded border with border color in c# winforms form border

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


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

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

这是结果:

在此处输入图像描述

我也试过:


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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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