2

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

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

4

4 回答 4

1

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

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

于 2012-07-18T11:25:25.307 回答
1

按照MLablanc 的 链接,我将代码转换为 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);
    }
}

要在您的项目或同一文件中使用包含此类,您将声明一个 Panel 并改用 RoundedPanel。

于 2019-10-04T06:38:21.280 回答
0

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

这是一个例子

于 2012-07-18T11:21:58.083 回答
-1

一个快速的谷歌上

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

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

具有自定义边框和圆角边缘的 C# 窗体

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

于 2012-07-18T11:21:14.003 回答