【问题标题】:Panel with Rounded Edges圆边面板
【发布时间】:2019-10-07 04:05:56
【问题描述】:

我正在使用 C# 和 winform 应用程序 .net 版本 3.5 和 Vs 2008

如何创建带有圆边的自定义面板?我们如何在不同的项目中使用该控件?

【问题讨论】:

  • 在另一个注意事项...当我想要一个圆形面板时,没有文本的组框几乎总是对我有用。唯一的例外是顶线上方烦人的标题区域,但通常这是由 z 排序和它上面的任何其他控件之间的间隙来处理的......

标签: c# winforms


【解决方案1】:

您必须覆盖 OnPaint 事件并使用 GraphicPath 对象绘制角。

看看这篇文章:http://www.switchonthecode.com/tutorials/csharp-creating-rounded-rectangles-using-a-graphics-path

【讨论】:

【解决方案2】:

按照MLablanc'slink我将代码转换为C#:

public class RoundedPanel : Panel {
    private float _thickness = 5;
    public float Thickness {
        get {
            return _thickness;
        }
        set {
            _thickness = value;
            _pen = new Pen(_borderColor,Thickness);
            Invalidate();
        }
    }

    private Color _borderColor = Color.White;
    public Color BorderColor {
        get {
            return _borderColor;
        }
        set {
            _borderColor = value;
            _pen = new Pen(_borderColor,Thickness);
            Invalidate();
        }
    }

    private int _radius = 20;
    public int Radius {
        get {
            return _radius;
        }
        set {
            _radius = value;
            Invalidate();
        }
    }

    private Pen _pen;

    public MpRoundedPanel() : base() {
        _pen = new Pen(BorderColor,Thickness);
        DoubleBuffered = true;            
    }
    private Rectangle GetLeftUpper(int e) {
        return new Rectangle(0,0,e,e);
    }
    private Rectangle GetRightUpper(int e) {
        return new Rectangle(Width - e,0,e,e);
    }
    private Rectangle GetRightLower(int e) {
        return new Rectangle(Width - e,Height - e,e,e);
    }
    private Rectangle GetLeftLower(int e) {
        return new Rectangle(0,Height - e,e,e);
    }

    private void ExtendedDraw(PaintEventArgs e) {
        e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
        GraphicsPath path = new GraphicsPath();
        path.StartFigure();
        path.AddArc(GetLeftUpper(Radius),180,90);
        path.AddLine(Radius,0,Width - Radius,0);
        path.AddArc(GetRightUpper(Radius),270,90);
        path.AddLine(Width,Radius,Width,Height - Radius);
        path.AddArc(GetRightLower(Radius),0,90);
        path.AddLine(Width - Radius,Height,Radius,Height);
        path.AddArc(GetLeftLower(Radius),90,90);
        path.AddLine(0,Height - Radius,0,Radius);
        path.CloseFigure();
        Region = new Region(path);
    }
    private void DrawSingleBorder(Graphics graphics) {
        graphics.DrawArc(_pen,new Rectangle(0,0,Radius,Radius),180,90);
        graphics.DrawArc(_pen,new Rectangle(Width - Radius - 1,-1,Radius,Radius),270,90);
        graphics.DrawArc(_pen,new Rectangle(Width - Radius - 1,Height - Radius - 1,Radius,Radius),0,90);
        graphics.DrawArc(_pen,new Rectangle(0,Height - Radius - 1,Radius,Radius),90,90);
        graphics.DrawRectangle(_pen,0.0f,0.0f,(float)Width - 1.0f,(float)Height - 1.0f);
    }
    private void Draw3DBorder(Graphics graphics) {
        DrawSingleBorder(graphics);
    }
    private void DrawBorder(Graphics graphics) {
        DrawSingleBorder(graphics);
    }
    protected override void OnPaint(PaintEventArgs e) {
        base.OnPaint(e);
        ExtendedDraw(e);
        DrawBorder(e.Graphics);
    }
}

要在您的项目中或在声明面板的同一文件中使用此类,请改用 RoundedPanel。

【讨论】:

    【解决方案3】:

    您可以使用 GDI+ 隐藏面板的边缘并绘制带有圆角的新边缘。

    这是example

    【讨论】:

      【解决方案4】:

      快速谷歌

      winforms 创建一个带有圆边的自定义面板

      返回以下作为第一个结果:

      C# Form with custom border and rounded edges

      要在其他项目中使用它,请将面板创建为 UserControl,而不是窗口。

      【讨论】:

      • "Kri-thick" 就像“Cryptic”第一部分的发音一样。我编的;它曾经是 Krynn “Krin”,但它演变和改变了,就像我自己一样。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多