Jump to content

How to Make AutoIt GUI Accept to Drag Files from Windows Explorer


Recommended Posts

Hello all
I have a question please
I've searched a lot on how to make a GUI for programs designed by Autoit to accepts dragging files from Windows File Explorer
Unfortunately, however, it failed
Is there any way to do this?
Where I am making a audio player and I want to activate the feature of dragging files from Windows Explorer to a program window to play them
I hope to find a simple solution here in this wonderful forum
Thank you very much in advance

Link to comment
Share on other sites

You can start by setting the state of the control you want to drag a file onto, like an input field or similar, with the state of $GUI_DROPACCEPTED and in your main loop, look for $GUI_EVENT_DROPPED. The file path will be held under @GUI_DragFile

The help file has info on this if you get lost.

Cheers!

Link to comment
Share on other sites

You mean something like this?

 

#include <GuiConstants.au3>
#include <GuiConstantsEx.au3>
#include <FileConstants.au3>
#include <EditConstants.au3>

GuiCreate('Drag and Drop', 700, 600, @DesktopWidth / 2 - 192, _
            @DesktopHeight / 2 - 235, -1, $WS_EX_ACCEPTFILES)

GUICtrlCreateGroup("Dict Files", 5, 5, 570, 90)
GUICtrlCreateLabel('Select file:', 20, 30)
$dropFile = GUICtrlCreateInput ("", 120, 30, 200, 20, -1, $WS_EX_STATICEDGE)
GUICtrlSetState (-1, $GUI_DROPACCEPTED)
GUICtrlSetTip (-1, 'You can drag & drop files here...')


GUICtrlCreateLabel('Select file:', 20, 60)
$dropFile = GUICtrlCreateInput ("", 120, 60, 200, 20, -1, $WS_EX_STATICEDGE)
GUICtrlSetState (-1, $GUI_DROPACCEPTED)
GUICtrlSetTip (-1, 'You can drag & drop files here...')


GuiSetState() ;Loop GUI
While 1
  $msg = GuiGetMsg()
    Select
    Case $msg = $GUI_EVENT_CLOSE
        Exit
  EndSelect
WEnd

 

Link to comment
Share on other sites

While what SplashFlo provided will work, you will find an Insert can occur rather than Replace for subsequent drops to the same inputs.

So try this instead.

#include <GuiConstants.au3>
#include <GuiConstantsEx.au3>
#include <FileConstants.au3>
#include <EditConstants.au3>

Global $dropFile_1, $dropFile_2

GuiCreate('Drag and Drop', 530, 120, @DesktopWidth / 2 - 192, _
            @DesktopHeight / 2 - 235, -1, $WS_EX_ACCEPTFILES)

GUICtrlCreateGroup("Dict Files", 5, 5, 510, 90)
GUICtrlCreateLabel('File 1:', 20, 30)
$dropFile_1 = GUICtrlCreateInput ("", 80, 30, 400, 20, -1, $WS_EX_STATICEDGE)
GUICtrlSetState (-1, $GUI_DROPACCEPTED)
GUICtrlSetTip (-1, 'You can drag & drop files here...')


GUICtrlCreateLabel('File 2:', 20, 60)
$dropFile_2 = GUICtrlCreateInput ("", 80, 60, 400, 20, -1, $WS_EX_STATICEDGE)
GUICtrlSetState (-1, $GUI_DROPACCEPTED)
GUICtrlSetTip (-1, 'You can drag & drop files here...')


GuiSetState() ;Loop GUI
While 1
  $msg = GuiGetMsg()
    Select
    Case $msg = $GUI_EVENT_CLOSE
        GUIDelete()
        ExitLoop
    Case $msg = $GUI_EVENT_DROPPED
        If @GUI_DropId = $dropFile_1 Then
            GUICtrlSetData($dropFile_1, @GUI_DragFile)
        ElseIf @GUI_DropId = $dropFile_2 Then
            GUICtrlSetData($dropFile_2, @GUI_DragFile)
        EndIf
  EndSelect
WEnd

Exit

 

Edited by TheSaint

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

Link to comment
Share on other sites

As far as I know, you cannot just drag & drop to a GUI itself. You need to specify a control to receive the drop.

Try this, which importantly has the $label control created last, is the full size of the GUI, and is set as Transparent.

#include <GuiConstants.au3>
#include <GuiConstantsEx.au3>
#include <FileConstants.au3>
#include <EditConstants.au3>

Global $dropFile_1, $dropFile_2, $label

GuiCreate('Drag and Drop', 530, 120, @DesktopWidth / 2 - 192, _
            @DesktopHeight / 2 - 235, -1, $WS_EX_ACCEPTFILES + $WS_EX_TOPMOST)

GUICtrlCreateGroup("Dict Files", 5, 5, 510, 90)
GUICtrlCreateLabel('File 1:', 20, 30)
$dropFile_1 = GUICtrlCreateInput ("", 80, 30, 400, 20, -1)
GUICtrlSetState($dropFile_1, $GUI_DROPACCEPTED)
GUICtrlSetTip($dropFile_1, 'You can drag & drop files here...')

