Jump to content

Recommended Posts

Posted (edited)
#include <GUIConstantsEx.au3>

Global $iExitLoop = 0, $hGUIParent1, $hGUIParent2, $iLoadGuiTwo = 0
Example1()
;~ Example2() ; not an example but a GUI ?
Func Example1($iParent = 0)
    Opt("GUIOnEventMode", 1)
    $hGUIParent1 = GUICreate("Parent", 400, 300, -1, -1, -1, -1, $iParent)
    GUISetOnEvent($GUI_EVENT_CLOSE, "OnEvent_CLOSE_ONE", $hGUIParent1)
    GUICtrlCreateButton("load GUI TWO", 8, 8, 100)
    GUICtrlSetOnEvent(-1, "LoadGuiTwoFromGuiOne")
    GUISetState(@SW_SHOW)

    While 1 ; Loop until the user exits.
        Sleep(100)
;~      If $iLoadGuiTwo Then ; .......... solution 1
;~          Example2($hGUIParent1) ; ....
;~          $iLoadGuiTwo = 0 ; ..........
;~      EndIf ; .........................
        If $iExitLoop Then ExitLoop
    WEnd
    $iExitLoop = 0
    GUIDelete($hGUIParent1)
EndFunc   ;==>Example1

Func Example2($iParent = 0)
    Opt("GUIOnEventMode", 1)
    $hGUIParent2 = GUICreate("Child", 300, 400, -1, -1, -1, -1, $iParent)
    GUISetOnEvent($GUI_EVENT_CLOSE, "OnEvent_CLOSE_TWO", $hGUIParent2)
    GUICtrlCreateButton("say hi", 8, 8)
    GUICtrlSetOnEvent(-1, "GuiTwoSayHi")
    GUISetState(@SW_SHOW)

    While 1 ; Loop until the user exits.
        Sleep(100)
        If $iExitLoop Then ExitLoop
    WEnd
    $iExitLoop = 0
    GUIDelete($hGUIParent2)
EndFunc   ;==>Example2


Func OnEvent_CLOSE_ONE()
    ConsoleWrite('- Func OnEvent_CLOSE_ONE()' & @CRLF)
    $iExitLoop = 1
EndFunc   ;==>OnEvent_CLOSE_ONE

Func OnEvent_CLOSE_TWO()
    ConsoleWrite('- Func OnEvent_CLOSE_TWO()' & @CRLF)
    $iExitLoop = 1
EndFunc   ;==>OnEvent_CLOSE_TWO

Func LoadGuiTwoFromGuiOne()
    Example2($hGUIParent1) ; no solution.
;~  $iLoadGuiTwo = 1 ; solution 1
;~  AdlibRegister("LoadGuiTwoRunner", 10) ; solution 2
EndFunc   ;==>LoadGuiTwoFromGuiOne

Func LoadGuiTwoRunner()
    AdlibUnRegister("LoadGuiTwoRunner")
    Example2($hGUIParent1)
EndFunc   ;==>LoadGuiTwoRunner

Func GuiTwoSayHi()
    ConsoleWrite('--- HI' & @CRLF)
EndFunc   ;==>GuiTwoSayHi

..I was using map[] and beta and is this a bug ?. But I coded this sampler and is not a beta thing, is an production thing too.
Is the code as presented supposed to fail ? ( found viable solutions, so this is not an OMG! kind of thing )
I believe that the logic would work if not GuiOnEvent mode.

Edit: Tried in v3.2.12.x and v3.3.8.1 and is a design thing. Not new. I just never noticed it.

Edited by argumentum
oops

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Posted

I've used OnEventMode with multiple GUIs before (for example my PlasmaKit script).

Perhaps, something like this?

#include <GUIConstantsEx.au3>

Global $iExitLoop = 0, $hGUIParent1, $hGUIParent2, $iLoadGuiTwo = 0
Example1()
;~ Example2() ; not an example but a GUI ?
Func Example1($iParent = 0)
    Opt("GUIOnEventMode", 1)
    $hGUIParent1 = GUICreate("Parent", 400, 300, -1, -1, -1, -1, $iParent)
    GUISetOnEvent($GUI_EVENT_CLOSE, "OnEvent_CLOSE_ONE", $hGUIParent1)
    GUICtrlCreateButton("load GUI TWO", 8, 8, 100)
    GUICtrlSetOnEvent(-1, "LoadGuiTwoFromGuiOne")
    GUISetState(@SW_SHOW)

    While 1 ; Loop until the user exits.
        Sleep(100)
        If $iExitLoop Then ExitLoop
    WEnd
    $iExitLoop = 0
    GUIDelete($hGUIParent1)
EndFunc   ;==>Example1

Func Example2($iParent = 0)
;~     Opt("GUIOnEventMode", 1)
    $hGUIParent2 = GUICreate("Child", 300, 400, -1, -1, -1, -1, $iParent)
    GUISetOnEvent($GUI_EVENT_CLOSE, "OnEvent_CLOSE_TWO", $hGUIParent2)
    GUICtrlCreateButton("say hi", 8, 8)
    GUICtrlSetOnEvent(-1, "GuiTwoSayHi")
    GUISetState(@SW_SHOW)

;~     While 1 ; Loop until the user exits.
;~         Sleep(100)
;~         If $iExitLoop Then ExitLoop
;~     WEnd
;~     $iExitLoop = 0
;~     GUIDelete($hGUIParent2)
EndFunc   ;==>Example2


Func OnEvent_CLOSE_ONE()
    ConsoleWrite('- Func OnEvent_CLOSE_ONE()' & @CRLF)
    $iExitLoop = 1
EndFunc   ;==>OnEvent_CLOSE_ONE

Func OnEvent_CLOSE_TWO()
    ConsoleWrite('- Func OnEvent_CLOSE_TWO()' & @CRLF)
    GUIDelete($hGUIParent2)
EndFunc   ;==>OnEvent_CLOSE_TWO

Func LoadGuiTwoFromGuiOne()
    Example2($hGUIParent1) ; no solution.
EndFunc   ;==>LoadGuiTwoFromGuiOne

Func GuiTwoSayHi()
    ConsoleWrite('--- HI' & @CRLF)
EndFunc   ;==>GuiTwoSayHi

Keep in mind, pressing the "load GUI TWO" button again while one is already open will overwrite the handle assigned to $hGUIParent2.  You could account for this in various ways if you intend to not allow this behavior.

Posted

I think the issue ultimately has to do with your second while loop when the 2nd GUI function runs.  That seems to be what is causing the events to not fire from what I can tell.  I'm not sure why it does, but it does.

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