1
I made an example when this happens pisca-pisca
, just put the control Panel
in the form and execute.
public Form1()
{
InitializeComponent();
this.KeyDown += Form1_KeyDown;
this.KeyPress += Form1_KeyPress;
this.panel1.Paint += panel1_Paint;
rectangle = new Rectangle()
{
Width = 20,
Height = 20,
};
}
Rectangle rectangle;
Keys keys;
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
keys = e.KeyCode;
}
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
this.panel1.Invalidate();
switch (keys)
{
case Keys.D:
{
rectangle.X += 10;
}
break;
case Keys.A:
{
rectangle.X -= 10;
}
break;
case Keys.W:
{
rectangle.Y -= 10;
}
break;
case Keys.S:
{
rectangle.Y += 10;
}
break;
default:
break;
}
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.FillRectangle(new SolidBrush(Color.Red), rectangle);
e.Graphics.DrawRectangle(new Pen(Color.Blue, 10), this.panel1.ClientRectangle);
}
You can tell by pressing one of the Keys
above, the edge of the Panel
and even the retângulo
blink simultaneously.
When it happened in the Form
, I gave the following command:
this.DoubleBuffered = true;
Then it would stop blinking, but on the panel it doesn’t have that. What to do?
I can do it with any control?
– sYsTeM
Yes, because the problem is not the control itself but the loading of the form
– Julio Borges
Remembering that this code is not silver bullet, it depends a lot on the amount of controls in the form and what each control carries, more solves most of the time.
– Julio Borges