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

23 Views Asked by At

I have the following custom control I've written for another project, but it doesn't seem to be applying double buffering or anti-aliasing correctly. This is my first attempt at writing a control like this so I'm not understanding what is happening.

`using System.Drawing; using System.Drawing.Drawing2D; using System.Windows.Forms;

namespace 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();
    }
}

}`

I am expecting that the corners would be a bit smoother than what they are and that the control would not flicker on draw

0

There are 0 best solutions below