How to change the shape of the buttons in Windows Forms c #?

5

Does anyone have any idea how to make the shape of the Windows Forms default buttons change? For example a round button

    
asked by Alvaro Duron 04.12.2017 в 22:14
source

2 answers

8

In Windows Forms (Winforms) you will have to use third-party controls, or if you want to create yours you have to inherit from the Button class and overload the OnPaint method, for example:

public class BtnRedondo : Button
{
    protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
    {
        GraphicsPath grPath = new GraphicsPath();
        grPath.AddEllipse(0, 0, ClientSize.Width, ClientSize.Height);
        this.Region = new System.Drawing.Region(grPath);
        base.OnPaint(e);
    }
}

But I recommend better to look for a third-party library with controls that comply with what you require, look at Github.com or CodeProject, for example here you have one: link

Or better yet, using WPF, which has a lot more freedom in the design of controls, Winforms is a pretty old-fashioned technology.

    
answered by 05.12.2017 / 03:21
source
2

As stated in the previous answer, you can use third-party libraries.

Another way you can create your own button style.

  

You can do it by using a UserControl ( User Control) , which you can inherit from the Button class

Here a Basic example you can set the FlatStyle: Flat property of the button. Here what you do is that you assign an image to the button depending on:

  • If the user hovers the mouse over the
  • button
  • If the user leaves the mouse over the button
  • If the user clicks on the button.
  • ....
  • is something very basic, it's just an example.

        public partial class BotonPersonalizado: Button
        {
            public BotonPersonalizado()
            {
                InitializeComponent();
            }
    
            //Implementar todos los métodos y propiedades que desees...
    
            private void Boton_MouseDown(object sender, MouseEventArgs e)
            {
               Boton.BackgroundImage = NombreProyecto.Properties.Resources.Tuimagen;
            }
    
            private void Boton_MouseEnter(object sender, EventArgs e)
            {
               Boton.BackgroundImage = NombreProyecto.Properties.Resources.Tuimagen;
            }
    
            private void Boton_MouseLeave(object sender, EventArgs e)
            {
                Boton.BackgroundImage = NombreProyecto.Properties.Resources.Tuimagen;
            }
    
            private void Boton_MouseUp(object sender, MouseEventArgs e)
            {
               Boton.BackgroundImage = NombreProyecto.Properties.Resources.Tuimagen;
            }
        }
    

    When inheriting from the button class, the user control has all the properties that a button contains and you can use that button as many times as you want in all the forms.

    The custom button will be added to the Toolbox

        
    answered by 23.12.2017 в 15:02