Jump to content

Help with setting up Drag n Drop for an Input field...


 Share

Recommended Posts

I've looked all through the online documentation and see that I have to use GUICtrlSetOnEvent(), GUISetOnEvent(), @GUI_DropId, @GUI_DragFile, and @GUI_DragId but I can't seem to get it working initially. I have it set up incorrectly and am now sure what I need to do in order to get the Drop Event function to do anything. Any help would be greatly appreciated.

As of now, since I added the $WS_EX_ACCEPTFILES extended style to my input field, my input field lost the border. And my other problem (that I see) is that for whatever reason, my GUICtrlSetOnEvent() right after my input field control, doesn't pop up the MsgBox() i'm trying to use for testing. Anyone see any problems?

#include <GUIConstants.au3>
#include <file.au3>
#include <array.au3>
#include <Constants.au3>
#include <GuiEdit.au3>
#NoTrayIcon

Opt("GuiOnEventMode", 1)
Opt("RunErrorsFatal", 0)

Global $arrSite[20],$sMenuItem,$menuFileRecent


$arrSite[0] = "Log Parser"      ; Program Name
$arrSite[1] = "v0.1a"           ; Program Version

;__CheckDuplicateProcess()




; ----------        GUI CREATION        -----------
$mainWindow =   GUICreate("Log Parser 0.1a",400,325)



; ----------        MENU        -----------

$menuFileParent     = GUICtrlCreateMenu("&File")
$menuFileOpen       = GUICtrlCreateMenuitem("Open",$menuFileParent)
GUICtrlSetState(-1,$GUI_DEFBUTTON)
GUICtrlSetOnEvent(-1,"__menuFileOpen")
$menuFileRecent     = GUICtrlCreateMenu("Recent Files",$menuFileParent,1)
$menuFileExit       = GUICtrlCreateMenuItem("Exit",$menuFileParent)
GUICtrlSetOnEvent(-1,"__Exit")



$menuHelpParent     = GUICtrlCreateMenu("Help",-1,3)
$menuHelpAbout      = GUICtrlCreateMenuItem("About", $menuHelpParent)





; ----------        CONFIG FILE DIALOG BOX      -----------
Local $CFB_L,$CFB_T

$CFB_L = 10
$CFB_T = 180

GUICtrlCreateLabel("Parse File: ",$CFB_L,$CFB_T,100,18)

$mainFormDotButton  = GUICtrlCreateButton("...", $CFB_L, $CFB_T+15, 25, 20, -1)
GUICtrlSetOnEvent(-1,"__GUIButtonDotOpen")


#############         THIS IS WHERE I'M HAVING PROBLEMS ALSO         ###################
 
$mainFormInput = GUICtrlCreateInput("", $CFB_L+28, $CFB_T+15, 200, 20, $ES_NOHIDESEL, $WS_EX_ACCEPTFILES)
GUICtrlSetState(-1,$GUI_DROPACCEPTED)
GUICtrlSetOnEvent(-1,"__GUIButtondrop")






GUISetOnEvent($GUI_EVENT_CLOSE, "__Exit")
GUISetState(@SW_SHOW)



GUISetState()
While 1
    Sleep(100)
WEnd





Func __menuFileOpen()
    Local $sMenuItem
    Local $openFile = FileOpenDialog("Choose file...",@ScriptDir,"All (*.*)",1+2+4+8)
    
    If $openFile <> "" Then
        __ExecuteTryAll($openFile)
        If @error <> 1 Then 
            $sMenuItem = GUICtrlCreateMenuitem ($openFile,$menuFileRecent)
            GUICtrlSetOnEvent(-1,"__ExePath")
        EndIf
    EndIf

EndFunc



Func __GUIButtonDotOpen()
    Local $guiID
    Local $openFile = FileOpenDialog("Choose file...",@ScriptDir,"All (*.*)",1+2+4+8)
    
    If $openFile <> "" Then
        GUICtrlSetData($mainFormInput, $openFile)
        If @error <> 1 Then 
            $sMenuItem = GUICtrlCreateMenuitem ($openFile,$menuFileRecent)
            GUICtrlSetOnEvent(-1,"__ExePath")
        EndIf
    EndIf
EndFunc



##########     THIS IS WHERE I'M HAVING PROBLEMS     #############

Func __GUIButtondrop()
    MsgBox(0,"",@GUI_DropId & @GUI_DragFile)
;GUICtrlSetData(@GUI_DropId, @GUI_DragFile)
EndFunc


