By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,330 Members | 1,384 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,330 IT Pros & Developers. It's quick & easy.

A question regarding to drawing in C#

P: n/a
Hi, thanks for the answers to my previous questions.

Now I have a question on drawing in C#:

For example I have written following code to draw a rectangle on a form:

protected override void OnPaint ( PaintEventArgs e)
{
Graphics dc = e.Graphics;
Pen BluePen = new Pen(Volor.Blue, 3);
dc.DrawRectangle(BluePen, 0, 0, 50, 50);
base.OnPaint (e);
}

The reason I put the code in this method instead of puting it into the
constructor is that
I want to move and redraw the rectangle later. Also is it is because if I
put the drawing code in the
constructor the rectangle would be "erased" when I move another window (say,
Task Manager)
over the rectangle.

Now I want to re-position the rectangle, say move to (10, 20), whenever I
click it.
Presumebly I should put the code for redrawing into the following method:

private void OnMouseDownEvent(object sender, MouseEventArgs e)

But how can I call the OnPaint method from the OnMouseDownEvent method?

Anyone has ideas? Thanks.

Yuelin


Nov 13 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Hello Yuelin,

Well you need to store the location to draw the rectangle in a variable in
the "MouseDown" event. Then call "this.Invalidate()" which will invalidate
the graphics of the control causing them to be redrawn.

This is used so that if the control does not need to be redrawn at this time
(it is minimized or another control/form is on top), then the system does
not waste resources redrawing the control until it is visible to the user.

Good luck!
-Noah Coad
Microsoft MVP & MCP [.NET/C#]

"Yuelin" <li***@lineone.net> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
Hi, thanks for the answers to my previous questions.

Now I have a question on drawing in C#:

For example I have written following code to draw a rectangle on a form:

protected override void OnPaint ( PaintEventArgs e)
{
Graphics dc = e.Graphics;
Pen BluePen = new Pen(Volor.Blue, 3);
dc.DrawRectangle(BluePen, 0, 0, 50, 50);
base.OnPaint (e);
}

The reason I put the code in this method instead of puting it into the
constructor is that
I want to move and redraw the rectangle later. Also is it is because if I
put the drawing code in the
constructor the rectangle would be "erased" when I move another window (say, Task Manager)
over the rectangle.

Now I want to re-position the rectangle, say move to (10, 20), whenever I
click it.
Presumebly I should put the code for redrawing into the following method:

private void OnMouseDownEvent(object sender, MouseEventArgs e)

But how can I call the OnPaint method from the OnMouseDownEvent method?

Anyone has ideas? Thanks.

Yuelin


Nov 13 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.