Like warping a button?

Asked

Viewed 101 times

0

How do I deform a button, using the Paint button event to have the shape of the key Enter or even the shape of a circle?

  • Hello, welcome(a) to SOPT. There are numerous ways to do this, so I will vote to close your question as very broad. One possible way is to create a custom component and implement the painting method. There are numerous tutorials on this subject, for example: https://www.youtube.com/watch?v=pd-S6UoQHh8

  • @Did Luizvieira ever tell me why you’re closing? what the ramaral answered might be the answer to other people.

  • 1

    @Pekita Although I answered I was the first to vote to close. The question is wide, however it can be edited and easily made more objective.

  • 1

    @ramaral Melhor?

  • I commented because I was looking at your question in the analysis queue, and I admit I didn’t realize (there, in the analysis queue) that she already had an answer. The answer is really useful, but the question remains broad because there are many other ways to do what you asked. You could, for example, use an image on a "normal" button. But since you just edited to specify that you want to do this as a custom component, I will withdraw my vote to close because the answer accepted fully meets.

  • But note how your edition has just become invalid the other answer you had. That’s the problem with big questions. They make your life difficult (as a person who asked, because you can get answers that aren’t exactly the ones you wanted) and the rest of the community (because people spend their time answering things that may not be useful to the questioner). May it serve as experience and learning for the future. :)

  • 1

    @Luizvieira is right. Things got worse. I didn’t think about the other answer when he proposed the edition

  • @ramaral my question got worse?

  • @I don’t think it’s gotten much worse. The amendment validated his reply more and, in my opinion, even left the question more objective (and, after all, it was already accepted). It got a little worse because the other answer (which was on the line to use the existing button component) no longer serves. But the question became even more objective.

  • 1

    @Pekita got better because "fits" my answer but got worse because it invalidates Bruno’s answer.

  • @then how can I fit in the answer of the two?

  • I don’t see how. However there is always the possibility of you reversing the edit.

  • 1

    @but if I reverse the issue people will vote to close

Show 8 more comments

2 answers

7


A simple way is to respond to the event Paint from the button, draw the desired shape there and assign it to the property Region button.

Example for a circular button(source)

//Este método transforma a forma do botão standard em circular,
// criando um forma em circulo e atribuindo-a à propriedade region do botão
private void roundButton_Paint(object sender, 
    System.Windows.Forms.PaintEventArgs e)
{

    System.Drawing.Drawing2D.GraphicsPath buttonPath = 
        new System.Drawing.Drawing2D.GraphicsPath();

    //Cria um rectangulo com o mesmo tamanho que o botão
    System.Drawing.Rectangle newRectangle = roundButton.ClientRectangle;

    // Diminui o tamanho do ractangulo.
    newRectangle.Inflate(-10, -10);

    // Desenha a borda do botão.
    e.Graphics.DrawEllipse(System.Drawing.Pens.Black, newRectangle);

    // Aumenta o tamanho do rectangulo para incluir a borda.
    newRectangle.Inflate( 1,  1);

    // Cria um circulo dentr0 do rectangulo.
    buttonPath.AddEllipse(newRectangle);

    //Atribui o circuloa propriedade region
    roundButton.Region = new System.Drawing.Region(buttonPath);

}

4

Pekita, the best that can be done with the standard Winforms Button (at least to my understanding) is to set your style to Flat (Flatstyle = Flat), remove its edges (Flatappearence.Bordersize = 0) and set an image to Backgroundimage.

Otherwise you can create a custom component with the dimensions and specifications you want, but the implementation and layout of this should be done manually, obviously you can inherit from the default Button object.

An example of how it could be done to my mind would be the junction of several quadrangular images that would together form the shape of the "Enter" button and when any of these images is pressed the click of the button is fired.

This may not be the most appropriate solution, but it’s what I could think of to help you. I suggest you wait to identify other possible suggestions or responses.

  • 1

    Look, the answer was wide. Then you gave your answer, which was good. But with the AP edition your answer became less valid. : / Even so, I gave you my +1 because the answer deserved earlier. :)

  • I apologise for my suggestion that the PA edit the question and invalidate its reply.

  • 1

    In no way ramaral, no problem. We are here to contribute to the community and the answer that has been accepted indicates a better solution to the problem proposed by the question, even after editing. I’m sure Pekita took better advantage of your answer in this context.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.