How to make a button with rounded corners in C # (Winforms)

1

I'm trying to customize the appearance of a button, basically I'm trying to customize the edges, background, font color and round the corners of the button. I was able to do everything but round the corners of the button . I tried following a tutorial , but it turned out that instead of creating with rounded corners was creating an ellipse.

public class Button : System.Windows.Forms.Button
    {
    protected override void OnPaint(PaintEventArgs e) {
        Color bc = Color.Black;
        Color bg = Color.Red;
        Color fc = Color.White;
        StringFormat formatter = new StringFormat();

        base.OnPaint(e);
        formatter.LineAlignment = StringAlignment.Center;
        formatter.Alignment = StringAlignment.Center;
        RectangleF rectangle = new RectangleF(0, 0, e.ClipRectangle.Width, e.ClipRectangle.Height);

        e.Graphics.FillRectangle(new SolidBrush(bg), e.ClipRectangle);
        ControlPaint.DrawBorder(e.Graphics, e.ClipRectangle, bc, ButtonBorderStyle.Solid);
        e.Graphics.DrawString(this.Text, this.Font, new SolidBrush(fc), rectangle, formatter);
    }
}

I need help getting round the edges.

    
asked by anonymous 11.01.2015 / 01:25

1 answer

2

On the MSDN site has an example that is working , the code snippet below was taken from there.

I changed the name of the function SuArredondaRect to BorderRadius , but it is working. For this code to work the pCanto must be greater than 0.

class Transform
{
    public static GraphicsPath BorderRadius(Rectangle pRect, int pCanto, bool pTopo, bool pBase) {
        GraphicsPath gp = new GraphicsPath();

        if (pTopo) {
            gp.AddArc(pRect.X - 1, pRect.Y - 1, pCanto, pCanto, 180, 90);
            gp.AddArc(pRect.X + pRect.Width - pCanto, pRect.Y - 1, pCanto, pCanto, 270, 90);
        } else {
            // Se não arredondar o topo, adiciona as linhas para poder fechar o retangulo junto com
            // a base arredondada
            gp.AddLine(pRect.X - 1, pRect.Y - 1, pRect.X + pRect.Width, pRect.Y - 1);
        }

        if (pBase) {
            gp.AddArc(pRect.X + pRect.Width - pCanto, pRect.Y + pRect.Height - pCanto, pCanto, pCanto, 0, 90);
            gp.AddArc(pRect.X - 1, pRect.Y + pRect.Height - pCanto, pCanto, pCanto, 90, 90);
        } else {
            // Se não arredondar a base, adiciona as linhas para poder fechar o retangulo junto com
            // o topo arredondado. Adiciona da direita para esquerda pois é na ordem contrária que 
            // foi adicionado os arcos do topo. E pra fechar o retangulo tem que desenhar na ordem :
            //  1 - Canto Superior Esquerdo
            //  2 - Canto Superior Direito
            //  3 - Canto Inferior Direito 
            //  4 - Canto Inferior Esquerdo.
            gp.AddLine(pRect.X + pRect.Width, pRect.Y + pRect.Height, pRect.X - 1, pRect.Y + pRect.Height);
        }

        return gp;
    }
}

To work, just call the method BorderRadius of class Transform (the code snippet above). An example call based on the current code would be:

public class Button : System.Windows.Forms.Button
    {
    protected override void OnPaint(PaintEventArgs e) {
        Color bc = Color.Black;
        Color bg = Color.Red;
        Color fc = Color.White;
        StringFormat formatter = new StringFormat();

        base.OnPaint(e);
        formatter.LineAlignment = StringAlignment.Center;
        formatter.Alignment = StringAlignment.Center;
        RectangleF rectangle = new RectangleF(0, 0, e.ClipRectangle.Width, e.ClipRectangle.Height);

        e.Graphics.FillRectangle(new SolidBrush(bg), e.ClipRectangle);
        ControlPaint.DrawBorder(e.Graphics, e.ClipRectangle, bc, ButtonBorderStyle.Solid);
        e.Graphics.DrawString(this.Text, this.Font, new SolidBrush(fc), rectangle, formatter);

        // BorderRadius é uma variável do tipo inteiro que define a quantidade que a borda deve ser arredondada.
        if (BorderRadius > 0) {
            GraphicsPath gp = Transform.BorderRadius(ClientRectangle, BorderRadius, true, true);
            this.Region = new System.Drawing.Region(gp);
        }
    }
}
    
11.01.2015 / 12:42