Func __Exit()
    Exit
EndFunc



Func __ExePath()
    Local $g
    $g = GUICtrlRead(@GUI_CtrlId,1)
    ShellExecute($g)
EndFunc



Func __mainFormRunButton()
    Local $strInputVal
    
    $strInputVal = GUICtrlRead($mainFormInput)
    If $strInputVal <> "" Then
        ShellExecute($strInputVal)
        If @error <> 1 Then $sMenuItem = GUICtrlCreateMenuitem ($strInputVal,$menuFileRecent)
        GUICtrlSetOnEvent(-1,"__ExePath")
    EndIf
    
    ControlFocus($mainWindow,"",$mainFormInput)
    ControlSend($mainWindow, "", $mainFormInput, "^{HOME}^+{END}")
EndFunc



Func __CheckDuplicateProcess()
    If WinExists($arrSite[0]) Then
        errHandler(1,"Duplicate Instance Detected", "There was a duplicate instance of " & $arrSite[0] & " detected running.  Only one instance can be running at any one time.")
        Exit
    EndIf
    AutoItWinSetTitle($arrSite[0])
EndFunc




Func errHandler($errNum,$errTitle,$ErrMsg)
    MsgBox(0,"ERROR[" & $errNum & "]:  " & $errTitle,$ErrMsg)
EndFunc
    
    

Func _ShellExecuteEx($sCmd, $sArgs = "", $sFolder = "", $sVerb = "", $iState = @SW_SHOWNORMAL, $hWnd = 0)
    Local $stINFO = DllStructCreate("long;long;long;ptr;ptr;ptr;ptr;long;long;long;ptr;long;long;long;long")
    Local $stVerb = DllStructCreate("char[15];char")
    Local $stPath = DllStructCreate("char[255];char")
    Local $stArgs = DllStructCreate("char[255];char")
    Local $stWDir = DllStructCreate("char[255];char")
    
    DllStructSetData($stVerb, 1, $sVerb)
    
    DllStructSetData($stPath, 1, $sCmd)
    DllStructSetData($stWDir, 1, $sFolder)
    DllStructSetData($stArgs, 1, $sArgs)

    DllStructSetData($stINFO, 1, DllStructGetSize($stINFO))
    DllStructSetData($stINFO, 2, BitOR(0xC, 0x40, 0x400))
    DllStructSetData($stINFO, 3, $hWnd)
    DllStructSetData($stINFO, 4, DllStructGetPtr($stVerb))
    DllStructSetData($stINFO, 5, DllStructGetPtr($stPath))
    DllStructSetData($stINFO, 6, DllStructGetPtr($stArgs))
    DllStructSetData($stINFO, 7, DllStructGetPtr($stWDir))
    DllStructSetData($stINFO, 8, $iState)

    Local $aRet = DllCall("shell32.dll", "int", "ShellExecuteEx", "ptr", DllStructGetPtr($stINFO))
    If Not IsArray($aRet) Or Not $aRet[0] Then Return SetError(2, 0, 0)
    
    Return 1
EndFunc

My Additions:- RunAs AdminDeviant Fun:- Variable Sound Volume

Link to comment
Share on other sites

Hmm... I haven't had to do this in GuiOnEventMode, but would have said there was nothing to it. But when I tried it, it didn't work as I expected. I simplified it to this:

#include <GUIConstants.au3>
#include <WindowsConstants.au3>

Opt("GuiOnEventMode", 1)

; ----------        GUI CREATION        -----------
$mainWindow = GUICreate("Log Parser 0.1a", 400, 325, Default, $WS_EX_ACCEPTFILES)
GUISetOnEvent($GUI_EVENT_CLOSE, "__Exit")
GUISetOnEvent($GUI_EVENT_DROPPED, "__GUI_Event_Dropped")

; ----------        CONFIG FILE DIALOG BOX        -----------
$CFB_L = 10
$CFB_T = 180
GUICtrlCreateLabel("Parse File: ", $CFB_L, $CFB_T, 100, 18)
$mainFormDotButton = GUICtrlCreateButton("...", $CFB_L, $CFB_T + 15, 25, 20, -1)
GUICtrlSetOnEvent(-1, "__GUIButtonDotOpen")

$mainFormInput = GUICtrlCreateInput("", $CFB_L + 28, $CFB_T + 15, 200, 20, -1, $WS_EX_ACCEPTFILES)
GUICtrlSetState(-1, $GUI_DROPACCEPTED)

GUISetState(@SW_SHOW)

While 1
    Sleep(100)
