somedude12 Posted December 27, 2014 Posted December 27, 2014 (edited) I thought that we need some better way to handle eventsMaybe someone can help with the code since I am not that good at programmingAlso there is room for an Off function, to delete events or controls from the Dictionary (I need help especially when I tried to call a function defined by the user with less parameters)expandcollapse popup#include <GUIConstantsEx.au3> #include <TreeViewConstants.au3> #include <WindowsConstants.au3> GUIRegisterMsg($WM_NOTIFY, "ON_WM_NOTIFY") Global $onEvents = ObjCreate("Scripting.Dictionary") Func On($event, $control, $func, $param1="", $param2="", $param3="", $param4="", $param5="") If Not $onEvents.Exists($event) Then ConsoleWrite($event&" new event dictionary"&@CRLF) $onEvents.Add($event, ObjCreate("Scripting.Dictionary")) EndIf If Not $onEvents.Item($event).Exists($control) Then ;add the first func for this control on this event ConsoleWrite($control&" new control handlers list"&@CRLF) $onEvents.Item($event).Add($control, ObjCreate("Scripting.Dictionary")) Local $params[] = [$param1,$param2,$param3,$param4,$param5] $onEvents.Item($event).Item($control).Add($func, $params) Else ConsoleWrite("appending event"&@CRLF) Local $params[] = [$param1,$param2,$param3,$param4,$param5] $onEvents.Item($event).Item($control).Add($func, $params) EndIf EndFunc Func ON_WM_NOTIFY($hWnd, $iMsg, $wParam, $lParam) ;wParam=element, lParam=codeStruct #forceref $hWnd, $iMsg, $wParam, $lParam Local $tagNMHDR = DllStructCreate("int hwndFrom; int idFrom; int code; int code2", $lParam) If @error Then Return Local $iEvent = DllStructGetData($tagNMHDR, "code") $wParam = BitAND($wParam, 0x0000FFFF) ;ConsoleWrite($iMsg&@CRLF) If $onEvents.Exists($iEvent) Then ;ConsoleWrite($iEvent&" triggered "&@CRLF) ;ConsoleWrite("control parent "&$wParam&@CRLF) ;ConsoleWrite("searching control "&GUICtrlRead($wParam)&@CRLF) If $onEvents.Item($iEvent).Exists(GUICtrlRead($wParam)) Then ConsoleWrite("calling funcs for event "&$iEvent&@CRLF) Local $funcs = $onEvents.Item($iEvent).Item(GUICtrlRead($wParam)) For $func In $funcs ConsoleWrite("FUNCTION: "&$func&@CRLF) If $funcs.Item($func)[4] Then ;;Here is where I call the function with the same # parameters when registered the event, this can probably be improved Call($func, GUICtrlRead($wParam), $funcs.Item($func)[0],$funcs.Item($func)[1],$funcs.Item($func)[2],$funcs.Item($func)[3],$funcs.Item($func)[4]) ElseIf $funcs.Item($func)[3] Then Call($func, GUICtrlRead($wParam), $funcs.Item($func)[0],$funcs.Item($func)[1],$funcs.Item($func)[2],$funcs.Item($func)[3]) ElseIf $funcs.Item($func)[2] Then Call($func, GUICtrlRead($wParam), $funcs.Item($func)[0],$funcs.Item($func)[1],$funcs.Item($func)[2]) ElseIf $funcs.Item($func)[1] Then Call($func, GUICtrlRead($wParam), $funcs.Item($func)[0],$funcs.Item($func)[1]) ElseIf $funcs.Item($func)[0] Then Call($func, GUICtrlRead($wParam), $funcs.Item($func)[0]) Else Call($func, GUICtrlRead($wParam)) EndIf Next EndIf EndIf $tagNMHDR = 0 Return $GUI_RUNDEFMSG EndFuncExample of use: expandcollapse popupGUICreate("GUI notify", 700, 500) Local $idTreeview = GUICtrlCreateTreeView(5, 5, -1, 490, -1, $WS_EX_CLIENTEDGE) Local $idTreeviewItem1 = GUICtrlCreateTreeViewItem("Table 1", $idTreeview) Local $idTreeviewItem2 = GUICtrlCreateTreeViewItem("Table 2", $idTreeview) Local $idTreeviewItem21 = GUICtrlCreateTreeViewItem("Table 2.1", $idTreeviewItem2) Local $idTreeviewItem22 = GUICtrlCreateTreeViewItem("Table 2.2", $idTreeviewItem2) ;Binding handlers to events ;Func On($event, $control, $func, $param1="", $param2="", $param3="", $param4="", $param5="") On($NM_CLICK, $idTreeviewItem1, "itemclick", "item1 was clicked") On($NM_CLICK, $idTreeviewItem2, "itemclick", "item2 was clicked") On($NM_DBLCLK, $idTreeviewItem1, "itemdblclk", "item1 was double clicked") On($NM_DBLCLK, $idTreeviewItem2, "itemdblclk", "item2 was double clicked") On($TVN_ITEMEXPANDEDW, $idTreeviewItem1, "expand", "item1 expand") On($TVN_ITEMEXPANDEDW, $idTreeviewItem2, "expand", "item2 expand") On($TVN_SELCHANGEDW, $idTreeviewItem1, "selected", "item1 selected") On($TVN_SELCHANGEDW, $idTreeviewItem2, "selected", "item2 selected") GUISetState(@SW_SHOW) While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE ExitLoop EndSwitch WEnd GUIDelete() ;Handling events Func itemclick($controlid, $param1) ; 5 parameters allowed ConsoleWrite($param1 & @CRLF) EndFunc Func itemdblclk($controlid, $param1) ; 5 parameters allowed ConsoleWrite($param1 & @CRLF) EndFunc Func expand($controlid, $param1, $param2) ; 5 parameters allowed ConsoleWrite("expand, id:" & $controlid & " text:" & GUICtrlRead($controlid,1) & @CRLF) ConsoleWrite("param1: " & $param1 & @CRLF) EndFunc Func selected($controlid, $param1) ; 5 parameters allowed ConsoleWrite("selected, id:" & $controlid & " text:" & GUICtrlRead($controlid,1) & @CRLF) ConsoleWrite("param1: " & $param1 & @CRLF) EndFunc Edited December 27, 2014 by somedude12
guinness Posted December 27, 2014 Posted December 27, 2014 Check my signature for _GUIRegisterMsg. 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 parsing • AutoIt Search • AutoIt3 Portable • AutoIt3WrapperToPragma • AutoItWinGetTitle()/AutoItWinSetTitle() • Coding • DirToHTML5 • FileInstallr • FileReadLastChars() • GeoIP database • GUI - Only Close Button • GUI Examples • GUICtrlDeleteImage() • GUICtrlGetBkColor() • GUICtrlGetStyle() • GUIEvents • GUIGetBkColor() • Int_Parse() & Int_TryParse() • IsISBN() • LockFile() • Mapping CtrlIDs • OOP in AutoIt • ParseHeadersToSciTE() • PasswordValid • PasteBin • Posts Per Day • PreExpand • Protect Globals • Queue() • Resource Update • ResourcesEx • SciTE Jump • Settings INI • SHELLHOOK • Shunting-Yard • Signature Creator • Stack() • Stopwatch() • StringAddLF()/StringStripLF() • StringEOLToCRLF() • VSCROLL • WM_COPYDATA • More Examples... Updated: 22/04/2018
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now