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

C# 2005 and simple graphics

P: n/a
I have an empty project with a button on a form.

What code do I need to put behind the button to draw a rectangle on my form
when it is clicked?
Aug 23 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Hello, Delme!

DGI have an empty project with a button on a form.

DGWhat code do I need to put behind the button to draw a rectangle on my
DGform when it is clicked?

You will need to use GDI+
Have a look at ( http://www.c-sharpcorner.com/2/gdi_plus.asp )

--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
Aug 23 '06 #2

P: n/a
Thank you very much, having not done anything with graphics I thought I
would start somewhere simple.

Great link.

Delme.
"Vadym Stetsyak" <va*****@ukr.netwrote in message
news:OZ**************@TK2MSFTNGP04.phx.gbl...
Hello, Delme!

DGI have an empty project with a button on a form.

DGWhat code do I need to put behind the button to draw a rectangle on my
DGform when it is clicked?

You will need to use GDI+
Have a look at ( http://www.c-sharpcorner.com/2/gdi_plus.asp )

--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com

Aug 23 '06 #3

P: n/a
You should also check out Bob Powell's GDI+ FAQ:

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

--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

It takes a tough man to make a tender chicken salad.
"Delme Greening" <this_part_is_rubbish@de***@i-byte.co.ukwrote in message
news:%2******************@TK2MSFTNGP02.phx.gbl...
Thank you very much, having not done anything with graphics I thought I
would start somewhere simple.

Great link.

Delme.
"Vadym Stetsyak" <va*****@ukr.netwrote in message
news:OZ**************@TK2MSFTNGP04.phx.gbl...
>Hello, Delme!

DGI have an empty project with a button on a form.

DGWhat code do I need to put behind the button to draw a rectangle on
my
DGform when it is clicked?

You will need to use GDI+
Have a look at ( http://www.c-sharpcorner.com/2/gdi_plus.asp )

--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com


Aug 23 '06 #4

P: n/a
Hi Delme,

Basically, all draing is done using a Graphics object, which can point to
a control, your main form (which is also a control), or a Bitmap image and
so on.

The quick and dirty way to draw using a button event is something like this

// constructor
public Form1()
{
Button b = new Button();
this.Controls.Add(b);
b.Click += new EventHandler(b_Click);
}

// click event method
void b_Click(object sender, EventArgs e)
{
using (Graphics g = this.CreateGraphics())
{
g.DrawEllipse(Pens.Black, this.ClientRectangle);
}
}

But!!!, you should avoid drawing inside your button event for several
reasons.
1) Any drawing will be lost if someting covers the window.
2) Using the graphics object of a control directly may cause unwanted side
effects

Instead do something like this instead

bool flag = false;
void b_Click(object sender, EventArgs e)
{
flag = true;
this.Refresh();
}

protected override void OnPaint(PaintEventArgs e)
{
if (flag)
e.Graphics.DrawEllipse(Pens.Black, this.ClientRectangle);
}

OnPaint will be called each time the window needs to be repainted, like
when it is uncovered.

If you swap 'flag = true' with 'flag = !flag' you show and hide the circle.

Good luck!

On Wed, 23 Aug 2006 14:27:56 +0200, Delme Greening
<this_part_is_rubbish@de***@i-byte.co.ukwrote:
Thank you very much, having not done anything with graphics I thought I
would start somewhere simple.

Great link.

Delme.
"Vadym Stetsyak" <va*****@ukr.netwrote in message
news:OZ**************@TK2MSFTNGP04.phx.gbl...
>Hello, Delme!

DGI have an empty project with a button on a form.

DGWhat code do I need to put behind the button to draw a rectangle on
my
DGform when it is clicked?

You will need to use GDI+
Have a look at ( http://www.c-sharpcorner.com/2/gdi_plus.asp )

--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com



--
Happy Coding!
Morten Wennevik [C# MVP]
Aug 23 '06 #5

P: n/a

"Delme Greening" <this_part_is_rubbish@de***@i-byte.co.ukwrote in message
news:u1**************@TK2MSFTNGP06.phx.gbl...
>I have an empty project with a button on a form.

What code do I need to put behind the button to draw a rectangle on my
form when it is clicked?
Also, along with the other good replies, you may want to check out Managed
DirectX if you are gearing more towards Gaming. If you are doing graphics
for business applications and don't need DirectX, then GDI+ is the way to go
:)

HTH,
Mythran
Aug 23 '06 #6

P: n/a
Morten,

This isn't completely true.

The drawing will be lost when a repaint is forced. This can be due to
many factors, not just having the window covered. You can resize the
window, you can have it covered, a call to invalidate the window could be
made, the display settings could be changed.

Also, I am curious, what side effects are you getting by using the DC of
a control? When you override the OnPaint method, or attach to the Paint
event, the DC of the control is exactly what you are getting.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Morten Wennevik" <Mo************@hotmail.comwrote in message
news:op***************@tr024.bouvet.no...
Hi Delme,

Basically, all draing is done using a Graphics object, which can point to
a control, your main form (which is also a control), or a Bitmap image and
so on.

The quick and dirty way to draw using a button event is something like this

// constructor
public Form1()
{
Button b = new Button();
this.Controls.Add(b);
b.Click += new EventHandler(b_Click);
}

// click event method
void b_Click(object sender, EventArgs e)
{
using (Graphics g = this.CreateGraphics())
{
g.DrawEllipse(Pens.Black, this.ClientRectangle);
}
}

But!!!, you should avoid drawing inside your button event for several
reasons.
1) Any drawing will be lost if someting covers the window.
2) Using the graphics object of a control directly may cause unwanted side
effects

