Jump to content

Drag And Drop Control Help


nitro322
 Share

Recommended Posts

I have a simple GUI with two input fields, one of which allows drag-and-drop. If I drop a file on the input field itself, it populates the filename. If I drop the file anywhere else in the GUI, it does not. Since that one field is the only thing that can accept dropped files, I'd like to code my GUI so that any files dropped anywhere on it will automatically populate that field to make it a little easier for them.

How can this be done in AutoIt? I've been reading through the docs and have searched the forum, but I haven't found anything that addresses this problem.

Thanks.

Link to comment
Share on other sites

#include <GUIConstants.au3>

Opt("GUICoordMode", 2)
Opt("GUIResizeMode", 1)
Opt("GUIOnEventMode", 1)

$parent1 = GUICreate("Parent1", 500, 500, -1, -1, -1, $WS_EX_ACCEPTFILES)
GUISetOnEvent($GUI_EVENT_CLOSE, "SpecialEvents")
GUISetOnEvent($GUI_EVENT_MINIMIZE, "SpecialEvents")
GUISetOnEvent($GUI_EVENT_RESTORE, "SpecialEvents")
GUISetOnEvent($GUI_EVENT_DROPPED, "SpecialEvents")

$ok1 = GUICtrlCreateButton("OK", 10, 30, 50)
GUICtrlSetOnEvent(-1, "OKPressed")
GUICtrlSetState(-1, $GUI_DROPACCEPTED)

$cancel1 = GUICtrlCreateButton("Cancel", 0, -1)
GUICtrlSetOnEvent(-1, "CancelPressed")
GUICtrlSetState(-1, $GUI_DROPACCEPTED)

GUICtrlCreateLabel("", 0, 0, 500, 500, -1, $WS_EX_TRANSPARENT)
GUICtrlSetState(-1, $GUI_DROPACCEPTED)

GUISetState(@SW_SHOW)


; Just idle around
While 1
    Sleep(10)
WEnd


; END


Func OKPressed()
    MsgBox(0, "OK Pressed", "ID=" & @GUI_CtrlId & " WinHandle=" & @GUI_WinHandle & " CtrlHandle=" & @GUI_CtrlHandle)
EndFunc  ;==>OKPressed


Func CancelPressed()
    MsgBox(0, "Cancel Pressed", "ID=" & @GUI_CtrlId & " WinHandle=" & @GUI_WinHandle & " CtrlHandle=" & @GUI_CtrlHandle)
EndFunc  ;==>CancelPressed


Func SpecialEvents()
    
    
    Select
        Case @GUI_CtrlId = $GUI_EVENT_CLOSE
            MsgBox(0, "Close Pressed", "ID=" & @GUI_CtrlId & " WinHandle=" & @GUI_WinHandle)
            Exit
            
        Case @GUI_CtrlId = $GUI_EVENT_MINIMIZE
            MsgBox(0, "Window Minimized", "ID=" & @GUI_CtrlId & " WinHandle=" & @GUI_WinHandle)
            
        Case @GUI_CtrlId = $GUI_EVENT_RESTORE
            MsgBox(0, "Window Restored", "ID=" & @GUI_CtrlId & " WinHandle=" & @GUI_WinHandle)
            
        Case @GUI_CtrlId = $GUI_EVENT_DROPPED
            MsgBox(0, "test", "Dropped File=" & @GUI_DragFile)
    EndSelect
    
EndFunc  ;==>SpecialEvents

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Link to comment
Share on other sites

Ok, the $GUI_EVENT_DROPPED looks like it'll provide what I need, but I'm having trouble making the entire canvas droppable. I see you did it in your code with this:

GUICtrlCreateLabel("", 0, 0, 500, 500, -1, $WS_EX_TRANSPARENT)
GUICtrlSetState(-1, $GUI_DROPACCEPTED)

However, when I do something similar in mine, the transparent label actually appears on top of everything, so that the entire window is gray. If I tab around, however, each control that becomes active will show through the transparent label. I can't figure out what I'm doing wrong here. Any suggestions?

#include <GUIConstants.au3>
$title = 'test'
opt('guioneventmode', 1)

; Create GUI
GUICreate($title, 300, 115, -1, -1, -1, $WS_EX_ACCEPTFILES)
GUICtrlCreateLabel("Archive/Installer to extract:", 5, 5, -1, 15)
$filecont = GUICtrlCreateInput("", 5, 20, 260, 20)
$filebut = GUICtrlCreateButton("...", 270, 20, 25, 20)
GUICtrlCreateLabel("Target directory:", 5, 45, -1, 15)
$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)
$dropzone = GUICtrlCreateLabel("", 0, 0, 300, 115, -1, $WS_EX_TRANSPARENT)

; Set properties
GUICtrlSetState($dropzone, $GUI_DROPACCEPTED)
GUICtrlSetState($filecont, $GUI_FOCUS)
GUICtrlSetState($ok, $GUI_DEFBUTTON)

; Set events
GUISetOnEvent($GUI_EVENT_DROPPED, "GUI_Drop")
GUICtrlSetOnEvent($filebut, "GUI_File")
GUICtrlSetOnEvent($dirbut, "GUI_Directory")
GUICtrlSetOnEvent($ok, "GUI_Ok")
GUICtrlSetOnEvent($cancel, "GUI_Exit")
GUISetOnEvent($GUI_EVENT_CLOSE, "GUI_Exit")

; Display GUI and wait for action
GUISetState(@SW_SHOW)
$finishgui = false
while 1
    if $finishgui then exitloop
