Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint

GUI Event Modes

How do you make something happen when a button is clicked on your GUI? There are two different event modes you can use to cause a reaction in your script when a button is pressed:

  1. MessageLoop (default). This is the default mode; it causes the GUI only to respond to events if the GuiGetMsg() function is called. It is wise to call this function many times per second in a loop; otherwise, you will have a slow or unresponsive GUI. The following loop continuously calls the GuiGetMsg() function until one of the two buttons are pressed or the user attempts to close the GUI:

    While 1
            $guimsg = GuiGetMsg()
            Select
                Case
    						$guimsg = $GUI_EVENT_CLOSE
                    Exit
    						; closes the GUI
    						Case
    						$guimsg = $button1
                    button1(); runs button1 function
    						Case
    						$guimsg = $button2
                    button2(); runs button2 function
    						EndSelect
        WEnd

    Notice that the loop is infinite (noted by While 1; you will learn more about this in the Conditional and Loop Statements section later in the Short Cut). The $guimsg variable will be continuously updated by the GuiGetMsg() function until one of the three cases is satisfied. Choosing Button1 or Button2 causes a respective function to run. Closing the GUI causes the program to exit. The GuiGetMsg() Method incorporates the GuiGetMsg() loop into a fully functional script.

    Example. The GuiGetMsg() Method

    ; Includes the GuiConstants (required for GUI function usage)
    							#include <GuiConstants.au3>
    
    ; GUI Creation
    							GuiCreate("Menu", 400, 150)
    							; Button1
    							$button1 = GUICtrlCreateButton("Button1", 100, 20, 200, 30)
    							; Button2
    							$button2 = GUICtrlCreateButton("Button2", 100, 70, 200, 30)
    							; Shows the GUI after creation
    							GUISetState(@SW_SHOW)
    
    Func Function1()
    							; Hides the GUI while the function is running
    							GUISetState(@SW_HIDE)
    							; ================
        ; The script you would like to perform for Button1 goes here
        ; ================
    							EndFunc
    
    Func Function2()
    							; Hides the GUI while the function is running
    							GUISetState(@SW_HIDE)
    							; ================
        ; The script you would like to perform for Button2 goes here
        ; ================
    							EndFunc
    
    While 1
        $guimsg = GuiGetMsg()
        Select
            Case
    							$guimsg = $GUI_EVENT_CLOSE
                Exit
    							; closes the GUI
    							Case
    							$guimsg = $button1
                function1(); runs Button1 function
    							Case
    							$guimsg = $button2
                function2(); runs Button2 function
    							EndSelect
           WEnd
    
    					  

  2. OnEvent. Denoted by AutoItSetOption('GUIOnEventMode', 1), OnEvent mode basically pauses the script when something is chosen on the GUI and runs a function. Once the function is completed, the main script resumes. The GUIOnEventMode Method shows how to properly use OnEvent mode in a script similar to The GuiGetMsg() Method.

    Example. The GUIOnEventMode Method

    ; Includes the GuiConstants (required for GUI function usage)
    							#include <GuiConstants.au3>
    
    ; Changes to OnEvent mode
    							Opt('GUIOnEventMode', 1)
    							; GUI Creation
    							GuiCreate("Menu", 400, 150)
    							; Runs the GUIExit() function if the GUI is closed
    							GUISetOnEvent($GUI_EVENT_CLOSE, 'GUIExit')
    							; Button1
    							GUICtrlCreateButton("Button1", 100, 20, 200, 30)
    GUICtrlSetOnEvent(−1, 'Function1')
    							; run Function1 when pressed
    
    ; Button2
    							GUICtrlCreateButton("Button2", 100, 70, 200, 30)
    GUICtrlSetOnEvent(−1, 'Function2')
    							; run Function2 when pressed
    							Func Function1()
    							; Hides the GUI while the function is running
    							GUISetState(@SW_HIDE)
    							; ================
        ; The script you would like to perform for Button1 goes here
        ; ================
    							EndFunc
    
    Func Function2()
    							; Hides the GUI while the function is running
    							GUISetState(@SW_HIDE)
    							; ================
        ; The script you would like to perform for Button2 goes here
        ; ================
    							EndFunc
    							; Shows the GUI after the function completes
    							GUISetState(@SW_SHOW)
    							; Idles the script in an infinite loop - this MUST be included when using
    ; OnEvent mode
    							While 1
        Sleep(500)
    WEnd
    							; This function makes the script exit when the GUI is closed
    							Func GUIExit()
        Exit
    EndFunc
    
    					  


  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint