Announcement

Collapse
No announcement yet.

Anyone got a "Drag and Drop" example?

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

  • Anyone got a "Drag and Drop" example?

    What are the basic concepts of dragging and dropping a file to a window or control?
    I assume it causes some message to fire? With the file name and path?

  • #2
    Hey Dave!

    Code:
    DragDrop Multiple Files
    
    'Compilable Example:
    #Compiler PBWin 9, PBWin 10
    #Compile Exe
    #Dim All
    %Unicode=1
    #Include "Win32API.inc"
    Global hDlg As Dword
    
    Function PBMain() As Long
       Dialog New Pixels, 0, "Drop Test",600,1400,200,200, %WS_OverlappedWindow To hDlg
       DragAcceptFiles hDlg, %True
       Control Add Label, hDlg, 200,"Drag/drop file anywhere within app!", 20,40,170,20
       Dialog Show Modal hDlg Call DlgProc
    End Function
    
    CallBack Function DlgProc() As Long
       Local i,FileCount As Long, FileName As WStringZ * %Max_Path, temp$
       Select Case Cb.Msg
          Case %WM_DROPFILES
             FileCount = DragQueryFile(Cb.WParam, -1, ByVal 0, 0)
             For i = 0 To FileCount-1
                DragQueryFile Cb.WParam, i, FileName, SizeOf(FileName)
                temp$ = temp$ + $CrLf + FileName
             Next i
             DragFinish Cb.WParam
             MsgBox "Dropped filenames: " + $CrLf + $CrLf + Trim$(temp$,$CrLf)
       End Select
    End Function

    Comment


    • #3
      Thanks Gary!
      I figured it was something like that!

      Comment


      • #4
        This is a real DOH! moment... I was able compile Gary's code, and although I could see the icon in the button bar, I wasn't seeing the panel. And from Process Explorer, I could tell it was a running process and not hung... I thought this might be related to a glitch in a recent virus-scan, that maybe something essential had been affected. But other programs compiled and ran with no problem...

        So after a good night's sleep, I noticed the x, y position in the DIALOG NEW statement, and realized the main panel was off my screen. Adjusted to 10,10 and of course, it was there and working properly...

        DOH!, right?
        -John

        Comment


        • #5
          Thanks John and Gary. The 10,10 did the job nicely

          Comment


          • #6
            Just looked at his code. "600,1400" ?
            Using a screen in portrait mode perhaps? That will be off the screen for a lot of people who cut paste the code to try it out

            Comment


            • #7
              Hey Stuart!

              Yes, that code snippet was BEFORE Borje chastised me for not using code that centers the dialog on the screen! It did indeed fall off his screen.

              In more recent snippets, I'm trying to do better. My 2560x1440 resolution spoils me.

              Comment


              • #8
                ...Reminds me that I've wanted to incorporate "check user display" code in my programs that determines best x, y, xx, yy values before doing DIALOG NEW...
                Last year I experimented with some code to respect the user's choice of taskbar height, so I just need to get in the habit of calling it first.

                -John

                Comment


                • #9
                  John, for all my released apps I use an INI file. When loading that file in WM_InitDialog, I set the dialog xy position centered on the screen. See my recent post on gbProcedures for the code example.

                  Comment


                  • #10
                    Yup, I understand. I also use INI files to hold settings. However, in order to obtain those settings, I like when the program figures out the parameters of the screen/environment, and adjusts itself before displaying its main panel and writing the INI values.

                    -John

                    Comment


                    • #11
                      John, the Settings function in gbProcedures does what you ask. Or, did I mis-understand your comment?

                      Here's the pertinent code ...

                      Code:
                      Sub Settings_INI(Task$)
                         Local xResult, yResult, tempz, INIFileName As WStringZ * %Max_Path
                         Local WinPla As WindowPlacement, x As Long
                         Local DefaultX, DefaultY, wDeskTop, hDeskTop As Long
                      
                         'set ini filename
                         INIFileName = Exe.Path$ + "gbprocedures.ini"
                         If Task$ = "get" Then
                      
                            'get dialog width/height from INI file and use to set Dialog size
                            GetPrivateProfileString "All", "Width", "1200", xResult, %Max_Path, INIFileName
                            GetPrivateProfileString "All", "Height", "600", yResult, %Max_Path, INIFileName
                            Dialog Set Size hDlg,Val(xResult), Val(yResult)   'width/height
                      
                            'default should be centered on screen
                            Desktop Get Client To wDeskTop, hDeskTop
                            DefaultX = (wDeskTop-Val(xResult))/2
                            DefaultY = (hDeskTop-Val(yResult))/2
                      
                            'get dialog top/left from INI file and use to set Dialog location
                            Getprivateprofilestring "All", "Left", Str$(DefaultX), xResult, %Max_Path, INIFileName
                            Getprivateprofilestring "All", "Top", Str$(DefaultY), yResult,  %Max_Path, INIFileName
                            Dialog Set Loc hDlg, Val(xResult), Val(yResult)   'left/top

                      Comment


                      • #12
                        Well, it looks OK, but in a file where I was testing last year, I wrote a note that Desktop Get Client wasn't working properly. Might have had something to do with my using a taskbar that's 2 rows high? (yes, uncommon, but I like a 3-line clock display...) (Or maybe

                        I can't test your code or mine till tomorrow night, so I'm going to have to ponder awhile..

                        But a few questions come to mind - what if you don't want the first panel to be centered, but placed a certain distance from left and top edges? And what if the user has an unusual display (like 2 monitors?, or what if user has moved the taskbar to the left side?

                        Your code is great for most "normal" circumstances, but "what if" something is unusual, or your initial settings are fine for a double screen, but not for a single?

                        ... more in the morning, I'm falling asleep on the keyboard! I've got to crash now...

                        -John

                        Comment

                        Working...
                        X