Modal MsgBox Styles

From AutoIt Wiki
Revision as of 14:32, 14 November 2012 by IEvKI3gv9Wrkd41u (talk | contribs) (+Category:Tutorials)
Jump to navigation Jump to search

Modal MsgBox Styles

MsgBox is a blocking function which means that a script pauses until a button on the MsgBox is pressed at which point the function terminates with a return value which you can use in a decision structure of some kind. Your main GUI appears unresponsive until the MsgBox is cleared. However, things are not quite that simple because the MsgBox can be set as Application, System or Task Modal. Although the values needed to set these styles are given in the Helpfile, it does not actually explain the difference between them.

Application

This is the default setting. In this case your GUI will react to mouse clicks and will even retake focus from the MsgBox, but will not act on the clicks until the MsgBox is cleared. Try closing the main GUI while the MsgBox is displayed - it will not close unless you clear the Msgbox first:

 #include <GUIConstantsEx.au3>
 
 $hGUI = GUICreate("Test", 500, 500)
 GUISetState()
 
 MsgBox(0, "Blocking", "Press me to continue")
 
 While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
 WEnd

System Modal

Now the MsgBox remains in view even if the main GUI regains focus although the main GUI appears inactive the GUI. However if the main GUI is closed this will again occur as soon as the MsgBox is cleared.

 #include <GUIConstantsEx.au3>
 
 $hGUI = GUICreate("Test", 500, 500)
 GUISetState()
 
 MsgBox(4096, "Blocking", "Press me to continue")
 
 While 1
     Switch GUIGetMsg()
         Case $GUI_EVENT_CLOSE
             Exit
     EndSwitch
 WEnd

Task Modal

Now we get to the real deal - you cannot action anything on the main GUI until the MsgBox is cleared. So you cannot close the main GUI - or do anything else with it at all!

 #include <GUIConstantsEx.au3>
 
 $hGUI = GUICreate("Test", 500, 500)
 GUISetState()
 
 MsgBox(8192, "Blocking", "Press me to continue")
 
 While 1
     Switch GUIGetMsg()
         Case $GUI_EVENT_CLOSE
             Exit
     EndSwitch
 WEnd

Now all you have to do is choose which type best suits your needs!

An Added Extra

Another way to prevent the GUI from being actioned while the MsgBox is present is to set the "parent" parameter like this:

 #include <GUIConstantsEx.au3>
 
 $hGUI = GUICreate("Test", 500, 500)
 
 GUISetState()
 
 MsgBox(0, "", "", 0, $hGUI)
 
 While 1
     Switch GUIGetMsg()
         Case $GUI_EVENT_CLOSE
             Exit
     EndSwitch
 WEnd