Pergunta

When the form is loading shouldn't the CreateGraphics() return a graphics object?

I mean, on the Form1_Load event can I write for example the following?

Graphics x;
private void Form1_Load(object sender, EventArgs e)
{
    x = this.CreateGraphics();
}

If not, then WHY?

I thought that when you create a new form, the constructor initiates the all object of the form. So why not also the graphics object?

I'm asking that, because when I'm trying to draw - on form_load, it doesn't show me what I draw.

The main reason is: I want to create a game, which have a board - so when the user click on the new game - first - I'm initiating the board and drawing it. And in the onPaint event I just want to draw the current state of the board. So I thought the the initial state of the board should be draw on the formLoad event.

Foi útil?

Solução

You should not use the Graphics object in this manner; you should enclose each usage of it in a using block, or otherwise make sure you dispose of it after each set of drawing operations. Your code here will leave an unnecessary Graphics object hanging around.

Brief example:

private void MyonPaintOverload()
{
    using(Graphics x = this.CreateGraphics())
    {
        // draw here...
    }
}

Also, drawing on Form_Load() won't work, because the window is not actually visible at that point; there's nothing to draw on, basically.

Yes, you generally need to redraw the whole thing each cycle - because something as simple as another window being dragged across your window will 'wipe out' your drawing, and when it's invalidated by the other window being moved away, you need to redraw everything that you manually drew.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top