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?
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?
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.
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.
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 c# winforms
You are not signed in. Login or sign up in order to post.
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
– Luiz Vieira
@Did Luizvieira ever tell me why you’re closing? what the ramaral answered might be the answer to other people.
– Pekita
@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.
– ramaral
@ramaral Melhor?
– Pekita
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.
– Luiz Vieira
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. :)
– Luiz Vieira
@Luizvieira is right. Things got worse. I didn’t think about the other answer when he proposed the edition
– ramaral
@ramaral my question got worse?
– Pekita
@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.
– Luiz Vieira
@Pekita got better because "fits" my answer but got worse because it invalidates Bruno’s answer.
– ramaral
@then how can I fit in the answer of the two?
– Pekita
I don’t see how. However there is always the possibility of you reversing the edit.
– ramaral
@but if I reverse the issue people will vote to close
– Pekita