Sign in to follow this  
Followers 0
DrLarch

Using GUISetOnEvent with Drag & Drop?

8 posts in this topic

#1 ·  Posted (edited)

I've barely any experienced working with GUI's (on my second try) and have been struggling with this and have been searching the forums, but obviously am stuck on the overall concept. I have a GUI that I setup with Koda and I have a GUICtrlCreateList drag & drop element along with a button and an input field.

I'm trying to adapt the example from along with the code generated by Koda but am stuck. My fields will work with Koda's output, but the drag & drop won't or vica versa. Obviously it has to do with how the events are being triggered. Koda uses the GUISetOnEvent method, while the drag & drop routine in the above link works differently.

From Koda:

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
Opt("GUIOnEventMode", 1)
#Region ### START Koda GUI section ###
$Form1 = GUICreate("Send File to Fax", 362, 297, 241, 135)
GUISetOnEvent($GUI_EVENT_CLOSE, "Form1Close")
GUISetOnEvent($GUI_EVENT_MINIMIZE, "Form1Minimize")
GUISetOnEvent($GUI_EVENT_MAXIMIZE, "Form1Maximize")
GUISetOnEvent($GUI_EVENT_RESTORE, "Form1Restore")
$Edit1 = GUICtrlCreateEdit("", 0, 0, 361, 241)
GUICtrlSetData($Edit1, "Edit1")
GUICtrlSetOnEvent($Edit1, "Edit1Change")
$Button1 = GUICtrlCreateButton("Fax!", 288, 248, 57, 41)
GUICtrlSetFont($Button1, 14, 800, 0, "MS Sans Serif")
GUICtrlSetOnEvent($Button1, "Button1Click")
$Fax = GUICtrlCreateInput("Fax", 128, 256, 145, 28)
GUICtrlSetFont($Fax, 12, 800, 0, "MS Sans Serif")
GUICtrlSetOnEvent($Fax, "FaxChange")
$Label1 = GUICtrlCreateLabel("Fax Number:", 16, 259, 105, 24)
GUICtrlSetFont($Label1, 12, 800, 0, "MS Sans Serif")
GUICtrlSetOnEvent($Label1, "Label1Click")
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
While 1
Sleep(100)
WEnd
Func Button1Click()
MsgBox(0,"Test","Button Pushed")
EndFunc
Func Edit1Change()
MsgBox(0,"Test","Function called...")
EndFunc
Func FaxChange()
MsgBox(0,"Test","Test")
EndFunc
Func Form1Close()
Exit
EndFunc
Func Form1Maximize()
EndFunc
Func Form1Minimize()
EndFunc
Func Form1Restore()
EndFunc
Func Label1Click()
EndFunc

And the example I linked to:

#include <GUIConstants.au3>
Global $WM_DROPFILES = 0x233
Global $gaDropFiles[1], $str = ""
### Koda GUI section start ###
$hGUI = GUICreate("Test", 400, 200, 219, 178, -1, BitOR($WS_EX_ACCEPTFILES, $WS_EX_TOPMOST))
$hList = GUICtrlCreateList("", 5, 5, 390, 190)
GUICtrlSetState (-1, $GUI_DROPACCEPTED)
GUISetState(@SW_SHOW)
### Koda GUI section end ###
GUIRegisterMsg ($WM_DROPFILES, "WM_DROPFILES_FUNC")
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
     Case $GUI_EVENT_CLOSE
         Exit
     Case $GUI_EVENT_DROPPED
         $str = ""
         For $i = 0 To UBound($gaDropFiles) - 1
             $str &= "" & $gaDropFiles[$i]
         Next
         GUICtrlSetData($hList, $str)
EndSwitch
WEnd
Func WM_DROPFILES_FUNC($hWnd, $msgID, $wParam, $lParam)
Local $nSize, $pFileName
Local $nAmt = DllCall("shell32.dll", "int", "DragQueryFile", "hwnd", $wParam, "int", 0xFFFFFFFF, "ptr", 0, "int", 255)
For $i = 0 To $nAmt[0] - 1
     $nSize = DllCall("shell32.dll", "int", "DragQueryFile", "hwnd", $wParam, "int", $i, "ptr", 0, "int", 0)
     $nSize = $nSize[0] + 1
     $pFileName = DllStructCreate("char[" & $nSize & "]")
     DllCall("shell32.dll", "int", "DragQueryFile", "hwnd", $wParam, "int", $i, "ptr", DllStructGetPtr($pFileName), "int", $nSize)
     ReDim $gaDropFiles[$i+1]
     $gaDropFiles[$i] = DllStructGetData($pFileName, 1)
     $pFileName = 0
