c# 对窗体绘制圆角边框

              对于form, 当BorderStyle = None时, 绘制圆角, 需要使用graphicspath的AddArc方法。

              程序如下

              protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            //圆弧半径
            int cornerRadius = 7;
           //边框颜色
            Color c = Color.FromArgb(156, 156, 156);
            Pen p = new Pen(c);
            //边框工作区
            Rectangle rect = this.ClientRectangle;
            GraphicsPath Rect = new GraphicsPath();
            // 添加圆弧
            Rect.AddArc(0, 0, cornerRadius * 2, cornerRadius * 2, 180, 90);
            Rect.AddArc(rect.Width - cornerRadius * 2-1, 0, cornerRadius * 2, cornerRadius * 2, 270, 90);
            Rect.AddArc(rect.Width - cornerRadius * 2-1,  rect.Height - cornerRadius * 2-1, cornerRadius * 2, cornerRadius * 2, 0, 90);
            Rect.AddArc(0, rect.Height - cornerRadius * 2-1, cornerRadius * 2, cornerRadius * 2, 90, 90);
            Rect.CloseFigure();
            e.Graphics.DrawPath(p, Rect);
           
        }

       从控件左边起点画可以出席那边框,但是当从控件右边框使用 理论值画时,右边框不会出现边框,不清楚是为什么,我只好把横坐标左移1像素,即(rect.Height - cornerRadius * 2-1), 可以出现边框。


如果周围有多个控件,看不到边框效果,可以适当修改Pen.Width=2 ,不要太大,否则边框加粗

你可能感兴趣的:(窗体绘制圆角)