Newbie Question

  • Thread starter Thread starter KC Eric
  • Start date Start date
K

KC Eric

Hi all,

Suppose I have a form, and a button, the form has these functions:

private void button1_Click(object sender, System.EventArgs e)
{
Start();
}

public void Start()
{
Graphics g = this.CreateGraphics();
g.DrawString("start!", new Font("Arial", 12, FontStyle.Regular),
Brushes.Black, 460, 10);
g.Dispose();
}


Inside
private void InitializeComponent(),
I have
this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);

my question is,
after I click the button, the Start() is called and so "start!" would appear
in the form,
but if I move the form or resize the form, Form1_Paint() would called, and
"start!" would disappear,
is there any easy way to keep the "start!" ?

Thanks

KC Eric
 
Hi KC Eric,

Yes, it is simple to retain "start!", and you might want to read the
GDI+ FAQ as your question is listed as the #1 most asked questions :P

http://www.bobpowell.net/faqmain.htm

The answer to your question is to call Start() inside the Paint event
(Form1_Paint).
 
Thanks much for your quick response and the GDI+ FAQ!

So, the only way to retain the drawing is to do it inside Paint event, am I
correct? If so, then if I want to develop a chess game, then all the drawing
would be inside the Paint event, is there any better way to do this?

Thanks!

KC Eric
 
Generally speaking, the paint event is where your drawing should take
place. Your chess game should use some sort of data structure to
represent the current state of the board. Perhaps a class to represent
the board and another class to represent the pieces. The Board class
could have a Pieces collection as a property. The paint event would
simply draw the board according the data in the class objects.

The point is to separate the drawing code from the data model.
 
KC Eric said:
So, the only way to retain the drawing is to do it inside Paint event, am
I
correct? If so, then if I want to develop a chess game, then all the
drawing
would be inside the Paint event, is there any better way to do this?

Instead of redrawing everything every time the window is redrawn, you can
create a 'Bitmap' object of appropriate size, obtain a 'Graphics' object for
it using 'Graphics.FromImage' and draw onto the bitmap instead of drawing
onto the screen. In the form's 'Paint' event handler or 'OnPaint' method
you simply draw this bitmap onto the form ('Graphics.DrawImage').
 
If you are going to go through the trouble of drawing to a bitmap, why not
just put a picture box on the form and set it up so it covers the whole
form, and assign the bitmap to it.


--
Thanks
Wayne Sepega
Jacksonville, Fl


"When a man sits with a pretty girl for an hour, it seems like a minute. But
let him sit on a hot stove for a minute and it's longer than any hour.
That's relativity." - Albert Einstein
 
Back
Top