Instead do something like this instead

bool flag = false;
void b_Click(object sender, EventArgs e)
{
flag = true;
this.Refresh();
}

protected override void OnPaint(PaintEventArgs e)
{
if (flag)
e.Graphics.DrawEllipse(Pens.Black, this.ClientRectangle);
}

OnPaint will be called each time the window needs to be repainted, like
when it is uncovered.

If you swap 'flag = true' with 'flag = !flag' you show and hide the circle.

Good luck!

On Wed, 23 Aug 2006 14:27:56 +0200, Delme Greening
<this_part_is_rubbish@de***@i-byte.co.ukwrote:
Thank you very much, having not done anything with graphics I thought I
would start somewhere simple.

Great link.

Delme.
"Vadym Stetsyak" <va*****@ukr.netwrote in message
news:OZ**************@TK2MSFTNGP04.phx.gbl...
>Hello, Delme!

DGI have an empty project with a button on a form.

DGWhat code do I need to put behind the button to draw a rectangle on
my
DGform when it is clicked?

You will need to use GDI+
Have a look at ( http://www.c-sharpcorner.com/2/gdi_plus.asp )

--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com



--
Happy Coding!
Morten Wennevik [C# MVP]
Aug 24 '06 #7

P: n/a
Nicholas,

Yes, but isn't it completely true that it will be lost when the control is
covered?

As for unwanted side effects:

[How to use the CreateGraphics method.]
http://www.bobpowell.net/creategraphics.htm
On Thu, 24 Aug 2006 17:31:32 +0200, Nicholas Paldino [.NET/C# MVP]
<mv*@spam.guard.caspershouse.comwrote:
Morten,

This isn't completely true.

The drawing will be lost when a repaint is forced. This can be due
to
many factors, not just having the window covered. You can resize the
window, you can have it covered, a call to invalidate the window could be
made, the display settings could be changed.

Also, I am curious, what side effects are you getting by using the
DC of
a control? When you override the OnPaint method, or attach to the Paint
event, the DC of the control is exactly what you are getting.



--
Happy Coding!
Morten Wennevik [C# MVP]
Aug 25 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.