views:

49

answers:

1

I just started on the .net compact framework. I want to draw a Sudoku field on the screen. So I put down a PictureBox and defined a method for the Paint event:

private void pictureBoxPlayfield_Paint(object sender, PaintEventArgs e)
{
    // use e.Graphics to draw the grid, numbers and cursor
}

This works, but you can see as the grid is drawn. So my question is, what is the right/better way to create such a custom control? Is there maybe a way to enable double buffering?

+2  A: 

There is no built-in support for double buffering in the Compact Framework. You can add it yourself, PictureBox already supports the Image property. Create a Bitmap in the constructor and assign it to Image. You don't need the Paint event anymore, the one provided by PictureBox already draws it to the screen.

Whenever the image needs to change, create a Graphics object with Graphics.FromImage(), passing the PB's Image and draw your stuff. Call the PB's Invalidate() method to tell it that it needs to redraw the image. If you still see flicker, override the PB's OnPaintBackground() method and do nothing.

The only other consideration is handling resizing, you'd need a larger or smaller Bitmap. Not so sure that would be necessary for a game.

Hans Passant
Works just fine, thanks :)
Hinek