Modal MsgBox Styles: Difference between revisions

From AutoIt Wiki
Jump to navigation Jump to search
m (+Category:Tutorials)
m (Replaced magic numbers)
 
Line 6: Line 6:
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:
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:
<syntaxhighlight lang="autoit">
<syntaxhighlight lang="autoit">
#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>
#include <GUIConstantsEx.au3>
$hGUI = GUICreate("Test", 500, 500)
 
GUISetState()
$hGUI = GUICreate("Test", 500, 500)
GUISetState()
MsgBox(0, "Blocking", "Press me to continue")
 
MsgBox($MB_APPLMODAL, "Blocking", "Press me to continue")
While 1
 
While 1
     Switch GUIGetMsg()
     Switch GUIGetMsg()
         Case $GUI_EVENT_CLOSE
         Case $GUI_EVENT_CLOSE
             Exit
             Exit
     EndSwitch
     EndSwitch
WEnd
WEnd
</syntaxhighlight>
</syntaxhighlight>


Line 24: Line 25:
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.
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.
<syntaxhighlight lang="autoit">
<syntaxhighlight lang="autoit">
#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>
#include <GUIConstantsEx.au3>
$hGUI = GUICreate("Test", 500, 500)
 
GUISetState()
$hGUI = GUICreate("Test", 500, 500)
GUISetState()
MsgBox(4096, "Blocking", "Press me to continue")
 
MsgBox($MB_SYSTEMMODAL, "Blocking", "Press me to continue")
While 1
 
    Switch GUIGetMsg()
While 1
        Case $GUI_EVENT_CLOSE
    Switch GUIGetMsg()
            Exit
        Case $GUI_EVENT_CLOSE
    EndSwitch
            Exit
WEnd
    EndSwitch
WEnd
  </syntaxhighlight>
  </syntaxhighlight>


Line 42: Line 44:
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!
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!
<syntaxhighlight lang="autoit">
<syntaxhighlight lang="autoit">
#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>
#include <GUIConstantsEx.au3>
$hGUI = GUICreate("Test", 500, 500)
 
GUISetState()
$hGUI = GUICreate("Test", 500, 500)
GUISetState()
MsgBox(8192, "Blocking", "Press me to continue")
 
MsgBox($MB_TASKMODAL, "Blocking", "Press me to continue")
While 1
 
    Switch GUIGetMsg()
While 1
        Case $GUI_EVENT_CLOSE
    Switch GUIGetMsg()
            Exit
        Case $GUI_EVENT_CLOSE
    EndSwitch
            Exit
WEnd
    EndSwitch
WEnd
</syntaxhighlight>
</syntaxhighlight>
Now all you have to do is choose which type best suits your needs!
Now all you have to do is choose which type best suits your needs!
Line 61: Line 64:
Another way to prevent the GUI from being actioned while the ''MsgBox'' is present is to set the "''parent''" parameter like this:
Another way to prevent the GUI from being actioned while the ''MsgBox'' is present is to set the "''parent''" parameter like this:
<syntaxhighlight lang="autoit">
<syntaxhighlight lang="autoit">
#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>
#include <GUIConstantsEx.au3>
$hGUI = GUICreate("Test", 500, 500)
 
$hGUI = GUICreate("Test", 500, 500)
GUISetState()
 
GUISetState()
MsgBox(0, "", "", 0, $hGUI)
 
MsgBox($MB_APPLMODAL, "Blocking", "Press me to continue", 0, $hGUI)
While 1
 
    Switch GUIGetMsg()
While 1
        Case $GUI_EVENT_CLOSE
    Switch GUIGetMsg()
            Exit
        Case $GUI_EVENT_CLOSE
    EndSwitch
            Exit
WEnd
    EndSwitch
WEnd
</syntaxhighlight>
</syntaxhighlight>

Latest revision as of 09:53, 25 March 2017

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 <MsgBoxConstants.au3>
#include <GUIConstantsEx.au3>

$hGUI = GUICreate("Test", 500, 500)
GUISetState()

MsgBox($MB_APPLMODAL, "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 <MsgBoxConstants.au3>
#include <GUIConstantsEx.au3>

$hGUI = GUICreate("Test", 500, 500)
GUISetState()

MsgBox($MB_SYSTEMMODAL, "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 <MsgBoxConstants.au3>
#include <GUIConstantsEx.au3>

$hGUI = GUICreate("Test", 500, 500)
GUISetState()

MsgBox($MB_TASKMODAL, "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 <MsgBoxConstants.au3>
#include <GUIConstantsEx.au3>

$hGUI = GUICreate("Test", 500, 500)

GUISetState()

MsgBox($MB_APPLMODAL, "Blocking", "Press me to continue", 0, $hGUI)

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd