Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Graphics
  4. draw and select object

draw and select object

Scheduled Pinned Locked Moved Graphics
graphicswinformshelpquestion
9 Posts 4 Posters 4 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mehdi0016
    wrote on last edited by
    #1

    hi i need help about drawing shape and object with gdi+ and then select each object by mouse and can change some properties ? thx

    M C 2 Replies Last reply
    0
    • M mehdi0016

      hi i need help about drawing shape and object with gdi+ and then select each object by mouse and can change some properties ? thx

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      What part of the process do you need help with? Mark

      Mark Salsbery Microsoft MVP - Visual C++ :java:

      M 1 Reply Last reply
      0
      • M Mark Salsbery

        What part of the process do you need help with? Mark

        Mark Salsbery Microsoft MVP - Visual C++ :java:

        M Offline
        M Offline
        mehdi0016
        wrote on last edited by
        #3

        how can i select a drawn object when mouse click on it? for example i draw a circle and then a rectangel. now i want to click on circle to select it and move it by mouse .

        M 1 Reply Last reply
        0
        • M mehdi0016

          how can i select a drawn object when mouse click on it? for example i draw a circle and then a rectangel. now i want to click on circle to select it and move it by mouse .

          M Offline
          M Offline
          Mark Salsbery
          wrote on last edited by
          #4

          It's up to you to keep track of where you draw the objects. With that saved info, you can perform "hit-testing", where you take the cursor position and determine which drawn object the cursor is on. To drag an object, the typical method is (in pseudocode): On WM_LBUTTONDOWN   if mousecursor on an object (hit-test)     capture mouse (SetCapture())     save base cursor position On WM_MOUSEMOVE   if mouse captured     if cursor position different than base cursor position       erase object at current its location       draw object at its new location (use delta from base cursor position)       set base cursor position to the current cursor position On WM_LBUTTONUP   if mouse captured     release mouse capture (ReleaseCapture()) MArk

          Mark Salsbery Microsoft MVP - Visual C++ :java:

          M F 2 Replies Last reply
          0
          • M Mark Salsbery

            It's up to you to keep track of where you draw the objects. With that saved info, you can perform "hit-testing", where you take the cursor position and determine which drawn object the cursor is on. To drag an object, the typical method is (in pseudocode): On WM_LBUTTONDOWN   if mousecursor on an object (hit-test)     capture mouse (SetCapture())     save base cursor position On WM_MOUSEMOVE   if mouse captured     if cursor position different than base cursor position       erase object at current its location       draw object at its new location (use delta from base cursor position)       set base cursor position to the current cursor position On WM_LBUTTONUP   if mouse captured     release mouse capture (ReleaseCapture()) MArk

            Mark Salsbery Microsoft MVP - Visual C++ :java:

            M Offline
            M Offline
            mehdi0016
            wrote on last edited by
            #5

            my problem excatly is how to hit testing ? which class and command must use ? thx

            M 1 Reply Last reply
            0
            • M Mark Salsbery

              It's up to you to keep track of where you draw the objects. With that saved info, you can perform "hit-testing", where you take the cursor position and determine which drawn object the cursor is on. To drag an object, the typical method is (in pseudocode): On WM_LBUTTONDOWN   if mousecursor on an object (hit-test)     capture mouse (SetCapture())     save base cursor position On WM_MOUSEMOVE   if mouse captured     if cursor position different than base cursor position       erase object at current its location       draw object at its new location (use delta from base cursor position)       set base cursor position to the current cursor position On WM_LBUTTONUP   if mouse captured     release mouse capture (ReleaseCapture()) MArk

              Mark Salsbery Microsoft MVP - Visual C++ :java:

              F Offline
              F Offline
              Force Code
              wrote on last edited by
              #6

              Mark Salsbery wrote:

              To drag an object, the typical method is (in pseudocode): On WM_LBUTTONDOWN if mousecursor on an object (hit-test) capture mouse (SetCapture()) save base cursor position On WM_MOUSEMOVE if mouse captured if cursor position different than base cursor position erase object at current its location draw object at its new location (use delta from base cursor position) set base cursor position to the current cursor position On WM_LBUTTONUP if mouse captured release mouse capture (ReleaseCapture())

              You only need SetCapture() to detect the mouse outside the client area of the application. You don't want to erase the entire object with each mouse move detection - only the part that's actually moved. If the mouse moves one pixel most of the object hasn't moved at all. If you erase the entire object each time there will be screen flashes. Well, maybe that's what you meant by "use delta from base cursor position". Nevermind.

              M 1 Reply Last reply
              0
              • F Force Code

                Mark Salsbery wrote:

                To drag an object, the typical method is (in pseudocode): On WM_LBUTTONDOWN if mousecursor on an object (hit-test) capture mouse (SetCapture()) save base cursor position On WM_MOUSEMOVE if mouse captured if cursor position different than base cursor position erase object at current its location draw object at its new location (use delta from base cursor position) set base cursor position to the current cursor position On WM_LBUTTONUP if mouse captured release mouse capture (ReleaseCapture())

                You only need SetCapture() to detect the mouse outside the client area of the application. You don't want to erase the entire object with each mouse move detection - only the part that's actually moved. If the mouse moves one pixel most of the object hasn't moved at all. If you erase the entire object each time there will be screen flashes. Well, maybe that's what you meant by "use delta from base cursor position". Nevermind.

                M Offline
                M Offline
                Mark Salsbery
                wrote on last edited by
                #7

                Heh thanks :) I'm trying to give him the basics without writing the whole thing.  I know the drawing issue is going to come up soon :) Cheers, Mark

                Mark Salsbery Microsoft MVP - Visual C++ :java:

                1 Reply Last reply
                0
                • M mehdi0016

                  my problem excatly is how to hit testing ? which class and command must use ? thx

                  M Offline
                  M Offline
                  Mark Salsbery
                  wrote on last edited by
                  #8

                  You need some way to decide if the cursor is on an object.  The simplest way is to use the bounding rectangle.  The problem with this is hollow, overlapped objects. You can use the PtInRect() API to test for a rectangle. A little more complicated is to use regions. You can use the PtInRegion() API to test for a rectangle. You can make it as complicated as it needs to be. There's lots of articles about hit testing.  Here's one example: Win32: Hit Testing Lines and Curves[^] Mark

                  Mark Salsbery Microsoft MVP - Visual C++ :java:

                  1 Reply Last reply
                  0
                  • M mehdi0016

                    hi i need help about drawing shape and object with gdi+ and then select each object by mouse and can change some properties ? thx

                    C Offline
                    C Offline
                    codeII
                    wrote on last edited by
                    #9

                    here is another approach the first thing you need to do is make a stack which supports all the graphics you are using: MyGraphicsStack; MyGraphicsStack.add( line, rect, color, ... ); <- user draws a line MyGraphicsStack.add( circle, rect, color, ..., fill ); <- user draws .. . . (or better, make objects and inherit from the same base class) Now when the user clicks, you create a white temporary bitmap with the same dimensions as the user is drawing on. the next thing is drawing your stack ( in the correct order ) on this temporary bitmap. For each item you are drawing, ignore the color in the GraphicsStack but use instead: (COLORREF)index_Of_Current_Object_In_MyStack. (index casting to a color) for filled objects: use this color for the fill/and the border color. Then transform your mouse coordinates where the user clicked, to bitmap coordinates. And for this coordination obtain the bitmap pixel color. When you cast the pixel color back it will be the index of the object kept in your stack, ( or white in case user pressed the bitmap ) UINT nInd = (COLORREF)getpixel(..) if ( nInd < MyGraphicsStack.Size( ) ) { MyGraphicsStack[ nInd ] //<-- here it is } in this way you have always the topmost object even when objects are drawn over each other. You also distinct objects which are filled or not. ( depending on the bit depth of the bitmap you are creating you are limited in the number of items you can draw, in that case you could work with more bitmaps, for a normal 24 bits bitmap you can keep 16777216 - 1 objects 1 = bitmap itself ) remark: Because Gdi+ plus can ignore drawing objects you are drawing outside the region of the dc, you can also create a small bitmap (I think 16x16 is the minimum?) and when drawing to this bitmap correct for the offset ( the bitmap coordinates the user selected ) in this way that the centre pixel of your small bitmap is the pixel you are interested in. well have fun!

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups