Reputation: 4064
I wrote a program that draws a specific situation of a board game(I think it's called "Ludo" in English). Now I can draw all the fields and the pawns etc., but if a pawn is moved I have to redraw that pawn. The problem is that the screen flashes for a very short time when I do this. This is probably because I first clear all fields and pawns, and then redraw them(I'm going to improve this, but the problem will, in smaller form, still occur I think), the screen is then flashing between the time I cleared everything until I redrawed everything. Is there a way to tell C# not to redraw even when I specifically call something like this.Controls.Clear()
until I tell it to?
I already tried it with this.SuspendLayout()
, because the name suggests it should do exactly this, but that doesn't work.
Upvotes: 1
Views: 352
Reputation: 18353
Although @KeightS's solution is nice, I'd much rather solve it by being able to suspend/resume drawing all together:
public partial class Form1 : Form
{
private bool suspendDraw;
public Form1()
{
this.InitializeComponent();
}
public void SuspendDraw()
{
this.suspendDraw = true;
}
public void ResumeDraw()
{
this.suspendDraw = false;
}
private const int WM_PAINT = 0x000F;
protected override void WndProc(ref Message m)
{
if ((m.Msg != WM_PAINT) || (!this.suspendDraw && m.Msg == WM_PAINT))
{
base.WndProc(ref m);
}
}
}
You just need to call the SuspendDraw/ResumeDraw()
methods to control allowing painting.
Upvotes: 0
Reputation: 33252
Best solution is the one of KeithS, but a faster option could be: have two images for each part, one on the white and one on the black, plus both empty square. In the move place the proper empty square in the leaving square, and the proper part on gthe destination square. This is a zero flicker, old ages ( so fast ) approach :)
Upvotes: 0
Reputation: 71573
Flickering is always going to be somewhat of a problem in Winforms custom graphics.
Try this: Instead of drawing the pawn's positions on the Form somewhere, draw them onto a Bitmap. When it's all done, simply change the Image of a PictureBox on your form to be the Bitmap. The form will be redrawn using the new Image. You can use two Bitmap objects (which are admittedly quite large) and clean them each time to avoid the program being a memory hog. Voila, you now have "page-flipping" graphics, which can be drawn and displayed independently of other requests to redraw the form.
Upvotes: 4
Reputation: 389
When you tell it to clear everything and then redraw it, it will flash like that. But how come when you move a pawn you don't draw it in the new space and draw a blank over top of where it used to be. This will move the pawn without refreshing the whole screen or clearing everything.
Also I think the game you are talking about is Chess. Thats the only game I know with Pawns, and I've never heard of Ludo. :P
Upvotes: 0