CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jun 2007
    Location
    New Delhi
    Posts
    70

    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

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    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.

  3. #3
    Join Date
    Jun 2007
    Location
    New Delhi
    Posts
    70

    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.

  4. #4
    Join Date
    Jun 2008
    Posts
    2,477

    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.

  5. #5
    Join Date
    Jun 2007
    Location
    New Delhi
    Posts
    70

    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.

  6. #6
    Join Date
    Jun 2008
    Posts
    2,477

    Re: drawing in win form

    Quote Originally Posted by john_avi View Post
    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.

  7. #7
    Join Date
    Jun 2007
    Location
    New Delhi
    Posts
    70

    Re: drawing in win form

    ok thanks for the help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured