473,406 Members | 2,894 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

A question regarding to drawing in C#

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
1 4552
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Champika Nirosh | last post by:
Hi, I want to create drawing board application that can draw Line, rectagle, circle and free hand drawing. Each drawing need to be transparent, moveable (draggable), have bring to front and...
0
by: Chenghui Li | last post by:
We have a problem with the Windows XP theme: We have a IDE which allows other developers to develop visual programs for their customers. Our IDE allow them to set font for window captions easyly...
1
by: Hadar | last post by:
Hi, I'm getting "object is currently in use elsewhere" when I use System.Drawing.Graphics.MesureString. This is what I do: My controls use a utility class the helps it to mesure strings. To...
12
by: jason ji | last post by:
Hi, buddies I met expression like"$this" to reference a current mainform control in VB.NET Winforms programming. I don't know what $this stand for and how to use $. please advise. thanks, ...
5
by: Jerry J | last post by:
I want to use the System.Drawing.Image class. According to the help file, this is an abstract base class. Because it is supposedly abstract, I created another class that inherits from it. However,...
10
by: Rob | last post by:
I am reading a book that says that the "name" property can be altered only at design time and cannot be modified at runtime. Please explain this given the code below... If you click Button3......
1
by: YouPoP | last post by:
I am doing an app (C# 2.0) where you can draw in a panel with your mouse in "real time". I actually have 2 problems; 1- it does not really is "real time", if your mouse move fast or very fast the...
2
by: ThatsIT.net.au | last post by:
I have this code that writes a pie chart in a asp.net page, but I want to use it in a server control. When I try I get a error on the last line "Response.OutputStream" Obviously there is no...
7
by: raylopez99 | last post by:
I have a logical drawing space much bigger than the viewport (the screen) and I'd like to center the viewport (the screen) to be at the center of the logical drawing space. After following the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.