【问题标题】:Label with smooth rounded corners带有光滑圆角的标签
【发布时间】:2017-07-26 10:06:42
【问题描述】:

两个标签都有AutoSize true 和TextAlign MiddleCenter。

label2如何也能显示平滑边框?

这里是处理程序Form.Load(...) & Form.Paint(...)的测试代码:

int _cornerRadius = 10;
Point _locationLabel2;

// Form.Load(...)
private void Form3_Load(object sender, EventArgs e)
{
    // Step 1: Cut the label regions (seems to be ok, result is the same for both labels)
    GraphicsPath graphicsPath = _getRoundPath(label1.ClientRectangle, _cornerRadius);
    label1.Region = new Region(graphicsPath);
    graphicsPath = _getRoundPath(label2.ClientRectangle, _cornerRadius);
    label2.Region = new Region(graphicsPath);

    _locationLabel2 = this.PointToClient(label2.Parent.PointToScreen(label2.Location));
}

// Form.Paint(...)
private void Form3_Paint(object sender, PaintEventArgs e)
{
    using (Pen pen = new Pen(label1.BackColor, 3.0f))
    {
        // Step 2: Smooth the label borders (ok only for label1)
        _drawRoundedRectangle(e.Graphics, pen, label1.Location.X, label1.Location.Y, 
                              label1.ClientRectangle.Width, label1.ClientRectangle.Height, _cornerRadius);
        _drawRoundedRectangle(e.Graphics, pen, _locationLabel2.X, _locationLabel2.Y,
                              label2.ClientRectangle.Width, label2.ClientRectangle.Height, _cornerRadius);
    }
}

// Helper 1/3
private static GraphicsPath _getRoundPath(Rectangle rectangle, int radius)
{
    int x = rectangle.X;
    int y = rectangle.Y;
    int width = rectangle.Width;
    int height = rectangle.Height;

    radius = radius << 1;

    GraphicsPath path = new GraphicsPath();

    if (radius > 0)
    {
        if (radius > height) radius = height;
        if (radius > width) radius = width;
        path.AddArc(x, y, radius, radius, 180, 90);
        path.AddArc(x + width - radius, y, radius, radius, 270, 90);
        path.AddArc(x + width - radius, y + height - radius, radius, radius, 0, 90);
        path.AddArc(x, y + height - radius, radius, radius, 90, 90);
        path.CloseFigure();
    }
    else
    {
        path.AddRectangle(rectangle);
    }

    return path;
}

// Helper 2/3
private void _drawRoundedRectangle(Graphics graphics, Pen pen, int x, int y, int width, int height, int radius)
{
    RectangleF rectangle = new RectangleF(x, y, width, height);
    GraphicsPath path = _generateRoundedRectangle(graphics, rectangle, radius);
    SmoothingMode old = graphics.SmoothingMode;
    graphics.SmoothingMode = SmoothingMode.AntiAlias;
    graphics.DrawPath(pen, path);
    graphics.SmoothingMode = old;
}

// Helper 3/3
private static GraphicsPath _generateRoundedRectangle(Graphics graphics, RectangleF rectangle, int radius)
{
    GraphicsPath path = new GraphicsPath();
    float diameter = radius * 2.0F;
    SizeF sizeF = new SizeF(diameter, diameter);
    RectangleF arc = new RectangleF(rectangle.Location, sizeF);

    path.AddArc(arc, 180, 90); 
    arc.X = rectangle.Right - diameter;
    path.AddArc(arc, 270, 90);
    arc.Y = rectangle.Bottom - diameter;
    path.AddArc(arc, 0, 90);
    arc.X = rectangle.Left;
    path.AddArc(arc, 90, 90); 
    path.CloseFigure();

    return path;
}

主要代码部分来自Arun Reginald Zaheeruddin

【问题讨论】:

  • 不可能,真的。 Regions 不允许抗锯齿,因此 SmoothingMode 将不允许任何抗锯齿像素。根据您的布局,您可能能够拥有它们,尽管就像您在 label1 中所做的那样。
  • 尝试使用path.AddBezier 而不是AddArc
  • 您真的需要更改区域吗?您可以像this example一样绘制支持Transparent背面颜色且圆角平滑的圆形标签。
  • 不要使用控件。画你想要的。

标签: c# winforms graphics controls panel


【解决方案1】:

@Reza Aghaei 根据answer 解决了这个问题。

解决方案

using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
public class RoundLabel : Label
{
    [Browsable(true)]
    public Color _BackColor { get; set; }

    public RoundLabel()
    {
        this.DoubleBuffered = true;
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
        using (var graphicsPath = _getRoundRectangle(this.ClientRectangle))
        {
            e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
            using (var brush = new SolidBrush(_BackColor))
                e.Graphics.FillPath(brush, graphicsPath);
            using (var pen = new Pen(_BackColor, 1.0f))
                e.Graphics.DrawPath(pen, graphicsPath);
            TextRenderer.DrawText(e.Graphics, Text, this.Font, this.ClientRectangle, this.ForeColor);
        }
    }

    private GraphicsPath _getRoundRectangle(Rectangle rectangle)
    {
        int cornerRadius = 15; // change this value according to your needs
        int diminisher = 1;
        GraphicsPath path = new GraphicsPath();
        path.AddArc(rectangle.X, rectangle.Y, cornerRadius, cornerRadius, 180, 90);
        path.AddArc(rectangle.X + rectangle.Width - cornerRadius - diminisher, rectangle.Y, cornerRadius, cornerRadius, 270, 90);
        path.AddArc(rectangle.X + rectangle.Width - cornerRadius - diminisher, rectangle.Y + rectangle.Height - cornerRadius - diminisher, cornerRadius, cornerRadius, 0, 90);
        path.AddArc(rectangle.X, rectangle.Y + rectangle.Height - cornerRadius - diminisher, cornerRadius, cornerRadius, 90, 90);
        path.CloseAllFigures();
        return path;
    }
}

【讨论】:

  • 它第一次工作,但之后我得到了一个正常的标签,虽然我正在尝试创建一个圆形标签。 VS2013.
  • @n.jmurov:你的问题还在吗?
  • 是的。稍后我可能会尝试附上屏幕截图。在代码中它们是 RoundLabels,但在屏幕上它们是正常的。我不相信代码中存在错误,因为它第一次工作。
  • 不错的解决方案:在 WinForms 上看起来很“自然”
  • @n.jmurov:我上传了一个小示例项目(VS 2010,.net 4.0),希望对您有所帮助 ==> drive.google.com/file/d/0B8sOSdWzK9AZTEpxbjc1YldZNWM/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-10-07
  • 1970-01-01
  • 2015-10-07
  • 2017-04-21
  • 1970-01-01
  • 2013-08-03
  • 2023-01-30
相关资源
最近更新 更多