wend

; Prompt user for file
func GUI_File()
; SNIPPED
endfunc

; Prompt user for directory
func GUI_Directory()
; SNIPPED
endfunc

; Set file to extract and target directory, then exit
func GUI_Ok()
; SNIPPED
    GUIDelete()
    $finishgui = true
endfunc
    
; Exit if Cancel clicked or window closed
func GUI_Exit()
    exit
endfunc

; Process dropped files outside of file input box
func GUI_Drop()
    msgbox(0, 'Test', 'testfunc')
endfunc
Edited by nitro322
Link to comment
Share on other sites

try it with a pic instead of a label

#include <GUIConstants.au3>
$title = 'test'
Opt('guioneventmode', 1)

; Create GUI
GUICreate($title, 300, 115, -1, -1, -1, $WS_EX_ACCEPTFILES)
$dropzone = GUICtrlCreatePic("",0,0,300,115)
GUICtrlSetState($dropzone, $GUI_DROPACCEPTED)
GUICtrlCreateLabel("Archive/Installer to extract:", 5, 5, -1, 15)
$filecont = GUICtrlCreateInput("", 5, 20, 260, 20)
$filebut = GUICtrlCreateButton("...", 270, 20, 25, 20)
GUICtrlCreateLabel("Target directory:", 5, 45, -1, 15)
$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)
GUICtrlSetState($ok, $GUI_DEFBUTTON)

; Set events
GUISetOnEvent($GUI_EVENT_DROPPED, "GUI_Drop")
GUICtrlSetOnEvent($filebut, "GUI_File")
GUICtrlSetOnEvent($dirbut, "GUI_Directory")
GUICtrlSetOnEvent($ok, "GUI_Ok")
GUICtrlSetOnEvent($cancel, "GUI_Exit")
GUISetOnEvent($GUI_EVENT_CLOSE, "GUI_Exit")

; Display GUI and wait for action
GUISetState(@SW_SHOW)
$finishgui = False
While 1
    If $finishgui Then ExitLoop
WEnd

; Prompt user for file
Func GUI_File()
; SNIPPED
EndFunc  ;==>GUI_File

; Prompt user for directory
Func GUI_Directory()
; SNIPPED
EndFunc  ;==>GUI_Directory

; Set file to extract and target directory, then exit
Func GUI_Ok()
; SNIPPED
    GUIDelete()
    $finishgui = True
EndFunc  ;==>GUI_Ok

; Exit if Cancel clicked or window closed
Func GUI_Exit()
    Exit
EndFunc  ;==>GUI_Exit

; Process dropped files outside of file input box
Func GUI_Drop()
    MsgBox(0, 'Test', 'testfunc')
EndFunc  ;==>GUI_Drop

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Link to comment
Share on other sites

pic seems to be more appropriate. But if you want to use a label you could try $GUI_BKCOLOR_TRANSPARENT and $GUI_DISABLE

#include <GUIConstants.au3>

$title = 'test'

opt('guioneventmode', 1)

; Create GUI

GUICreate($title, 300, 115, -1, -1, -1, $WS_EX_ACCEPTFILES)

$dropzone = GUICtrlCreateLabel("", 0, 0, 300, 115)

GUICtrlSetBkColor ($dropzone , $GUI_BKCOLOR_TRANSPARENT)

GuiCtrlSetState($dropzone, $GUI_DISABLE)

GUICtrlCreateLabel("Archive/Installer to extract:", 5, 5, -1, 15)

$filecont = GUICtrlCreateInput("", 5, 20, 260, 20)

$filebut = GUICtrlCreateButton("...", 270, 20, 25, 20)

GUICtrlCreateLabel("Target directory:", 5, 45, -1, 15)

$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($dropzone, $GUI_DROPACCEPTED)

GUICtrlSetState($filecont, $GUI_FOCUS)

GUICtrlSetState($ok, $GUI_DEFBUTTON)

; Set events

GUISetOnEvent($GUI_EVENT_DROPPED, "GUI_Drop")

GUICtrlSetOnEvent($filebut, "GUI_File")

GUICtrlSetOnEvent($dirbut, "GUI_Directory")

GUICtrlSetOnEvent($ok, "GUI_Ok")

GUICtrlSetOnEvent($cancel, "GUI_Exit")

GUISetOnEvent($GUI_EVENT_CLOSE, "GUI_Exit")

; Display GUI and wait for action

GUISetState(@SW_SHOW)

$finishgui = false

while 1

if $finishgui then exitloop

wend

; Prompt user for file

func GUI_File()

; SNIPPED

endfunc

; Prompt user for directory

func GUI_Directory()

; SNIPPED

endfunc

; Set file to extract and target directory, then exit

func GUI_Ok()

; SNIPPED

GUIDelete()

$finishgui = true

endfunc

; Exit if Cancel clicked or window closed

func GUI_Exit()

exit

endfunc

; Process dropped files outside of file input box

func GUI_Drop()

msgbox(0, 'Test', 'testfunc')

endfunc

Link to comment
Share on other sites

Sweet! I got it working. gafrost, I had a separate problem with your second example. The appearance and drop functionality was fine, however the picture layer seemed to be "on top" of the buttons. Keyboard input worked fine, but mouse clicks had no effect whatsoever. I tried playing around with the order, but didn't have much luck.

aec, your example worked perfectly. Of course, as it builds off of gafrost's, I want to make sure both of you get proper credit. :-)

Thanks again.

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