Jump to content

How To Act On Drag And Drop Event?


Recommended Posts

I have a simple GUI with two input boxes. I want to enable drag and drop on one of the fields to populate it with the selected file name. Once the file is dropped, I also want to populate the second field with the directory of the dropped file. However, I cannot figure out how to act on a drag and drop event. How can I determine when a file has been dropped and then take some action on it?

I've searched the forum but didn't have any luck. Most answers seemed to be based on checking the file input field for some change. However, the user can also manually enter a filename in this field, or select a file through a fileopendialog() button, so that option doesn't work for me.

Any ideas? I've included a snippet of the relevant code below. You can also see how/where I'm trying to perform the action, though obviously the code doesn't work as-is. :-) Any help would be most appreciated. Thanks.

; Create GUI
GUICreate($title, 300, 115, -1, -1, -1, $WS_EX_ACCEPTFILES)
GUICtrlCreateLabel("Archive/Installer to extract:", 5, 5, -1, 20)
$filecont = GUICtrlCreateInput("", 5, 20, 260, 20)
$filebut = GUICtrlCreateButton("...", 270, 20, 25, 20)
GUICtrlCreateLabel("Target directory:", 5, 45, -1, 20)
$dircont = GUICtrlCreateInput("", 5, 60, 260, 20)
$dirbut = GUICtrlCreateButton("...", 270, 60, 25, 20)
$ok = GUICtrlCreateButton("&OK", 55, 90, 80, 20)
$cancel = GUICtrlCreateButton("&Cancel", 165, 90, 80, 20)

; Set properties
GUICtrlSetState($filecont, $GUI_FOCUS & $GUI_ACCEPTFILES)
GUICtrlSetState($ok, $GUI_DEFBUTTON)

; Display GUI and wait for action
GUISetState(@SW_SHOW)
while 1
    $action = GUIGetMsg()
    select
    ; DETECT DRAG AND DROP EVENT HERE <-------------------
        case $action = ???
            msgbox(0, 'Test', 'test')

    ; Set file to extract and target directory, then exit
        case $action = $ok
        <SNIP>
    endselect
wend
Edited by nitro322
Link to comment
Share on other sites

GuiCtrlRead() can see the event happen.

Se if this is what you need

#include <GUIConstants.au3>
$title = 'lovely'
; Create GUI
GUICreate($title, 300, 115, -1, -1, -1, $WS_EX_ACCEPTFILES)
GUICtrlCreateLabel("Archive/Installer to extract:", 5, 5, -1, 20)
$filecont = GUICtrlCreateInput("", 5, 20, 260, 20)
$filebut = GUICtrlCreateButton("...", 270, 20, 25, 20)
GUICtrlCreateLabel("Target directory:", 5, 45, -1, 20)
$dircont = GUICtrlCreateInput("", 5, 60, 260, 20)
$dirbut = GUICtrlCreateButton("...", 270, 60, 25, 20)
$ok = GUICtrlCreateButton("&OK", 55, 90, 80, 20)
$cancel = GUICtrlCreateButton("&Cancel", 165, 90, 80, 20)

; Set properties
GUICtrlSetState($filecont, $GUI_FOCUS & $GUI_ACCEPTFILES)
GUICtrlSetState($ok, $GUI_DEFBUTTON)

; Display GUI and wait for action
GUISetState(@SW_SHOW)
while 1
    $action = GUIGetMsg()
    select
   ; DETECT DRAG AND DROP EVENT HERE <-------------------
    Case GUICtrlRead($filecont) <> '' And Not GUICtrlRead($dircont)
        GUICtrlSetData($dircont, 'directory')
        
        case $action = 10
            msgbox(0, 'Test', 'test')

   ; Set file to extract and target directory, then exit
    case $action = $ok
;~       <SNIP>
    endselect
wend
Link to comment
Share on other sites

GuiCtrlRead() can see the event happen.

Thanks for the reply, MHz. Unfortunately, though, this doesn't work quite right. Yes. it'll perform the action after I drop a file on the control, but it'll also perform the action on any change to $filecont. If I were to type, for example, c:\test.zip, it would activate as soon as I entered c.

I need some way to distinguish between true drag-and-drop events and other input methods.

Edited by nitro322
Link to comment
Share on other sites

  • 2 weeks later...

Thanks, wiredbits. That looks like it should do exactly what I wanted; unfortunately, though, I'm trying to avoid using the beta version for this particular project. :-(

I managed to find a workaround, though. The whole point was to preset that field to a default directory. I'd still like to do that eventually (probably when the next version goes stable), but for now I'm simply setting the directory field to the default directory if blank after submission. Not quite as clean as I'd like, but close enough.

Thanks for the suggestions.

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...