Jump to content

[Solved] Handling mutiple windows with onevent mode


Recommended Posts

Here is a little something I'm unable to understand.

I have a simple window with a button on it.

When the button is pressed, the window is destroyed and a second window opens.

The second windows also contains a button.

When the button on the second window is pressed, the second window is destroyed and the first is created again.

The problem is that once the second window is created, the events of are not catched.

Posted Image

Could someone please tell me why following code is not working?

I could really use some help on this.

; --------
; Includes
; --------

#include <GuiCOnstants.au3>


; -------
; Options
; -------

AutoItSetOption("GUIOnEventMode", 1)


; ---------
; Variables
; ---------

Global $window1
Global $window1events
Global $window2
Global $window2events

Global $button1
Global $button2


; ---------
; Functions
; ---------

; window 1
Func _CreateWindow1()   
    $window1 = GuiCreate("window 1", 180, 45)
    GUISetOnEvent($GUI_EVENT_CLOSE, "_Event_Window1_Close", $window1)
    
    $button1 = GUICtrlCreateButton("button 1", 60, 10, 60, 25)
    GUICtrlSetOnEvent($button1, "_Event_Window1_Button1")
    
    GUISetState(@SW_SHOW, $window1)
    GUISwitch($window1)

    $window1events = True
    
    While $window1events
        Sleep(1000)
    WEnd
EndFunc


Func _DestroyWindow1()  
    $window1events = False
    GUISetState(@SW_HIDE, $window1)
    GuiDelete($window1)
EndFunc


Func _Event_Window1_Close() 
    _DestroyWindow1()
EndFunc


Func _Event_Window1_Button1()   
    _DestroyWindow1()
    _CreateWindow2()
EndFunc


; window 2
Func _CreateWindow2()   
    $window2 = GuiCreate("window 2", 180, 45)
    GUISetOnEvent($GUI_EVENT_CLOSE, "_Event_Window2_Close", $window2)
    
    $button2 = GUICtrlCreateButton("button 2", 60, 10, 60, 25)
    GUICtrlSetOnEvent($button2, "_Event_Window2_Button2")
    
    GUISetState(@SW_SHOW, $window2)
    GUISwitch($window2)
    $window2events = True
    
    While $window2events
        Sleep(1000)
    WEnd
EndFunc


Func _DestroyWindow2()  
    $window2events = False
    GUISetState(@SW_HIDE, $window2)
    GuiDelete($window2)
EndFunc


Func _Event_Window2_Close()
    _DestroyWindow2()
EndFunc


Func _Event_Window2_Button2()
    _DestroyWindow2()
    _CreateWindow1()
EndFunc


; -------
; Runtime
; -------

_CreateWindow1()
Edited by Psychoman
Link to comment
Share on other sites

Have a look at the Wiki about Multiple GUI's >> http://www.autoitscript.com/wiki/Managing_Multiple_GUIs

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

Thank for the tutorial.

Although I have read it I still don't understand why my script isn't working.

These are the differences I see:

- The tutorial contains only 1 main loop

In my script I have 2, one for handling the incoming events of each window

- The tutorial always keeps 1 window active

In my script only 1 window exists at the same time

I'm sorry but I still don't understand why my script isn't working.

Link to comment
Share on other sites

Give this a try

; --------
; Includes
; --------

#include <GuiCOnstants.au3>


; -------
; Options
; -------

AutoItSetOption("GUIOnEventMode", 1)


; ---------
; Variables
; ---------

Global $window1
Global $window1events
Global $window2
Global $window2events

Global $button1
Global $button2


; -------
; Runtime
; -------

_CreateWindow1()
While 1
    Sleep(10)
WEnd


; ---------
; Functions
; ---------

; window 1
Func _CreateWindow1()
    $window1 = GUICreate("window 1", 180, 45)
    GUISetOnEvent($GUI_EVENT_CLOSE, "_Event_Window1_Close", $window1)

    $button1 = GUICtrlCreateButton("button 1", 60, 10, 60, 25)
    GUICtrlSetOnEvent($button1, "_Event_Window1_Button1")

    GUISetState(@SW_SHOW, $window1)
    GUISwitch($window1)

    $window1events = True

EndFunc   ;==>_CreateWindow1


