Sign in to follow this  
Followers 0
JohnBailey

DragDrop File into ListView

4 posts in this topic

Is there a way of dragging and dropping a file into a listview? I setup a gui with $WS_EX_ACCEPTFILES and $GUI_DROPACCEPTED

#include <GUIConstants.au3>
#Include <GuiListView.au3>

$Form1 = GUICreate("AForm1", 268, 301, 193, 115,-1,$WS_EX_ACCEPTFILES)
$ListView1 = GUICtrlCreateListView("File Name", 24, 24, 193, 225)
;GuiCtrlSetState($ListView1 ,$GUI_DROPACCEPTED)
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

A decision is a powerful thing

Share this post


Link to post
Share on other sites



This is what I did. Is this the best method?

#include <GUIConstants.au3>
#Include <GuiListView.au3>
#Include <GuiList.au3>
#include <File.au3>
#include <Array.au3>

Opt("GUIOnEventMode", 1)

Dim $szDrive, $szDir, $szFName, $szExt

$Form1 = GUICreate("AForm1", 268, 325, 193, 115,-1,$WS_EX_ACCEPTFILES)
;$ListView1 = GUICtrlCreateListView("File Name", 24, 24, 193, 225)
$input = GUICtrlCreateInput("",24,10,193,180)
$ListView1 = GUICtrlCreateList("", 24, 10, 193, 180)
;$input = GUICtrlCreateInput("",30,200,175)
GuiCtrlSetState($input,$GUI_DROPACCEPTED)
GUISetOnEvent($GUI_EVENT_DROPPED, "addFile")
GUISetOnEvent($GUI_EVENT_CLOSE, "close")
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

Func addFile()
    GUICtrlSetState($input, $GUI_HIDE)
    $files = StringSplit(GUICtrlRead($input), "|")
    _ArrayDelete($files,0)
    $max = _ArrayMaxIndex($files, 1, 1)
    For $i = 0 to $max
        $TestPath = _PathSplit($files[$i], $szDrive, $szDir, $szFName, $szExt)
        _GUICtrlListAddItem ($ListView1, $szFName&$szExt )
    Next
EndFunc

Func close()
    Exit
EndFunc

A decision is a powerful thing

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Here's what I came up with (99% credit to lazycat)...

; original by lazycat
; modded by me

#include <GUIConstants.au3>
#include <GUIListView.au3>

Global $WM_DROPFILES = 0x233
Global $gaDropFiles[1]

GUICreate('', 320, 220, -1, -1, -1, $WS_EX_ACCEPTFILES)

$list = GUICtrlCreateListView('Files', 10, 10, 300, 200)
GUICtrlSendMsg($list, $LVM_SETEXTENDEDLISTVIEWSTYLE, $LVS_EX_GRIDLINES, -1)
_GUICtrlListViewSetColumnWidth($list, 0, 296)
GUICtrlSetState($list, $GUI_DROPACCEPTED)

GUIRegisterMsg($WM_DROPFILES, 'WM_DROPFILES_FUNC')

GUISetState()

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $GUI_EVENT_DROPPED
            For $i = 0 To UBound($gaDropFiles) - 1
                GUICtrlCreateListViewItem($gaDropFiles[$i], $list)
            Next
    EndSwitch
WEnd

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 $gaDropFiles[$i + 1]
        $gaDropFiles[$i] = DllStructGetData($pFileName, 1)
        $pFileName = 0
    Next
EndFunc

Func quit()
    Exit
EndFunc

edit - change to the loop adding items to the listview

Edited by xcal

Share this post


Link to post
Share on other sites

xcal, that does EXACTLY what I want it to! Thank you so much! I'm about to pass out, so I'll take a look at the script in detail tomorrow morning. Thank you!! That makes a lot more sense than what I was doing already. Props to lazycat


A decision is a powerful thing

Share this post


Link to post
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
Sign in to follow this  
Followers 0