双缓冲/抗锯齿在自定义圆形面板控件上未生效

Double Buffering / Anti-aliasing not taking effect on custom rounded panel control

提问人:John Aldrich 提问时间:10/14/2023 更新时间:10/14/2023 访问量:17

问:

我有为另一个项目编写的以下自定义控件,但它似乎没有正确应用双重缓冲或抗锯齿。这是我第一次尝试编写这样的控件,所以我不明白发生了什么。

'使用 System.Drawing; 使用 System.Drawing.Drawing2D; 使用 System.Windows.Forms;

命名空间 RoundedCornerPanel {

public class RoundedCornerPanel : Panel
{
    private int cornerRadiusTopLeft = 10;
    private int cornerRadiusTopRight = 10;
    private int cornerRadiusBottomLeft = 10;
    private int cornerRadiusBottomRight = 10;

    public int CornerRadiusTopLeft
    {
        get { return cornerRadiusTopLeft; }
        set
        {
            cornerRadiusTopLeft = value;
            Invalidate();
        }
    }

    public int CornerRadiusTopRight
    {
        get { return cornerRadiusTopRight; }
        set
        {
            cornerRadiusTopRight = value;
            Invalidate();
        }
    }

    public int CornerRadiusBottomLeft
    {
        get { return cornerRadiusBottomLeft; }
        set
        {
            cornerRadiusBottomLeft = value;
            Invalidate();
        }
    }

    public int CornerRadiusBottomRight
    {
        get { return cornerRadiusBottomRight; }
        set
        {
            cornerRadiusBottomRight = value;
            Invalidate();
        }
    }

    public RoundedCornerPanel()
    {
        DoubleBuffered = true;
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        Graphics graphics = e.Graphics;
        graphics.SmoothingMode = SmoothingMode.AntiAlias;

        using (var path = new GraphicsPath())
        {
            int radiusTL = CornerRadiusTopLeft;
            int radiusTR = CornerRadiusTopRight;
            int radiusBL = CornerRadiusBottomLeft;
            int radiusBR = CornerRadiusBottomRight;
            int width = ClientRectangle.Width;
            int height = ClientRectangle.Height;

            if (radiusTL > 0)
                path.AddArc(0, 0, radiusTL * 2, radiusTL * 2, 180, 90);
            else
                path.AddLine(0, 0, 0, 0);

            if (radiusTR > 0)
                path.AddArc(width - radiusTR * 2, 0, radiusTR * 2, radiusTR * 2, 270, 90);
            else
                path.AddLine(width, 0, width, 0);

            if (radiusBR > 0)
                path.AddArc(width - radiusBR * 2, height - radiusBR * 2, radiusBR * 2, radiusBR * 2, 0, 90);
            else
                path.AddLine(width, height, width, height);

            if (radiusBL > 0)
                path.AddArc(0, height - radiusBL * 2, radiusBL * 2, radiusBL * 2, 90, 90);
            else
                path.AddLine(0, height, 0, height);

            path.CloseFigure();

            using (var brush = new SolidBrush(BackColor))
            {
                graphics.FillPath(brush, path);
            }

            Region = new Region(path); // Update the region
        }
    }

    protected override void OnResize(EventArgs eventargs)
    {
        base.OnResize(eventargs);
        Invalidate();
    }
}

}`

我希望角落会比它们现在更平滑一些,并且控件不会在绘制时闪烁

绘制 自定义控件 闪烁

评论


答: 暂无答案