Announcement

Collapse
No announcement yet.

Drag & Drop

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

  • Drag & Drop

    I can not figure it out how to implement a simple drag & drop in PBDLL 6.0.
    What I want is to drag one image control to another place in the same window with the normal Windows left mouse button down. I would appreciate any sample code.

    Sincerely,


    Peter Redei

    ------------------

  • #2
    hi peter,



    regards, jules

    Comment


    • #3
      Peter;

      First, subclass your controls.

      Then process the %WM_NCHITTEST message and call the DefWindowProc to
      get the proper return value.

      Now if the DefWindowProc returns the value :

      %HTCLIENT

      Then replace the value with :

      %HTCAPTION

      This will make Windows think you clicked on a Caption bar (which
      a control does not have) rather than the client area of the control.
      This will impliment the standard windows drag function and you can
      move the control around just like any Dialog can be moved around
      by dragging on the Caption bar.


      ------------------
      Chris Boss
      Computer Workshop
      Developer of "EZGUI"
      http://cwsof.com
      http://twitter.com/EZGUIProGuy

      Comment


      • #4
        Peter,

        Sorry, I misunderstood.

        Try this instead...

        This will snap to a 8x8 grid, you can remove that feature.
        Unlike Chris suggestion, this will move the window dynamically. A nicer
        effect rather than the windows standard Focus Rectangle.

        Code:
        '----------------------------------------------------
        'Snap-to-grid demo. Another C to PB conversion
        'Final code fixes by Jim Huguley.
        'October 19th, 1999
        '----------------------------------------------------
         
        
        $COMPILE EXE
        $INCLUDE "WIN32API.INC"
          
        '---
        GLOBAL ghInst       AS LONG
        GLOBAL ghWnd        AS LONG
        GLOBAL ghWndChild   AS LONG
        GLOBAL szNullString AS ASCIIZ*20
        GLOBAL szAppName    AS ASCIIZ*20
        GLOBAL szChildName  AS ASCIIZ*20
        GLOBAL szMoveMe     AS ASCIIZ*20
         
        
        '----------------------------------------------------------
        FUNCTION WINMAIN(BYVAL hInstance AS LONG, _
                         BYVAL hPrevInstance AS LONG, _
                         lpszCmdLine AS ASCIIZ PTR, _
                         BYVAL nCmdShow AS LONG) AS LONG
         
        
        LOCAL Msg        AS tagMsg
        LOCAL wc         AS WNDCLASSEX
        LOCAL hFormBrush AS LONG
        LOCAL hFormBM    AS LONG
        
          
        '---
        IF ISFALSE(hPrevInstance) THEN
         
        '**Register Parent Window Class
        szAppName        = "BASE1"
        wc.cbSize        = SIZEOF(wc)
        wc.style         = %CS_BYTEALIGNCLIENT
        wc.lpfnWndProc   = CODEPTR(WndProc)
        wc.cbClsExtra    = 0
        wc.cbWndExtra    = 0
        wc.hInstance     = hInstance
        wc.hIcon         = %NULL
        wc.hCursor       = LoadCursor(%NULL, BYVAL %IDC_ARROW)
        wc.hbrBackground = GetStockObject(%WHITE_BRUSH)
        wc.lpszMenuName  = %NULL
        wc.lpszClassName = VARPTR(szAppName)
        RegisterClassEx wc
        '---
         
        '**Register the Child Window Class
        szChildName         = "Ben"
        wc.cbSize           = SIZEOF(wc)
        wc.style            = %CS_HREDRAW OR %CS_VREDRAW OR %CS_BYTEALIGNCLIENT
        wc.lpfnWndProc      = CODEPTR(FormWndProc)
        wc.cbClsExtra       = 0
        wc.cbWndExtra       = 0
        wc.hInstance        = hInstance
        wc.hIcon            = %NULL
        wc.hCursor          = LoadCursor(hInstance, "HAND")
        wc.hbrBackground    = GetStockObject(%LTGRAY_BRUSH)
        wc.lpszMenuName     = %NULL
        wc.lpszClassName    = VARPTR(szChildName)
        RegisterClassEx wc
        
        ELSE
            EXIT FUNCTION
        END IF
        '---
        
         
          ghInst = hInstance
         
          ghWnd  = CreateWindow (szAppName, "PASSMSG Sample", _
                                 %WS_OVERLAPPEDWINDOW OR %WS_BORDER, _
                                 GetSystemMetrics ( %SM_CXSCREEN ) \4, _
                                 GetSystemMetrics ( %SM_CYSCREEN ) \4, _
                                 GetSystemMetrics ( %SM_CXSCREEN ) \2, _
                                 GetSystemMetrics ( %SM_CYSCREEN ) \2, _
                                 %NULL, %NULL, hInstance, %NULL)
         
          szNullString = ""
          ghWndChild =   CreateWindow (szChildName, szNullString, _
                                       %WS_CHILD OR %WS_VISIBLE, _
                                       10, 10, _
                                       100, 100, _
                                       ghWnd, BYVAL %NULL, hInstance, %NULL)
        
         
          ShowWindow  ghWnd, %SW_SHOW
          UpdateWindow ghWnd
         
        '---
        WHILE GetMessage(Msg, %NULL, 0, 0)
            TranslateMessage Msg
            DispatchMessage Msg
        WEND
         
          DeleteObject  hFormBrush
         
          FUNCTION = Msg.wParam
        END FUNCTION
        
         
        '-------------------------------------------------------
        FUNCTION WndProc(BYVAL hWnd AS LONG, _
                         BYVAL Msg AS LONG,BYVAL wParam AS LONG, _
                         BYVAL lParam AS LONG) AS LONG
         
          STATIC bDragging AS LONG     'flag
          STATIC ptLastPos AS POINTAPI
         
        
        SELECT CASE Msg
        
         
            CASE %WM_CREATE
         
            CASE %WM_LBUTTONDOWN
         
              ptLastPos.x = LOWRD ( lParam)
              ptLastPos.y = HIWRD ( lParam)
         
              ptLastPos.x = LOWRD ( lParam ) - LOWRD ( lParam ) MOD 8
              ptLastPos.y = HIWRD ( lParam ) - HIWRD ( lParam ) MOD 8
        
               
              IF ChildWindowFromPoint(hWnd, ptLastPos.x, ptLastPos.y ) <> hWnd THEN
         
                '**Must be the child!
                SetCapture hWnd
                bDragging = %TRUE
         
              END IF
         
            CASE %WM_LBUTTONUP
         
              IF bDragging = %TRUE THEN
         
                InvalidateRect  ghWndChild, BYVAL %NULL, %TRUE
                ReleaseCapture
                bDragging = %FALSE
         
              END IF
         
            CASE %WM_MOUSEMOVE
         
              IF bDragging = %TRUE THEN
         
                DIM ptNew AS POINTAPI
         
                ptNew.x = LOWRD ( lParam ) - LOWRD ( lParam ) MOD 8
                ptNew.y = HIWRD ( lParam ) - HIWRD ( lParam ) MOD 8
        
         
                IF (( ptNew.x <> ptLastPos.x ) OR ( ptNew.y <> ptLastPos.y )) THEN
         
                    DIM ptTopLeft AS POINTAPI
         
                    '**Figure out current position of Child window
         
                    ptTopLeft.x = 0 'Modify these values if you add a thick border or other
                    ptTopLeft.y = 0 'adornments to reflect the client area offset
         
                    ClientToScreen  ghWndChild, ptTopLeft
                    ScreenToClient  hWnd,       ptTopLeft
         
                    '**Move child Window to new spot
         
                    SetWindowPos ghWndChild, _
                                 %NULL, _
                                 ptTopLeft.x + (ptNew.x-ptLastPos.x), _
                                 ptTopLeft.y + (ptNew.y-ptLastPos.y), _
                                 0, _
                                 0, _
                                 %SWP_NOSIZE OR %SWP_NOZORDER
         
                    InvalidateRect ghWndChild, BYVAL %NULL, %TRUE
                    UpdateWindow ghWndChild
         
                    ptLastPos = ptNew
         
                END IF
         
              END IF
        
        
         
            CASE %WM_DESTROY
         
              PostQuitMessage 0
         
            CASE ELSE
         
              FUNCTION = DefWindowProc ( hWnd, msg, wParam, lParam )
              EXIT FUNCTION
         
        END SELECT
         
        FUNCTION = 0
         
        END FUNCTION
        
         
        
        '-----------------------------------------------------
        FUNCTION FormWndProc( BYVAL hWnd AS LONG, _
                              BYVAL Msg AS LONG, _
                              BYVAL wParam AS LONG, _
                              BYVAL lParam AS LONG) AS LONG
        
         
          LOCAL pt  AS POINTAPI
          LOCAL hDC AS LONG
          LOCAL ps  AS PAINTSTRUCT
          LOCAL rc  AS RECT
         
        SELECT CASE Msg
         
            CASE %WM_ERASEBKGND  ' Eat this message to avoid flicker
         
            CASE %WM_CREATE
         
            'CASE %WM_LBUTTONDOWN
            'CASE %WM_LBUTTONUP
            'CASE %WM_MOUSEMOVE
         
             
            CASE %WM_LBUTTONDOWN , %WM_LBUTTONUP, %WM_MOUSEMOVE
         
              '**Pass all mouse messages on through to parent,
              '  in parent coordinates
         
              pt.x = LOWRD ( lParam )
              pt.y = HIWRD ( lParam )
         
              ClientToScreen hWnd,            pt
              ScreenToClient GetParent(hWnd), pt
         
              PostMessage  ghWnd, Msg, wParam, MAKLNG (pt.x, pt.y )
         
            CASE %WM_PAINT
         
              '**Just draw a simple little something nice centered
              '  in the window
         
              hDC = BeginPaint ( hWnd, ps )
              GetClientRect  hWnd, rc
         
              SetBkColor    hDC, RGB (   0,   0, 128 )
              SetTextColor  hDC, RGB ( 255, 255,   0 )
         
              DIM sz AS SIZEL
              szMoveMe = "Move Me!"
         
              GetTextExtentPoint32  hDC, szMoveMe, LEN(szMoveMe), sz
         
              pt.x = (rc.nright  - sz.cx)\2
              pt.y = (rc.nbottom - sz.cy)\2
         
              ExtTextOut  hDC, pt.x, pt.y, %ETO_OPAQUE, rc, szMoveMe, LEN(szMoveMe), BYVAL %NULL
         
              EndPaint  hWnd, ps
        
         
            CASE ELSE
         
              FUNCTION = DefWindowProc ( hWnd, msg, wParam, lParam )
              EXIT FUNCTION
         
        END SELECT
         
        FUNCTION = 0
         
        END FUNCTION
        Regards, Jules

        Comment


        • #5
          peter,

          i think this is more what you are looking for...

          http://www.powerbasic.com/support/pb...ad.php?t=22854

          regards,
          jules
          mailto:[email protected][email protected]</a>

          Comment


          • #6
            Thank you Chris and Jules both very much. Now, I just need to get it work with all squares of a chess board.

            Sincerely,


            Peter Redei

            ------------------

            Comment

            Working...
            X