Jump to content

Recommended Posts

Posted

Hi!

Sometimes, you have multiple GUIs in your project. Even more, you don't know how many GUIs you have in advance (dynamically managed GUIs).

And you want to be able to have a different message handler for every GUI and/or message.

This small function is for you!

It lets you register/unregister message handlers that will be specific to every GUI/message.

Drawbacks:

  • you can not use standard GUIRegisterMsg in your script anymore.
  • [not really a proble] you must unregister message handler when a GUI is destroyed.

 

#include-once
#include <GUIConstantsEx.au3>

Global $__gMGRM_oGUIs = ObjCreate("Scripting.Dictionary") ; obj["0xHWND:MSGID"] = sFuncName
Global $__gMGRM_oMSGs = ObjCreate("Scripting.Dictionary") ; obj[iMsgID] = NumGUIs

Func _MultiGUIRegisterMsg($hGUI, $iMsg, $vFunc = Null)
    If IsString($iMsg) Then
        $iMsg = StringSplit($iMsg, " ,-|;.")
        For $i = 1 To $iMsg[0]
            _MultiGUIRegisterMsg($hGUI, Int($iMsg[$i]), $vFunc)
        Next
        Return
    EndIf

    If IsFunc($vFunc) Or IsString($vFunc) Then
        ; register message dispatcher, and increment message counter
        If Not $__gMGRM_oMSGs.Exists($iMsg) Or $__gMGRM_oMSGs.Item($iMsg) <= 0 Then
            GUIRegisterMsg($iMsg, __multiGUIRegisterMsg_dispatcher)
            $__gMGRM_oMSGs.Item($iMsg) = 1
        Else
            $__gMGRM_oMSGs.Item($iMsg) = $__gMGRM_oMSGs.Item($iMsg) + 1
        EndIf

        ; register message handler for this GUI
        $__gMGRM_oGUIs.Item(String($hGUI) & ":" & String($iMsg)) = IsFunc($vFunc) ? FuncName($vFunc) : $vFunc
    Else
        ; decrement message counter, unregister message dispatcher if counter reaches 0
        $__gMGRM_oMSGs.Item($iMsg) = $__gMGRM_oMSGs.Item($iMsg) - 1
        If $__gMGRM_oMSGs.Item($iMsg) <= 0 Then
            GUIRegisterMsg($iMsg, "")
            $__gMGRM_oMSGs.Remove($iMsg)
        EndIf

        ; unregister message handler for this GUI
        $__gMGRM_oGUIs.Remove(String($hGUI) & ":" & String($iMsg))
    EndIf
EndFunc

; -------------------------------------------------------------------------------------------------
; Internals

Func __multiGUIRegisterMsg_dispatcher($hWnd, $iMsg, $wParam, $lParam)
    Local $sFunc = $__gMGRM_oGUIs.Item(String($hWnd) & ":" & String($iMsg))
    If $sFunc Then Return Call($sFunc, $hWnd, $iMsg, $wParam, $lParam)
    Return $GUI_RUNDEFMSG
EndFunc

 

 

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
×
×
  • Create New...