Next
EndFunc

I like the way this multiple drag & drop example works, but am hitting a wall on how to integrate it with my GUI. I've also found that this example works as it is written with GUICtrlCreateList, but not as GUICtrlCreateListView even though the parameters are identical.

My last hurdle is how to adapt the drag & drop example to be able to delete selected files from the list. Any help would be greatly :) appreciated as GUI's are obviously not my forte. Going home now :...

Edited by DrLarch

Share this post


Link to post
Share on other sites



DrLarch,

Was there actually a specific question in all that? Because I could not find one. :wacko:

Here is a an example of how to set up a List control to accept dropped files in OnEvent mode: ;)

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

Opt("GUIOnEventMode", 1)

$hGUI = GUICreate("Test", 500, 500, 50, 50, Default, $WS_EX_ACCEPTFILES)
GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")
GUISetOnEvent($GUI_EVENT_DROPPED, "_Drop")

$cList = GUICtrlCreateList("", 10, 10, 480, 200)
GUICtrlSetState(-1, $GUI_DROPACCEPTED)

GUISetState()

While 1
    Sleep(10)
WEnd

Func _Drop()
    GUICtrlSetData($cList, @GUI_DragFile)
EndFunc

Func _Exit()
    Exit
EndFunc

As you can see, you do not need the complex code in the example. That dated from 2006 - AutoIt has developed a great deal since then and using code that old is often counter-productive. ;)

Please ask if you have any difficulty with the example - or if you actually have a question that it does not answer. :D

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Share this post


Link to post
Share on other sites

DrLarch,,

Sorry, i now see a question about deleting items: ;)

#include <Array.au3>

Opt("GUIOnEventMode", 1)

$hGUI = GUICreate("Test", 500, 500, 50, 50, Default, $WS_EX_ACCEPTFILES)
GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")
GUISetOnEvent($GUI_EVENT_DROPPED, "_Drop")

$cList = GUICtrlCreateList("", 10, 10, 480, 200)
GUICtrlSetState(-1, $GUI_DROPACCEPTED)

$cButton = GUICtrlCreateButton("Delete", 10, 300, 80, 30)
GUICtrlSetOnEvent(-1, "_Delete")

GUISetState()

While 1
    Sleep(10)
WEnd

Func _Drop()
    GUICtrlSetData($cList, @GUI_DragFile)
EndFunc

Func _Delete()
    $sItem = GUICtrlRead($cList)
    $iIndex = _GUICtrlListBox_FindString($cList, $sItem)
    _GUICtrlListBox_DeleteString($cList, $iIndex)
EndFunc

Func _Exit()
    Exit
