Thread: [C] drawing: drag GDI shapes

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    49

    [C] drawing: drag GDI shapes

    salutations,

    we are beginners.
    doubt: we would like to know what is the most convenient method to select and drag a shape drawn with Windows GDI (rectangles, ellipses, polygons...) inside a window or dialog box. for example, if we have drawn several ellipses (Ellipse() function) in a window's client area, we should be able to click one of the ellipses and drag it without affecting the other ellipses.
    we have thought of a (naïve) solution, but we don't know if it is correct, and it seems a little complicated to implement:
    to have a (dynamic?) array of structs in which every struct has all the informations necessary to draw one polygon (type, position, color, whether it's selected or not etc.). when the user clicks the window, the program loops through the array of structs until it finds a struct where the cursor's position is inside a shape's limits. if it does, it selects it. when the user drags the cursor, the position information in the struct of the selected shape is continually changed according to the cursor's position (maybe inside WM_MOUSEMOVE). then we should program WM_PAINT to draw each shape from the array of structs and redraw the window each time positions change.
    thus, the act of clicking and moving changes information inside an array of structs which is used by WM_PAINT to draw shapes on the window. thus, the user is able to add more objects to the screen by adding more structs to the array.
    what do you think? is there a better way?

    thank you in advance.

  2. #2
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    There was a section on this in pertzold's book. The general gist of it was to draw your objects and on WM_LBUTTONDOWN hit test your object by seeing if the coordinates were in the RECT coords of your shape, then draw the shape and update its coordinates on the WM_LBUTTONUP new mouse coordinates. This will only draw it after the mouse button is lifted though, constantly redrawing it on WM_MOUSEMOVE could be an option too.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    49
    Quote Originally Posted by valaris View Post
    There was a section on this in pertzold's book. The general gist of it was to draw your objects and on WM_LBUTTONDOWN hit test your object by seeing if the coordinates were in the RECT coords of your shape, then draw the shape and update its coordinates on the WM_LBUTTONUP new mouse coordinates. This will only draw it after the mouse button is lifted though, constantly redrawing it on WM_MOUSEMOVE could be an option too.
    thank you for the response.
    but, to see if the coordinates are in the RECT coordinates of our shape, we would have to store those coordinates somewhere, right? like an array, like we told, or maybe a vector in C++.
    because, as far as we know, when a shape is drawn in the window, it "merges" with the window, thus it's not a "separate" object anymore... if you understand what we meant. so, the whole image has to be redrawn so that one object can be moved.
    is that right?
    thank you in advance.

  4. #4
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    Yes you must store the coordinates of each shape. If you know how many you are going to have use an array, otherwise yah throw them in a vector. Most people just create a static array/vector in the winProc that it will be drawn.

    The shape doesn't become "part" of the window, its simply drawn inside the client area usually.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [C] GDI: how to erase material drawn at an entire screen DC
    By pc2-brazil in forum Windows Programming
    Replies: 3
    Last Post: 01-24-2009, 07:24 PM
  2. [C] Drag and drop controls
    By pc2-brazil in forum Windows Programming
    Replies: 1
    Last Post: 09-02-2008, 02:41 AM
  3. GDI - Drawing to the screen correctly
    By Tonto in forum Windows Programming
    Replies: 1
    Last Post: 06-24-2006, 09:53 PM
  4. GDI Drawing Help
    By The Brain in forum Windows Programming
    Replies: 3
    Last Post: 10-20-2005, 02:01 AM
  5. drawing sprites with the windows gdi
    By lambs4 in forum Game Programming
    Replies: 10
    Last Post: 08-14-2003, 09:06 AM