GUICtrlCreateLabel('File 2:', 20, 60)
$dropFile_2 = GUICtrlCreateInput ("", 80, 60, 400, 20, -1)
GUICtrlSetState($dropFile_2, $GUI_DROPACCEPTED)
GUICtrlSetTip($dropFile_2, 'You can drag & drop files here...')

$label = GUICtrlCreateLabel('', 0, 0, 530, 120)
GUICtrlSetBkColor($label, $GUI_BKCOLOR_TRANSPARENT)
GUICtrlSetState($label, $GUI_DROPACCEPTED)

GuiSetState() ;Loop GUI
While 1
  $msg = GuiGetMsg()
    Select
    Case $msg = $GUI_EVENT_CLOSE
        GUIDelete()
        ExitLoop
    Case $msg = $GUI_EVENT_DROPPED
        If @GUI_DropId = $dropFile_1 Then
            GUICtrlSetData($dropFile_1, @GUI_DragFile)
        ElseIf @GUI_DropId = $dropFile_2 Then
            GUICtrlSetData($dropFile_2, @GUI_DragFile)
        Else;If @GUI_DropId = $label Then
            MsgBox(262144, "Drop Result", @GUI_DragFile)
        EndIf
  EndSelect
WEnd

Exit

Drag to anywhere but the two Inputs and the MsgBox will popup with file path.

That should give you enough clues to play around with.

Edited by TheSaint

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

Link to comment
Share on other sites

  • 2 years later...
On 2/14/2018 at 4:03 PM, TheSaint said:

As far as I know, you cannot just drag & drop to a GUI itself. You need to specify a control to receive the drop.

Try this, which importantly has the $label control created last, is the full size of the GUI, and is set as Transparent.

#include <GuiConstants.au3>
#include <GuiConstantsEx.au3>
#include <FileConstants.au3>
#include <EditConstants.au3>

Global $dropFile_1, $dropFile_2, $label

GuiCreate('Drag and Drop', 530, 120, @DesktopWidth / 2 - 192, _
            @DesktopHeight / 2 - 235, -1, $WS_EX_ACCEPTFILES + $WS_EX_TOPMOST)

GUICtrlCreateGroup("Dict Files", 5, 5, 510, 90)
GUICtrlCreateLabel('File 1:', 20, 30)
$dropFile_1 = GUICtrlCreateInput ("", 80, 30, 400, 20, -1)
GUICtrlSetState($dropFile_1, $GUI_DROPACCEPTED)
GUICtrlSetTip($dropFile_1, 'You can drag & drop files here...')

GUICtrlCreateLabel('File 2:', 20, 60)
$dropFile_2 = GUICtrlCreateInput ("", 80, 60, 400, 20, -1)
GUICtrlSetState($dropFile_2, $GUI_DROPACCEPTED)
GUICtrlSetTip($dropFile_2, 'You can drag & drop files here...')

$label = GUICtrlCreateLabel('', 0, 0, 530, 120)
GUICtrlSetBkColor($label, $GUI_BKCOLOR_TRANSPARENT)
GUICtrlSetState($label, $GUI_DROPACCEPTED)

GuiSetState() ;Loop GUI
While 1
  $msg = GuiGetMsg()
    Select
    Case $msg = $GUI_EVENT_CLOSE
        GUIDelete()
        ExitLoop
    Case $msg = $GUI_EVENT_DROPPED
        If @GUI_DropId = $dropFile_1 Then
            GUICtrlSetData($dropFile_1, @GUI_DragFile)
        ElseIf @GUI_DropId = $dropFile_2 Then
            GUICtrlSetData($dropFile_2, @GUI_DragFile)
        Else;If @GUI_DropId = $label Then
            MsgBox(262144, "Drop Result", @GUI_DragFile)
        EndIf
  EndSelect
WEnd

Exit

Drag to anywhere but the two Inputs and the MsgBox will popup with file path.

That should give you enough clues to play around with.

Hi, im trying to use your example.

I dropped files and it is getting their directory. But now i want to start a skript by using a button and use the input like:

 

if $start == 1 then
    
    $path1 = MY FIRST DROPPED FILE like C:\Useres\XY\file.xml
    $path2 = MY SECOND DROPPED FILE C:\Useres\XY\file2.xml
    
    do something here
    
endif

 

How do i do that?

Link to comment
Share on other sites

Maybe this ?

#include <Constants.au3>
#include <GuiConstants.au3>
#include <FileConstants.au3>
#include <EditConstants.au3>

Opt("MustDeclareVars", 1)

Local $dropFile_1, $dropFile_2, $label, $msg

GUICreate('Drag and Drop', 530, 150, @DesktopWidth / 2 - 192, _
    @DesktopHeight / 2 - 235, -1, $WS_EX_ACCEPTFILES + $WS_EX_TOPMOST)

