Jump to content

Easy $WM_NOTIFY events registration


somedude12
 Share

Recommended Posts

I thought that we need some better way to handle events

Maybe someone can help with the code since I am not that good at programming

Also 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)

#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
EndFunc
Example of use:

 

GUICreate("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 by somedude12
Link to comment
Share on other sites

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 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

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...