Jump to content

Drag and drop dir / folder only onto input control in GUI


Mungo
 Share

Recommended Posts

I am trying restrict the drag and drop action to folder paths only (rather than path and file names).

I am using the extended style $WS_EX_ACCEPTFILES during gui creation and GUICtrlSetState(-1, $GUI_DROPACCEPTED) on the input control (sample code below).

Thanks for any hints.

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
f_gui_drop()
Func f_gui_drop()
  Local $msg
  GUICreate("Test drop DIR only", 300, 300, -1, -1, -1, $WS_EX_ACCEPTFILES)
  GUICtrlCreateLabel("Drop a dir only ... " , 10, 10, 200, 20)
  Local $dir_dropped = GUICtrlCreateInput("", 10, 30, 280, 20)
  GUICtrlSetState(-1, $GUI_DROPACCEPTED)
  GUISetState(@SW_SHOW)
  While 1
       $msg = GUIGetMsg()
       If $msg = $GUI_EVENT_CLOSE Then ExitLoop
  WEnd
  GUIDelete()
EndFunc
Edited by Mungo
Link to comment
Share on other sites

Try this:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
f_gui_drop()
Func f_gui_drop()
  Local $msg
  GUICreate("Test drop DIR only", 300, 300, -1, -1, -1, $WS_EX_ACCEPTFILES)
  GUICtrlCreateLabel("Drop a dir only ... " , 10, 10, 200, 20)
  Local $dir_dropped = GUICtrlCreateInput("", 10, 30, 280, 20)
  GUICtrlSetState(-1, $GUI_DROPACCEPTED)
  GUISetState(@SW_SHOW)

  While 1
      Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            ExitLoop
        Case $GUI_EVENT_DROPPED
            GUICtrlSetData($dir_dropped, StringRegExpReplace(GUICtrlRead($dir_dropped), "(.*).*", "$1"))
    EndSwitch

  WEnd
  GUIDelete()
EndFunc

Br,

UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

Thanks UEZ!

That works fine ! It strips off the filename nicely if a file is being dropped. However, the problem is if the user actually does what he is supposed to do (in my program) and drops a DIR onto the input control, it strips off one dir level (to the next ""), which shouldn't be.

I found a way by using FileGetAttrib in a condition to check for the file attribute to be "D" for DIRECTORY within the dropped string and combined it with your StringRegExpReplace line. That seems to do the trick :-)

Cheers

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
Opt('MustDeclareVars', 1)
f_gui_drop()
Func f_gui_drop()
  GUICreate("Test drop DIR only", 400, 300, -1, -1, -1, $WS_EX_ACCEPTFILES)
  GUICtrlCreateLabel("Drop a dir only ... " , 10, 10, 200, 20)
  Local $dir_dropped = GUICtrlCreateInput("", 10, 30, 380, 20)
  GUICtrlSetState(-1, $GUI_DROPACCEPTED)
  Local $clear_button = GUICtrlCreateButton("Clear", 160, 200, 80, 24)
  GUISetState(@SW_SHOW)
  While 1
    Switch GUIGetMsg()
      Case $GUI_EVENT_CLOSE
        ExitLoop
      Case $GUI_EVENT_DROPPED
        Local $dir_string = GUICtrlRead($dir_dropped)
        Local $attribute = FileGetAttrib($dir_string)    ; Retrieve the file/dir attributes
        If StringInStr($attribute, "D") Then         ; If the attribute string contains the letter 'D' then it is a DIR.
          MsgBox(4096, "", "It is a DIR - OK !")
        Else
          GUICtrlSetData($dir_dropped, StringRegExpReplace(GUICtrlRead($dir_dropped), "(.*).*", "$1")) ; Strip filename from path
          MsgBox(4096, "", "It is a FILE - Filename stripped from PATH!")
        EndIf
      Case $clear_button
        GUICtrlSetData($dir_dropped, "")
    EndSwitch
  WEnd
  GUIDelete()
  EndFunc
Edited by Mungo
Link to comment
Share on other sites

  • 3 months later...

...
Local $attribute = FileGetAttrib($dir_string) ; Retrieve the file/dir attributes
If StringInStr($attribute, "D") Then ; If the attribute string contains the letter 'D' then it is a DIR.
MsgBox(4096, "", "It is a DIR - OK !")
...

Why not use _WinAPI_PathIsDirectory from WinAPIEx? It's included in the beta.

Regards,

Cyberbit

_ArrayConcatenate2D · Aero Flip 3D · EPOCH (destroy timestamps) · File Properties Modifier · _GetFileType · UpdateEngine<new> · In-line Case (_ICase) <new>

[hr]

50% of the time, it works all the time. -PezoFaSho

Link to comment
Share on other sites

Because not everyone is using WinAPIEx and/or has the beta version of AutoIt installed. Nothing wrong with using native functions.

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

  • 3 months later...

try this :

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

f_gui_drop()

Func f_gui_drop()
Local $msg
GUICreate("Test drop DIR only", 300, 300, -1, -1, -1, $WS_EX_ACCEPTFILES)
GUICtrlCreateLabel("Drop a dir only ... " , 10, 10, 200, 20)
Local $dir_dropped = GUICtrlCreateInput("", 10, 30, 280, 20)
GUICtrlSetState(-1, $GUI_DROPACCEPTED)
GUISetState(@SW_SHOW)

While 1
Switch GUIGetMsg()
Case $GUI_EVENT_CLOSE
ExitLoop
Case $GUI_EVENT_DROPPED
Local $attribute = FileGetAttrib(GUICtrlRead($dir_dropped)) ; Retrieve the file/dir attributes
If StringInStr($attribute, "D") Then ; If the attribute string contains the letter 'D' then it is a DIR.
Else
GUICtrlSetData($dir_dropped,"")
EndIf
EndSwitch
WEnd
GUIDelete()
EndFunc

:zorro:

[center]I LOVE ALGERIA .... ;-)[/center]
Link to comment
Share on other sites

  • 9 months later...


Case $GUI_EVENT_DROPPED

 

 

I'm using this technique.  It works fine if the input is empty.  But if I load the input from an .ini file, then later if a file path is dropped, the old data is not erased.  I end up with, as example,  C:MyFolderMyFile.txtC:OtherFolderotherfile.txt in the input.

Since I have 2 inputs I don't know where the drop is going to try to erase it with SetData("")

Edit: and just having the inputs start blank is no help as the user will want to know what files were saved to the ini file.

I'm using AutoIt 3.3.8.1

Edit:  @GUI_DropId did the trick

Edited by MilesAhead
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...