GUICtrlCreateGroup("Dict Files", 5, 5, 510, 90)
GUICtrlCreateLabel('File 1:', 20, 30)
$dropFile_1 = GUICtrlCreateInput("", 80, 30, 400, 20, -1)
GUICtrlSetState($dropFile_1, $GUI_DROPACCEPTED)
GUICtrlSetTip($dropFile_1, 'You can drag & drop files here...')

GUICtrlCreateLabel('File 2:', 20, 60)
$dropFile_2 = GUICtrlCreateInput("", 80, 60, 400, 20, -1)
GUICtrlSetState($dropFile_2, $GUI_DROPACCEPTED)
GUICtrlSetTip($dropFile_2, 'You can drag & drop files here...')

Local $idStart = GUICtrlCreateButton("Start", 215, 110, 100, 25)
Local $sFile1, $sFile2

GUISetState()
While 1
  $msg = GUIGetMsg()
  Switch $msg
    Case $GUI_EVENT_CLOSE
      GUIDelete()
      ExitLoop
    Case $GUI_EVENT_DROPPED
      If @GUI_DropId = $dropFile_1 Then
        GUICtrlSetData($dropFile_1, @GUI_DragFile)
      ElseIf @GUI_DropId = $dropFile_2 Then
        GUICtrlSetData($dropFile_2, @GUI_DragFile)
      EndIf
    Case $idStart
      $sFile1 = GUICtrlRead($dropFile_1)
      $sFile2 = GUICtrlRead($dropFile_2)
      If $sFile1 And $sFile2 Then
        ;   do something here
        ; clear fields afterwards
        GUICtrlSetData($dropFile_1, "")
        GUICtrlSetData($dropFile_2, "")
      Else
        MsgBox ($MB_SYSTEMMODAL,"Error","You must provide both file names")
      EndIf
  EndSwitch
WEnd

 

Link to comment
Share on other sites

  • 2 weeks later...
On 2/17/2020 at 4:46 PM, Nine said:

Maybe this ?

#include <Constants.au3>
#include <GuiConstants.au3>
#include <FileConstants.au3>
#include <EditConstants.au3>

Opt("MustDeclareVars", 1)

Local $dropFile_1, $dropFile_2, $label, $msg

GUICreate('Drag and Drop', 530, 150, @DesktopWidth / 2 - 192, _
    @DesktopHeight / 2 - 235, -1, $WS_EX_ACCEPTFILES + $WS_EX_TOPMOST)

GUICtrlCreateGroup("Dict Files", 5, 5, 510, 90)
GUICtrlCreateLabel('File 1:', 20, 30)
$dropFile_1 = GUICtrlCreateInput("", 80, 30, 400, 20, -1)
GUICtrlSetState($dropFile_1, $GUI_DROPACCEPTED)
GUICtrlSetTip($dropFile_1, 'You can drag & drop files here...')

GUICtrlCreateLabel('File 2:', 20, 60)
$dropFile_2 = GUICtrlCreateInput("", 80, 60, 400, 20, -1)
GUICtrlSetState($dropFile_2, $GUI_DROPACCEPTED)
GUICtrlSetTip($dropFile_2, 'You can drag & drop files here...')

Local $idStart = GUICtrlCreateButton("Start", 215, 110, 100, 25)
Local $sFile1, $sFile2

GUISetState()
While 1
  $msg = GUIGetMsg()
  Switch $msg
    Case $GUI_EVENT_CLOSE
      GUIDelete()
      ExitLoop
    Case $GUI_EVENT_DROPPED
      If @GUI_DropId = $dropFile_1 Then
        GUICtrlSetData($dropFile_1, @GUI_DragFile)
      ElseIf @GUI_DropId = $dropFile_2 Then
        GUICtrlSetData($dropFile_2, @GUI_DragFile)
      EndIf
    Case $idStart
      $sFile1 = GUICtrlRead($dropFile_1)
      $sFile2 = GUICtrlRead($dropFile_2)
      If $sFile1 And $sFile2 Then
        ;   do something here
        ; clear fields afterwards
        GUICtrlSetData($dropFile_1, "")
        GUICtrlSetData($dropFile_2, "")
      Else
        MsgBox ($MB_SYSTEMMODAL,"Error","You must provide both file names")
      EndIf
  EndSwitch
WEnd

 

Works like a charme thank you.

But i have one problem now.

I created a function for my script. But now the basic things arent working anymore.

For example

local $var = "test/test/test"
local $xmlFileName = StringRegExpReplace($var, "^.*\/", "")
MsgBox($MB_SYSTEMMODAL, "Test", $xmlFileName)

--> correct output

--------
CASE 2
--------

func test($var)
    local $xmlFileName = StringRegExpReplace($var, "^.*\/", "")
    MsgBox($MB_SYSTEMMODAL, "Test", $xmlFileName)
endfunc

test("test/test/test")

--> wrong ouput, StringRegExpReplace is doing nothing

All the basic function arent working, like stringSplit or all the others

What am i doing wrong?

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

×
×
  • Create New...