; window 2
Func _CreateWindow2()
    $window2 = GUICreate("window 2", 180, 45)
    GUISetOnEvent($GUI_EVENT_CLOSE, "_Event_Window2_Close", $window2)

    $button2 = GUICtrlCreateButton("button 2", 60, 10, 60, 25)
    GUICtrlSetOnEvent($button2, "_Event_Window2_Button2")

    GUISetState(@SW_SHOW, $window2)
    GUISwitch($window2)
    $window2events = True

EndFunc   ;==>_CreateWindow2


Func _DestroyWindow1()
    $window1events = False
    GUISetState(@SW_HIDE, $window1)
    GUIDelete($window1)
EndFunc   ;==>_DestroyWindow1


Func _DestroyWindow2()
    $window2events = False
    GUISetState(@SW_HIDE, $window2)
    GUIDelete($window2)
EndFunc   ;==>_DestroyWindow2


Func _Event_Window1_Close()
    _DestroyWindow1()
EndFunc   ;==>_Event_Window1_Close


Func _Event_Window1_Button1()
    _DestroyWindow1()
    _CreateWindow2()
EndFunc   ;==>_Event_Window1_Button1


Func _Event_Window2_Close()
    _DestroyWindow2()
EndFunc   ;==>_Event_Window2_Close


Func _Event_Window2_Button2()
    _DestroyWindow2()
    _CreateWindow1()
EndFunc   ;==>_Event_Window2_Button2

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Your script was stuck in the second window's While loop so it wasn't looking for any key strokes, I got rid of the While loops inside the functions, and moved it to a common location, so both functions properly exit after they've created the GUI, and sit inside that external While loop waiting for you to hit a key. Because the second function never exits, the script is waiting before it activates the function 1.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Your script was stuck in the second window's While loop so it wasn't looking for any key strokes

OK, I can even confirm this.

But why can Autoit handle looking for keystrokes in the first loop and not in the second?

Actually it get's even stranger when I add loops to the first loop.

It's possible to open the second window when I replace following code

While $window1events
  Sleep(1000)
WEnd

with this code

While $window1events
  Sleep(1000)
  While $window1events
    Sleep(1000)
    While $window1events
      Sleep(1000)
    WEnd
  WEnd
WEnd

This seem to indicate that Autoit can handle nested loops.

Sorry but I'm a bit confused about the internal mechanisms of Autoit.

Link to comment
Share on other sites

Reread the tutorial on using multiple GUIs

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Thank you for your patience BrewManNH.

I read the tutorial again and had to study it before really understanding the content.

Thanks again for putting an emphasis on the tutorial.

Could you tell me if following summary is correct?

When using the OnEventMode we can register controls (like buttons) with OnEventCalls (a function to be executed when the button is pressed)

To do this we use the function GUICtrlSetOnEvent($controlId, "_OnEventExecuteThisFunction")

A normal function can be interrupted by an OnEventCall.

The normal function is paused and the OnEventCall (defined in _OnEventExecuteThisFunction) is executed.

Once the OnEventCall has been executed, the normal function continues it's work.

OnEventCalls can not be interrupted by other OnEventCalls.

The idea is that OnEventCalls are queued by Autoit.

An OnEventCall has to be completely executed before the next OnEventCall is executed.

By consequence, the execution of an OnEventCall will stop controls from interacting with the code executed in the OnEventCall

The previous quote is also applicable on the MessageLoopMode.

In this case, an OnEventCall is a function call which resides in the same environment as the GUIGetMsg() statement.

In following example

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $hButton_1
            _Func_1()
        Case $hButton_2
            _Func_2()
    EndSwitch
WEnd
the environment of the GUIGetMsg() statement starts at the Switch statement and ends at the WEnd statement.

This means we have 2 OnEventCalls:

- _Func_1()

- _Func_2()

Link to comment
Share on other sites

The first quote is an expansion on what I was saying in my post. Once you got into the second function, it wasn't going to execute the onevent button push from the first function, because it's already in an onevent function that hasn't ended yet. You had to let the functions return before going into idle mode by the While loop, otherwise they get stuck like that.

It's a very common mistake that gets made all the time, thinking you need a While loop in each function to keep that GUI running, instead of just using a common one to do it instead.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

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