Jump to content

Dropping files on a ListBox?


ycomp
 Share

Recommended Posts

Yes, but i pefer ListView: the standard source snippets i use tgerefor;

#Region ;Drag and Drop und Doppelklick
Func WM_DROPFILES_FUNC($hWnd, $msgID, $wParam, $lParam)
    Local $nSize, $pFileName
    Local $nAmt = DllCall("shell32.dll", "int", "DragQueryFile", "hwnd", $wParam, "int", 0xFFFFFFFF, "ptr", 0, "int", 255)
    For $i = 0 To $nAmt[0] - 1
        $nSize = DllCall("shell32.dll", "int", "DragQueryFile", "hwnd", $wParam, "int", $i, "ptr", 0, "int", 0)
        $nSize = $nSize[0] + 1
        $pFileName = DllStructCreate("char[" & $nSize & "]")
        DllCall("shell32.dll", "int", "DragQueryFile", "hwnd", $wParam, "int", $i, "ptr", DllStructGetPtr($pFileName), "int", $nSize)
        ReDim $aDropFiles[$i + 1]
        $aDropFiles[$i] = DllStructGetData($pFileName, 1)
        $pFileName = 0
    Next
EndFunc   ;==>WM_DROPFILES_FUNC

Func _FilesDropped()
    Local $szDrive, $szDir, $szFName, $szExt
    _GUICtrlListView_BeginUpdate($idLV_Div)
    _GUICtrlListView_BeginUpdate($idLV_Music)
    _GUICtrlListView_BeginUpdate($idLV_Prog)
    For $i = 0 To UBound($aDropFiles) - 1
        $split = _PathSplit($aDropFiles[$i], $szDrive, $szDir, $szFName, $szExt)
        $szExt = StringUpper($szExt)
        ConsoleWrite($aDropFiles[$i] & ", " & $szExt & @CRLF)
        Switch $szExt
            Case ".EXE", ".BAT", ".CMD"
                GUICtrlCreateListViewItem($szFName & "||" & $aDropFiles[$i], $idLV_Prog)
            Case ".MP3", ".MPEG", ".MPG", ".WMV"
                GUICtrlCreateListViewItem($szFName & "||" & $aDropFiles[$i], $idLV_Music)
            Case Else
                GUICtrlCreateListViewItem($szFName & "||" & $aDropFiles[$i], $idLV_Div)
        EndSwitch
    Next
    _GUICtrlListView_SetColumnWidth($idLV_Div, 0, $LVSCW_AUTOSIZE)
    _GUICtrlListView_SetColumnWidth($idLV_Music, 0, $LVSCW_AUTOSIZE)
    _GUICtrlListView_SetColumnWidth($idLV_Prog, 0, $LVSCW_AUTOSIZE)
    _GUICtrlListView_EndUpdate($idLV_Div)
    _GUICtrlListView_EndUpdate($idLV_Music)
    _GUICtrlListView_EndUpdate($idLV_Prog)
EndFunc   ;==>_FilesDropped

Func WM_NOTIFY($hWnd, $msgID, $wParam, $lParam)
    Local $tagNMHDR, $event, $hwndFrom, $code, $hListview = -999
    $tagNMHDR = DllStructCreate("int;int;int", $lParam)
    If @error Then Return 0
    $code = DllStructGetData($tagNMHDR, 3)
    If $wParam = $idLV_Prog Then $hListview = $idLV_Prog
    If $wParam = $idLV_Div Then $hListview = $idLV_Div
    If $wParam = $idLV_Music Then $hListview = $idLV_Music
    If $wParam = $hListview And $code = -3 And _GUICtrlListView_GetSelectedCount($hListview) > 0 Then
        ConsoleWrite("Prog: " & $idLV_Prog & ", Div: " & $idLV_Div & ", Music: " & $idLV_Music & ", Zugriff über: " & $hListview & @CRLF)
        #cs

            For $i = 0 To _GUICtrlListView_GetItemCount($hListview)
            If _GUICtrlListView_GetItemSelected($hListview, $i) Then
            $aItem = _GUICtrlListView_GetItemTextArray($hListview, $i)
            ConsoleWrite($aItem[1] & ", " & $aItem[3] & @crlf)
            ShellExecute($aItem[3])
            EndIf
            Next
            Else
            ;MP3 abspielen z.B.: Soundplay oder mit der Bass.dll solange mit externem registriertem Player
            ;wenn fertig Zeile 364 löschen
        #ce     ;vorerst keine Mehrfchselektion
        $aItem = _GUICtrlListView_GetItemTextArray($hListview)
        ConsoleWrite($aItem[1] & ", " & $aItem[3] & @CRLF)
        ShellExecute($aItem[3])
    EndIf
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY
#EndRegion

 

Link to comment
Share on other sites

When you select multiple items, the item you actually click on is the first one in the array, which is why I made it show the array. Also, note that the list box is made to not sort the items. Hopefully, this will help:

#include <Array.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <WinAPI.au3>

Global $__aGUIDropFiles = 0

$Form1 = GUICreate("Form1", 600, 229, -1, -1, -1, $WS_EX_ACCEPTFILES)
$List1 = GUICtrlCreateList("", 16, 24, 545, 175, $WS_BORDER) ; Unsorted list!!!!
GUICtrlSetState(-1, $GUI_DROPACCEPTED)
GUISetState(@SW_SHOW)

GUIRegisterMsg($WM_DROPFILES, 'WM_DROPFILES')

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $GUI_EVENT_DROPPED
            _ArrayDisplay($__aGUIDropFiles)
            $sList = _ArrayToString($__aGUIDropFiles, "|", 1) ; The 1 is to start at array[1] (second item), since the array's first item array[0] is the item count
            GUICtrlSetData($List1, "") ; Clears the list
            GUICtrlSetData($List1, $sList) ; Sets new data
    EndSwitch
WEnd

Func WM_DROPFILES($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $lParam
    Switch $iMsg
        Case $WM_DROPFILES
            Local Const $aReturn = _WinAPI_DragQueryFileEx($wParam)
            If UBound($aReturn) Then
                $__aGUIDropFiles = $aReturn
            Else
                Local Const $aError[1] = [0]
                $__aGUIDropFiles = $aError
            EndIf
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_DROPFILES

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...