|
-
February 17th, 2009, 01:14 AM
#1
drawing in win form
Hi All,
I am new to c# .net programming. I am making an application where I have to draw some shapes on win forms. I want to draw it after "draw" button is clicked. I tried to draw it using Graphics object. But its not appearing. I am attaching sample code.
Code:
private void DrawClicked(object sender, EventArgs e)
{
//location of "draw" button
Point ptBtn = buttonDraw.Location ;
int xCord = ptBtn.X;
int yCord = ptBtn.Y;
//location of win form
Point ptDlg = this.Location;
int xStart = ptDlg.X;
int yStart = ptDlg.Y;
//size of "draw" button.
int ibtnWidth = buttonDraw.Width ;
int btnHt = buttonDraw.Height ;
Graphics drawObj = buttonDraw.CreateGraphics();
Pen blackPen = new Pen(Color.Black, 3);
drawObj.DrawPie(blackPen, xStart + yCord, yCord + yStart + btnHt+10, 50, 50, 0, 30);
}
In this form, I have empty space below "draw" button and I want to draw below that button.
can somebody help me what is wrong with above code
-
February 17th, 2009, 01:21 AM
#2
Re: drawing in win form
If you would like to paint a control, you should override the OnPaint method of the canvas object and paint there. You can call Invalidate() when you need to cause a re-paint. Currently you are calling CreateGraphics() on the button, so your drawing surface is the button itself. Also, your arguments to DrawPie are strange. I haven't used it in a while, but I believe that it takes a start angle and a number representing the number of degrees to draw.
Last edited by BigEd781; February 17th, 2009 at 02:51 AM.
-
February 17th, 2009, 01:51 AM
#3
Re: drawing in win form
I have given some hard coded values for start angle and end angle. But I want to draw it after the "draw" button is clicked so how can I get a graphics object inside draw click event handler which will let me draw inside the client area of dialog.
-
February 17th, 2009, 01:55 AM
#4
Re: drawing in win form
You don't. You override the OnPaint method of your class and put the drawing logic there. When you need to repaint, call Invalidate() on your control.
-
February 17th, 2009, 02:12 AM
#5
Re: drawing in win form
then i"ll have to keep a switch case in onpaint() for different buttons clicked and different shapes drawing. which does not seem to be a fair solution.
-
February 17th, 2009, 02:18 AM
#6
Re: drawing in win form
 Originally Posted by john_avi
then i"ll have to keep a switch case in onpaint() for different buttons clicked and different shapes drawing. which does not seem to be a fair solution.
Well, your model is not great, so that is why you need the switch statement, not because of some deficiency in how painting is supposed to be done in .NET. Click a button and paint on a form is overly simplistic. A real graphics application would be much more complex than that.
-
February 17th, 2009, 02:29 AM
#7
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|