How to draw a circle in C#
-
Yes there is, in a windows form app you can: private void Form1_Load(object sender, EventArgs e) { Pen pen = new Pen(Color.Black, 3); Graphics gfx = this.CreateGraphics(); gfx.DrawEllipse(pen, 20, 20, 50, 50); } That should get you on the right track :D
-
Yes there is, in a windows form app you can: private void Form1_Load(object sender, EventArgs e) { Pen pen = new Pen(Color.Black, 3); Graphics gfx = this.CreateGraphics(); gfx.DrawEllipse(pen, 20, 20, 50, 50); } That should get you on the right track :D
I made a slight mistake in my last reply, you should actually add the code to the form "paint" event: private void Form1_Paint(object sender, PaintEventArgs e) { Pen pen = new Pen(Color.Black, 3); Graphics gfx = this.CreateGraphics(); gfx.DrawEllipse(pen, 20, 20, 50, 50); } as the form "load" event doesnt apply graphical effects it just initialises its base values. :-D
-
I made a slight mistake in my last reply, you should actually add the code to the form "paint" event: private void Form1_Paint(object sender, PaintEventArgs e) { Pen pen = new Pen(Color.Black, 3); Graphics gfx = this.CreateGraphics(); gfx.DrawEllipse(pen, 20, 20, 50, 50); } as the form "load" event doesnt apply graphical effects it just initialises its base values. :-D
-
I made a slight mistake in my last reply, you should actually add the code to the form "paint" event: private void Form1_Paint(object sender, PaintEventArgs e) { Pen pen = new Pen(Color.Black, 3); Graphics gfx = this.CreateGraphics(); gfx.DrawEllipse(pen, 20, 20, 50, 50); } as the form "load" event doesnt apply graphical effects it just initialises its base values. :-D
A couple of points... 1. Your pen should be disposed (or better still enclosed in a using block.) 2. Any graphics object you create yourself should also be disposed (gfx) 2b. Creating grapics is 'expensive', there is already a Graphics object in e. 3. Better to override the OnPaint method. 3b. You should call the base's method at a suitable location. so the code would become...
protected override void OnPaint(PaintEventArgs e)
{
using (Pen pen = new Pen(Color.Black, 3))
{
e.Graphics.DrawEllipse(pen, 20, 20, 50, 50);
}
base.OnPaint(e);
}Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Why are you using VB6? Do you hate yourself? (Christian Graus) -
A couple of points... 1. Your pen should be disposed (or better still enclosed in a using block.) 2. Any graphics object you create yourself should also be disposed (gfx) 2b. Creating grapics is 'expensive', there is already a Graphics object in e. 3. Better to override the OnPaint method. 3b. You should call the base's method at a suitable location. so the code would become...
protected override void OnPaint(PaintEventArgs e)
{
using (Pen pen = new Pen(Color.Black, 3))
{
e.Graphics.DrawEllipse(pen, 20, 20, 50, 50);
}
base.OnPaint(e);
}Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Why are you using VB6? Do you hate yourself? (Christian Graus)Hi Dave,
DaveyM69 wrote:
You should call the base's method at a suitable location
Really? I never do that for Paint, and neither does this MSDN example[^]. So my questions are: why? what does it do for me? and if so, what is "a suitable location"? :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
Merry Christmas and a Happy New Year to all.
-
Hi Dave,
DaveyM69 wrote:
You should call the base's method at a suitable location
Really? I never do that for Paint, and neither does this MSDN example[^]. So my questions are: why? what does it do for me? and if so, what is "a suitable location"? :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
Merry Christmas and a Happy New Year to all.
Because otherwise (if overriding
OnPaint
rather than handling thePaint
event) thePaint
event will not get raised. I normally do it at the end of the method i.e. after all painting, however if there is some custom action that needs doing after all paint handlers have finished thenbase.OnPaint(e)
should be called before then. Edit: I've just followed the link a reread your post - In my point before I had suggested overriding OnPaint rather than handling the Paint event which is where the confusion comes from! I prefer to override OnPaint if painting as I can be sure that no subscribers to the Paint event will be called until I've done with painting. If I paint in the Paint event handler then other subscribers may already have been notified yet my painting hasn't even begun!Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Why are you using VB6? Do you hate yourself? (Christian Graus)modified on Saturday, January 2, 2010 9:32 AM