CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Oct 2007
    Posts
    9

    best way to remove shapes

    alright dudes/dudettes, i have a question about removing shapes in C#.

    firstly i am wanting to draw a small circle on the screen at the co-ordinates where the mouse is clicked, then i wish to remove this circle when a button is pressed. Now i could create as many circle as i like and then click on the button and they would all need to be removed.

    For this i could use the GDI+ or picture boxes but i am unsure which would be the best way to go. Picture boxes are fine but i don't want to be having picture boxes all over the place. However when when using GDI+ i am unsure how to delete a shape i have previously drawn.

    So what is the best way in C# using .net framework to draw and remove shapes on the screen? I am just after using the GDI+ and any form controls in .net framework 3.5, no dll extensions.

    p.s. i do not wish to remove all GDI+ graphics when the button is clicked, just the circles in which have been drawn. I know how to draw circles on the screen using GDI+ and how to use form controls but i do not know how to remove a shape drawn with the GDI+.

    Thanks
    Last edited by Sharkadder; August 3rd, 2009 at 10:13 AM.

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

    Re: best way to remove shapes

    You should be doing all of your drawing in OnPaint (GDI+ and using a PictureBox are not really separate options, GDI+ is simply the API for drawing to the screen). Keep each shape in some type of data structure (Rectangles probably) and draw them to the supplied Graphics object in OnPaint. When the user presses the button, simply empty out your collection of shapes and call invalidate.

  3. #3
    Join Date
    Oct 2007
    Posts
    9

    Re: best way to remove shapes

    ah i see, well i have used onpaint before and i have managed to change the colour of shapes before but not draw them depending on a mouses co-ordinate.

    I guess the part i am really confused about is removing the shape. You reckon that i should draw rectangles depending on the mouses x and y position, give it a name and when i want to remove the shapes, remove that shape name and all of the shapes will be removed when i invalidate. Is that what you mean?

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

    Re: best way to remove shapes

    I think you need to split these things apart a bit. For example, override OnMouseDown/OnMouseMove to grab the coordinates. Store the mouse state (up or down) and the coordinates in methods. That's all, just store information. If the mouse state has changed, or if you are moving with the mouse down, call Invalidate( ). Now, the OnPaint method simply draws according to the information already stored by the mouse handlers. There is no reason to get mouse coordinates from within OnPaint, OnPaint need only know about how many shapes to draw at which locations.

  5. #5
    Join Date
    Oct 2007
    Posts
    9

    Re: best way to remove shapes

    ah i see, well thanks for the reply, i shall have a real good look into it tomorrow and i may post some code up if i get stuck with anything.

    Thanks for the suggestions.

    EDIT

    Sorted now, thanks dudes
    Last edited by Sharkadder; August 4th, 2009 at 10:20 AM.

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