C# 带圆边的面板

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11540117/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 18:12:10  来源:igfitidea点击:

Panel with Rounded Edges

c#winforms

提问by siddharth

I am using C# and winform application .net Version 3.5 and Vs 2008

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

How Would i Can create A custom Panel with the rounded edges ? How Can we use that Control in Different Project ?

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

采纳答案by DaveDev

A quick google on

一个快速的谷歌

winforms create A custom Panel with the rounded edges

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

returned the following as the first result:

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

C# Form with custom border and rounded edges

带有自定义边框和圆角边缘的 C# 表单

To use it in other projects, create the panel as a UserControl, rather than a window.

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

回答by Nickon

You can hide panel's edges and draw new ones with rounded corners using GDI+.

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

Here's an example.

这是一个例子

回答by MLeblanc

You have to override the OnPaint event and draw the corners using a GraphicPath object.

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

Take a look at this article : http://www.switchonthecode.com/tutorials/csharp-creating-rounded-rectangles-using-a-graphics-path

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

回答by 0tombo0

Following MLablanc'slinkI converted the code to C#:

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

To use just include this class in your project or in the same file you are declaring a Panel and use RoundedPanel instead.

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