Announcement

Collapse
No announcement yet.

Dragdetect

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Dragdetect

    I chanced upon the DragDetect API the other day but so far I cannot make it work.

    What it is supposed to do: msdn

    What happens in my clumsy hands: the first few iterations of the while loop do just what I would expect. Then - GPF!

    Here is my attempt in code. Any ideas on how to make this API work would be appreciated:
    Code:
    ' to investigate DragDetect API
    ' Chris Holbrook 10 Dec 2008
    
    #compile exe
    #dim all
    #include once "win32api.inc"
    
    %pic_margin = 3
    
    global GrDialogProc as long               ' used in subclassing the grapic control to get mouse msgs
    global GrOldProc as long                  ' used in subclassing the grapic control to get mouse msgs
    global gMousePt as pointapi                     ' Mouse x and y
    global gLbDOWN as long,gRBDOWN as long    ' Left and right mouse button
    
    '--------------------------------------------------------------------------------
    function GrProc(byval hWnd as dword, byval wMsg as dword, byval wParam as dword, byval lParam as long) as long
        static startpt, dragpt as pointAPI
        
    
      select case wMsg
         '
         case %wm_mousemove
             gMousePt.X = lo(word,lParam)
             gMousePt.Y = hi(word,lParam)  ' Current Mouse X and Y Position in the graphic window
             if glbdown then
                ?"StartDrag"
                startpt = gmousept
                while dragdetect (hWnd, startpt)
                    sleep 2
                    getcursorpos ( dragpt)       ' screen coordinates
                    screentoclient(hWnd, dragpt) ' window coordinates
                    ? "DrawDrag", dragpt.x, dragpt.y
                wend
                ?"EndDrag"
                 gmousept = dragpt
             end if
             exit function
        '
        case %wm_lbuttondown
            gMousePt.X = lo(word,lParam)
            gMousePt.Y = hi(word,lParam)          ' Current Mouse X and Y Position in the graphic window
            gLBDOWN = 1
            exit function                      ' Left button pressed
    
        case %wm_lbuttonup
            glbdown = 0
            exit function
        '
      end select
     function = CallWindowProc(GrOldProc, hWnd, wMsg, wParam, lParam)
    
    end function
        '----------------------------------------------------------------------
    '-----------------------------------------------------------------------------------
    function pbmain () as long
        local skey as string
        local currentrect as rect
        local hGW as dword
        local clicked, X, Y as long
        local lastpt as point
        local hWin as dword
    
        ' Create and show a Graphic window on screen
    
    
        graphic window "", 450, 130, 500, 500 to hWin
    
        graphic attach hWin, 0, redraw
        hGW = GetWindow(hWin, %GW_CHILD) ' Retrieve hWnd of graphic window
    
        ' Subclass the Graphic control
        GrOldProc = SetWindowLong(hGW, %GWL_WNDPROC, codeptr(GrProc))
        graphic set pos (0,0)
    
        setrect currentrect, 0, 70, 250, 250
        lastpt = gMousePt
        graphic box (currentrect.nleft, currentrect.ntop)- (currentrect.nright, currentrect.nbottom),0,0,-2,0
        graphic redraw
        do
            graphic clear -1, 0
            graphic print "resize and drag a box on a GRAPHIC WINDOW"
            graphic print "start with cursor in the graphic window(this one)"
            graphic print "Use the mouse to size the box. Left button down to drag it"
            graphic print "system drag rect is" + str$(getsystemmetrics(%SM_CXDRAG)) + " x" + _
                          str$(getsystemmetrics(%SM_CXDRAG))
            graphic print "<Esc> to exit"
    
            graphic box (currentrect.nleft, currentrect.ntop)- (currentrect.nright, currentrect.nbottom),0,0,-2,0
            graphic redraw
            if gLBdown then
                offsetrect currentrect, gMousePt.X - lastpt.X , gMousePt.Y - lastpt.Y
            else
                currentrect.nright  = gMousePt.X
                currentrect.nbottom = gMousePt.Y
            end if
            skey = ""
            graphic inkey$ to skey
            if skey$ = $esc then exit loop
            lastpt = gMousePt
            sleep 50
        loop
    
        SetWindowLong(hGW, %GWL_WNDPROC, GrOldProc) ' remove subclassing
        graphic window end
    
    end function

  • #2
    Doh! Idiot child!

    It is so simple to drag with this technique. Rather than attempting (as I did at first) to control a loop containing the "dragdraw" actions, just call DragDetect like a "black box", as below. Also I had forgotten to convert the point to screen coordinates.


    Code:
    ' to investigate DragDetect API
    ' Chris Holbrook 10 Dec 2008
    
    #compile exe
    #dim all
    #include once "win32api.inc"
    
    %pic_margin = 3
    
    global GrDialogProc as long               ' used in subclassing the grapic control to get mouse msgs
    global GrOldProc as long                  ' used in subclassing the grapic control to get mouse msgs
    global gMousePt as pointapi                     ' Mouse x and y
    global gLbDOWN as long,gRBDOWN as long    ' Left and right mouse button
    
    '--------------------------------------------------------------------------------
    function GrProc(byval hWnd as dword, byval wMsg as dword, byval wParam as dword, byval lParam as long) as long
        static startpt, dragpt as pointAPI
        
    
      select case wMsg
         '
         case %wm_mousemove
             gMousePt.X = lo(word,lParam)
             gMousePt.Y = hi(word,lParam)  ' Current Mouse X and Y Position in the graphic window
             if glbdown then
                ?"StartDrag"
                startpt = gmousept
                clienttoscreen hwnd, startpt
    '            while dragdetect (hWnd, startpt)
    '                sleep 2
    '                getcursorpos ( dragpt)       ' screen coordinates
    '                screentoclient(hWnd, dragpt) ' mouse coordinates
    '                ? "DrawDrag", dragpt.x, dragpt.y
    '            wend
                dragdetect (hWnd, startpt)
                ?"EndDrag"
                 gmousept = dragpt
             end if
             exit function
        '
        case %wm_lbuttondown
            gMousePt.X = lo(word,lParam)
            gMousePt.Y = hi(word,lParam)          ' Current Mouse X and Y Position in the graphic window
            gLBDOWN = 1
            exit function                      ' Left button pressed
    
        case %wm_lbuttonup
            glbdown = 0
            exit function
        '
      end select
     function = CallWindowProc(GrOldProc, hWnd, wMsg, wParam, lParam)
    
    end function
        '----------------------------------------------------------------------
    '-----------------------------------------------------------------------------------
    function pbmain () as long
        local skey as string
        local currentrect as rect
        local hGW as dword
        local clicked, X, Y as long
        local lastpt as pointapi
        local hWin as dword
    
        ' Create and show a Graphic window on screen
    
    
        graphic window "", 450, 130, 500, 500 to hWin
    
        graphic attach hWin, 0, redraw
        hGW = GetWindow(hWin, %GW_CHILD) ' Retrieve hWnd of graphic window
    
        ' Subclass the Graphic control
        GrOldProc = SetWindowLong(hGW, %GWL_WNDPROC, codeptr(GrProc))
        graphic set pos (0,0)
    
        setrect currentrect, 0, 70, 250, 250
        lastpt = gMousePt
        graphic box (currentrect.nleft, currentrect.ntop)- (currentrect.nright, currentrect.nbottom),0,0,-2,0
        graphic redraw
        do
            graphic clear -1, 0
            graphic print "resize and drag a box on a GRAPHIC WINDOW"
            graphic print "start with cursor in the graphic window(this one)"
            graphic print "Use the mouse to size the box. Left button down to drag it"
            graphic print "system drag rect is" + str$(getsystemmetrics(%SM_CXDRAG)) + " x" + _
                          str$(getsystemmetrics(%SM_CXDRAG))
            graphic print "<Esc> to exit"
    
            graphic box (currentrect.nleft, currentrect.ntop)- (currentrect.nright, currentrect.nbottom),0,0,-2,0
            graphic redraw
            if gLBdown then
                offsetrect currentrect, gMousePt.X - lastpt.X , gMousePt.Y - lastpt.Y
            else
                currentrect.nright  = gMousePt.X
                currentrect.nbottom = gMousePt.Y
            end if
            skey = ""
            graphic inkey$ to skey
            if skey$ = $esc then exit loop
            lastpt = gMousePt
            sleep 50
        loop
    
        SetWindowLong(hGW, %GWL_WNDPROC, GrOldProc) ' remove subclassing
        graphic window end
    
    end function

    Comment


    • #3
      Well here I was studying this for the fun of it. Don't mind though, I learned a little more, thank ye.
      Rod
      In some future era, dark matter and dark energy will only be found in Astronomy's Dark Ages.

      Comment


      • #4
        Originally posted by Rodney Hicks View Post
        Well here I was studying this for the fun of it...
        Sorry for my impatience!

        I'm wondering why there are no significant references to DragDetect in the forums. Maybe there is an even simpler way of doing it which I have missed.

        Comment

        Working...
        X