WEnd

Func __GUIButtonDotOpen()
    Local $openFile = FileOpenDialog("Choose file...", @ScriptDir, "All (*.*)", 1 + 2 + 4 + 8)
    If $openFile <> "" Then
        GUICtrlSetData($mainFormInput, $openFile)
    EndIf
EndFunc   ;==>__GUIButtonDotOpen

Func __GUI_Event_Dropped()
    MsgBox(0, "", "DropID = " & @GUI_DropId & @CRLF & "DragFile = " & @GUI_DragFile)
EndFunc   ;==>__GUI_Event_Dropped

Func __Exit()
    Exit
EndFunc   ;==>__Exit

The $WS_EX_ACCEPTFILES option is supposed to be on the GUI, not on the control, and that's the way it works in a GuiGetMsg() loop, but that doesn't even show the mouse prompt for drop in GuiOnEventMode without the style applied to the control. I'm missing something here...

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

  • 7 months later...

Hello, sorry to bump into an old thread, I'm having a problem with drag n drop, I have made it work in OnEventMode but I'm having another problem...

Part of my code is:

$SecGUI = GUICreate("Options",500,180,-175,Default,0,$WS_EX_MDICHILD+$WS_EX_ACCEPTFILES,$MainGUI)
GUICtrlCreateLabel("Source:",5,7,45,20)
$SourceInput = GUICtrlCreateInput($SourcePath,80,5,348,20,$ES_AUTOHSCROLL)
$Graphic = GUICtrlCreateGraphic(407, 62, 80, 80)
GUICtrlSetState($Graphic,$GUI_DROPACCEPTED)
GUICtrlSetOnEvent($GUI_EVENT_DROPPED,"GetText")
GUICtrlSetBkColor($Graphic, 0xEAEAEA)
GUICtrlSetColor($Graphic, 0x0000)
GUICtrlSetGraphic($Graphic, $GUI_GR_COLOR, 0x0000)
$DropLabel = GUICtrlCreateLabel("Drop Folders",415,93,70,20)
GUICtrlSetCursor ($Graphic,3)
GUICtrlSetState($DropLabel, $GUI_DISABLE)
GUICtrlSetBkColor($DropLabel, 0xEAEAEA)

This works perfectly if I drop a file on that area, the problem is if I only just click on that area before dropping anything my program exits with an error because the @GUI_DRAGFILE is not valid...

Is there any way of seeting @GUI_DRAGFILE to have some default value to avoid that?

If anyone think he can help I will post the whole program (part of something larger anyway)

Edited by AoRaToS

s!mpL3 LAN Messenger

Current version 2.9.9.1 [04/07/2019]

s!mpL3 LAN Messenger.zip

s!mpL3

Link to comment
Share on other sites

Hello, sorry to bump into an old thread, I'm having a problem with drag n drop, I have made it work in OnEventMode but I'm having another problem...

Part of my code is:

$SecGUI = GUICreate("Options",500,180,-175,Default,0,$WS_EX_MDICHILD+$WS_EX_ACCEPTFILES,$MainGUI)
GUICtrlCreateLabel("Source:",5,7,45,20)
$SourceInput = GUICtrlCreateInput($SourcePath,80,5,348,20,$ES_AUTOHSCROLL)
$Graphic = GUICtrlCreateGraphic(407, 62, 80, 80)
GUICtrlSetState($Graphic,$GUI_DROPACCEPTED)
GUICtrlSetOnEvent($GUI_EVENT_DROPPED,"GetText")
GUICtrlSetBkColor($Graphic, 0xEAEAEA)
GUICtrlSetColor($Graphic, 0x0000)
GUICtrlSetGraphic($Graphic, $GUI_GR_COLOR, 0x0000)
$DropLabel = GUICtrlCreateLabel("Drop Folders",415,93,70,20)
GUICtrlSetCursor ($Graphic,3)
GUICtrlSetState($DropLabel, $GUI_DISABLE)
GUICtrlSetBkColor($DropLabel, 0xEAEAEA)

This works perfectly if I drop a file on that area, the problem is if I only just click on that area before dropping anything my program exits with an error because the @GUI_DRAGFILE is not valid...

Is there any way of seeting @GUI_DRAGFILE to have some default value to avoid that?

If anyone think he can help I will post the whole program (part of something larger anyway)

You don't provide the event ID for GuiCtrlSetOnEvent() (see help file for parameters). Try:
GUICtrlSetOnEvent($Graphic,"GetText")

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
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...