EndFunc

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Many thanks Melba! :D I got it going much better now with your very helpful examples...

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#Include <GuiListBox.au3>
#Include <Array.au3>
Global $a_SelectedItems = 0
Opt("GUIOnEventMode", 1)
#Region ### START Koda GUI section ###
$hGUI = GUICreate("Send File to Fax", 362, 223, 258, 162)
GUISetOnEvent($GUI_EVENT_CLOSE, "hGUIClose")
GUISetOnEvent($GUI_EVENT_MINIMIZE, "hGUIMinimize")
GUISetOnEvent($GUI_EVENT_MAXIMIZE, "hGUIMaximize")
GUISetOnEvent($GUI_EVENT_RESTORE, "hGUIRestore")
$cList = GUICtrlCreateList("", 0, 0, 361, 145,Default, $WS_EX_ACCEPTFILES)
GUISetOnEvent($GUI_EVENT_DROPPED, "_Drop")
GUICtrlSetState(-1, $GUI_DROPACCEPTED)
$bSend = GUICtrlCreateButton("Fax!", 280, 184, 57, 28)
GUICtrlSetFont(-1, 13, 800, 0, "MS Sans Serif")
GUICtrlSetOnEvent(-1, "bSendClick")
$fNumber = GUICtrlCreateInput("", 128, 184, 145, 28)
GUICtrlSetFont(-1, 12, 800, 0, "MS Sans Serif")
GUICtrlSetOnEvent(-1, "fNumberChange")
$Label = GUICtrlCreateLabel("Fax Number:", 18, 187, 105, 24)
GUICtrlSetFont(-1, 12, 800, 0, "MS Sans Serif")
;GUICtrlSetOnEvent(-1, "LabelClick")
$bDelete = GUICtrlCreateButton("Delete Selected", 128, 152, 105, 25)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
GUICtrlSetOnEvent(-1, "bDeleteClick")
$bClear = GUICtrlCreateButton("Clear All", 240, 152, 105, 25)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
GUICtrlSetOnEvent(-1, "bClearClick")
$Icon = GUICtrlCreateIcon("C:\Windows\System32\shell32.dll", -106, 48, 150, 32, 32)
;GUICtrlSetOnEvent(-1, "IconClick")
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
GUISetState()
While 1
Sleep(100)
WEnd
Func _Drop()
GUICtrlSetData($cList, @GUI_DragFile)
EndFunc
Func bDeleteClick()
$sItem = GUICtrlRead($cList)
$iIndex = _GUICtrlListBox_FindString($cList, $sItem)
_GUICtrlListBox_DeleteString($cList, $iIndex)
EndFunc
Func bClearClick()
GUICtrlSetData($cList, "")
EndFunc
Func bSendClick()
$FileCount = _GUICtrlListBox_GetCount($cList)
Global $FileList[$FileCount + 1]
For $i = 0 To $FileCount
$FileList[$i] = _GUICtrlListBox_GetText($cList,$i)
Next
_ArrayDelete($FileList, $i)
_ArrayDisplay($FileList)
bClearClick()
EndFunc
Func FaxChange()
MsgBox(0,"Test","Test")
EndFunc
Func hGUIClose()
Exit
EndFunc
Func hGUIMaximize()
EndFunc
Func hGUIMinimize()
EndFunc
Func hGUIRestore()
EndFunc

What I'd like to be able to do now is drag-drop multiple files at a time like (again, the example from my first post that Melba said is old... so not sure where to go on this) and also be able to multi-select delete from the GUI (as ). Still working on this, but any further help appreciated (again).

Edit: Oh, forgot to add this in again - I like the GUICtrlCreateListView appearance with the headings as I'd like to add an index column to the left of the file names, but it doesn't seem to work with drag and drop like GUICtrlCreateList does (probably because I'm missing something... :ermm: )

Edited by DrLarch

Share this post


Link to post
Share on other sites

DrLarch,

If you want multiple dropped files we need to go another route - look for the <<<<<<<< lines: ;)

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

;Global $a_SelectedItems ; No need to declare as Global - we only need it as Local within the function <<<<<<<<<
Global $aDrop_List ; Declare array to hold dropped items <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

Opt("GUIOnEventMode", 1)

$hGUI = GUICreate("Send File to Fax", 362, 223, 258, 162, Default, $WS_EX_ACCEPTFILES) ; You need the extended style here <<<<<<<<
GUISetOnEvent($GUI_EVENT_CLOSE, "hGUIClose")

$cList = GUICtrlCreateList("", 0, 0, 361, 145, $LBS_EXTENDEDSEL) ; Permit multiple selections <<<<<<<<<<<<<<<<<<

$bSend = GUICtrlCreateButton("Fax!", 280, 184, 57, 28)
GUICtrlSetFont(-1, 13, 800, 0, "MS Sans Serif")
GUICtrlSetOnEvent(-1, "bSendClick")
$fNumber = GUICtrlCreateInput("", 128, 184, 145, 28)
GUICtrlSetFont(-1, 12, 800, 0, "MS Sans Serif")
;GUICtrlSetOnEvent(-1, "fNumberChange")
$Label = GUICtrlCreateLabel("Fax Number:", 18, 187, 105, 24)
GUICtrlSetFont(-1, 12, 800, 0, "MS Sans Serif")
;GUICtrlSetOnEvent(-1, "LabelClick")
$bDelete = GUICtrlCreateButton("Delete Selected", 128, 152, 105, 25)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
GUICtrlSetOnEvent(-1, "bDeleteClick")
$bClear = GUICtrlCreateButton("Clear All", 240, 152, 105, 25)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
GUICtrlSetOnEvent(-1, "bClearClick")
$Icon = GUICtrlCreateIcon("C:\Windows\System32\shell32.dll", -106, 48, 150, 32, 32)

