【问题标题】:Rounded border with border color in c# winforms form borderc# winforms表单边框中带边框颜色的圆角边框
【发布时间】:2020-12-02 19:40:11
【问题描述】:

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


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

这是结果:

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

【问题讨论】:

  • 我希望您需要绘制一条与圆角相匹配的路径。

标签: c# winforms


【解决方案1】:

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

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(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)); } 这个?我将在哪里更改它?这对我来说真的很新鲜;(
  • @Hacki 在演示中,我调用了Form1_Paint 中的方法。只需将14修改为合适的值即可。
  • 虽然不是完美的剪辑,但无论如何都是很好的答案,我会接受的。我没有看到任何顺利的结果
猜你喜欢
  • 2012-10-26
  • 2017-05-27
  • 2013-04-06
  • 1970-01-01
  • 1970-01-01
  • 2023-03-30
  • 1970-01-01
  • 2011-09-04
  • 1970-01-01
相关资源
最近更新 更多