$cDrop_Dummy = GUICtrlCreateDummy()
GUICtrlSetOnEvent(-1, "_On_Drop")

GUISetState(@SW_SHOW)

; Register $WM_DROPFILES function to detect drops anywhere on the GUI <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
GUIRegisterMsg(0x233, "On_WM_DROPFILES") ; $WM_DROPFILES

While 1
    Sleep(10)
WEnd

Func _On_Drop() ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    For $i = 1 To $aDrop_List[0]
        GUICtrlSetData($cList, $aDrop_List[$i])
    Next
EndFunc   ;==>_On_Drop

Func bDeleteClick() ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    Local $a_SelectedItems = _GUICtrlListBox_GetSelItems($cList)
    For $i = $a_SelectedItems[0] To 1 Step -1
        _GUICtrlListBox_DeleteString($cList, $a_SelectedItems[$i])
    Next
EndFunc   ;==>bDeleteClick

Func bClearClick()
    GUICtrlSetData($cList, "")
EndFunc   ;==>bClearClick

Func bSendClick()
    $FileCount = _GUICtrlListBox_GetCount($cList)
    Global $FileList[$FileCount + 1]
    For $i = 0 To $FileCount
        $FileList[$i] = _GUICtrlListBox_GetText($cList, $i)
    Next
    _ArrayDelete($FileList, $i)
    _ArrayDisplay($FileList)
    bClearClick()
EndFunc   ;==>bSendClick

Func FaxChange()
    MsgBox(0, "Test", "Test")
EndFunc   ;==>FaxChange

Func hGUIClose()
    Exit
EndFunc   ;==>hGUIClose

; React to items dropped on the GUI <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Func On_WM_DROPFILES($hWnd, $iMsg, $wParam, $lParam)
    ; Credit to ProgAndy for DLL calls
    #forceref $hWnd, $iMsg, $lParam
    Local $iSize, $pFileName
    ; Get number of files dropped
    Local $aRet = DllCall("shell32.dll", "int", "DragQueryFileW", "hwnd", $wParam, "int", 0xFFFFFFFF, "ptr", 0, "int", 0)
    ; Reset array to correct size
    Global $aDrop_List[$aRet[0] + 1] = [$aRet[0]]
    ; And add item names
    For $i = 0 To $aRet[0] - 1
        $aRet = DllCall("shell32.dll", "int", "DragQueryFileW", "hwnd", $wParam, "int", $i, "ptr", 0, "int", 0)
        $iSize = $aRet[0] + 1
        $pFileName = DllStructCreate("wchar[" & $iSize & "]")
        DllCall("shell32.dll", "int", "DragQueryFileW", "hwnd", $wParam, "int", $i, "ptr", DllStructGetPtr($pFileName), "int", $iSize)
        $aDrop_List[$i + 1] = DllStructGetData($pFileName, 1)
        $pFileName = 0
    Next
    ; Send the count to trigger the drop function in the main loop
    GUICtrlSendToDummy($cDrop_Dummy, $aDrop_List[0])
EndFunc   ;==>On_WM_DROPFILES

The multiple delete is pretty easy as you can see. ;)

Doing the same thing with a ListView (which is a very different animal to a ListBox) is not too difficult as the principles are the same - why not give it a go and see how you get on. :)

M23

1 person likes this

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

:thumbsup: Thanks Melba! Your examples are helping me get a better idea of how this works. I got both multiple drag & drop working along with multiple delete. I also used your ExtMsgBox UDF for dialogs - I just need to figure out now how to change the font/style within a dialog (looks like it's in the StringSize UDF), but that's probably more appropriate for the original I'll post there if I can't figure it out...

Again, thanks for your help!!!

Share this post


Link to post
Share on other sites

DrLarch,

I also used your ExtMsgBox UDF for dialogs - I just need to figure out now how to change the font/style within a dialog

Did you figure it out? :huh:

Hint:

Look at the _ExtMsgBoxSet function. ;)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
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
Sign in to follow this  
Followers 0