Jump to content
Sign in to follow this  

Centered Message Box

Recommended Posts


I wanted a custom message box that would be centered on the application window when clicked rather than centered on the screen. I found this MsgBox by photonbuddy ( http://www.autoitscript.com/forum/index.php?showtopic=43832 ) and modified it. I thought I would share:

; Where:
;     $title      =  The title of the message box
;     $text    =  The text to be displayed. Long lines will wrap. The text will also
;                    be centered.
;     $button    =  The text for the buttons. Seperate buttons with the
;                    pipe (|) character. To set a button as the default button,
;                    place the ampersand (&) character before the word. If you
;                    put more than 1 ampersand character, the function will
;                    fail and return -1, plus set @error to 1
;     $hWin    =  The handle of the window the message box is to be centered in.
;     $msgwidth   =  Width of the displayed window. This value will be automatically
;                    increased if the resulting window will not be wide enough to
;                    accommodate the buttons requested. Default is 300
;     $msgheight  =  Height of the displayed window. This is not adjusted. If the
;                    text is too big, it will not display it all. Default is 80
;     $buttonwidth=  The width of the button in pixels.  Default is 80
;     Success:  Returns the button pressed, starting at 1, counting from the LEFT.
;                 Returns 0 is the Message Box was closed via a "CloseGUI" event, i.e. click
;                 the close box or press Escape.
;     Failure:  Returns -1 if Message Box was not created due to more than 1 default button 
;                 being set or other GUI error.  Error is set to 1.
Func _CenteredMsg($title, $text, $button, $hWin, $msgwidth=300,$msgheight=80,$buttonwidth=80,$ntimeout=-1)
    Local $nOldOpt = Opt('Guioneventmode',0)
    Local $buttonarray=StringSplit($button,"|")
    Local $msgButton[$buttonarray[0]+1]
    Local $buttoncount,$defbutton=0, $retvalue, $buttonwork, $buttonxpos, $mmsg
    Local $arTemp=WinGetPos($hWin)
    If StringInStr($button,"&",0,2)<>0 Then;if two buttons are showing focus
        $nOldOpt = Opt('Guioneventmode',$nOldOpt)
        Return -1
    If ($buttonwidth+8)*$buttonarray[0]+8>$msgwidth Then;if button width exceeds window size, resize window
    Local $msggui = GUICreate($title, $msgwidth, $msgheight, ($arTemp[2]-$msgwidth)/2+$arTemp[0]-3,($arTemp[3]-$msgheight)/2+$arTemp[1]-16, _
BitOr(0x80880000,0x00C00000)); $WS_POPUPWINDOW & $WS_CAPTION
    If $msggui=0 Then 
        $nOldOpt = Opt('Guioneventmode',$nOldOpt)
        Return -1
    Local $hDummy=GUICtrlCreateLabel($text, 8, 8, $msgwidth-16, $msgheight-40)
    For $buttoncount=0 To $buttonarray[0]-1
        If StringLeft($buttonwork,1)="&" Then
        $msgbutton[$buttoncount] = GUICtrlCreateButton($buttonarray[$buttoncount+1], $buttonxpos+($buttoncount*$buttonwidth)+($buttoncount*$buttonxpos*2), _
$msgheight-32, $buttonwidth, 24,$defbutton)

    Dim $timeout
    If $ntimeout <> -1 Then $timeout = TimerInit()
    While 1
        $mmsg = GUIGetMsg()
        If $mmsg > 2 Then
            $retvalue = $mmsg-$hDummy;ctrl number of button minus the ctrl number of the label will result in button 1 = 1, button 2 = 2
            Case $mmsg = -3;$GUI_EVENT_CLOSE = -3
                $nOldOpt = Opt('Guioneventmode',$nOldOpt)
                Return 0
            Case Else
                If TimerDiff($timeout)/1000 >= $ntimeout AND $ntimeout <> -1 Then
                    $nOldOpt = Opt('Guioneventmode',$nOldOpt)
                    Return 0
        If $retvalue <> '' Then ExitLoop
    $nOldOpt = Opt('Guioneventmode',$nOldOpt)
    Return $retvalue
EndFunc;==> _My_DisplayMsg()

Here is a sample script using the function:

$hGUI=GUICreate("My Test",300,600,200,200)


GUISetState(@SW_SHOW, $hGUI)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case -3;$GUI_EVENT_CLOSE = -3
        Case $hButton
            $nValue=_CenteredMsg("My Test Message","This is Just a Test","&1|2|3|4|5",$hGUI,400,100,50)

I hope someone finds this useful.


Edited by YellowLab

You can't see a rainbow without first experiencing the rain.

Share this post

Link to post
Share on other sites

Nice! It should also have a flag so that it will set the parent of the GUI to the HWND. :P

Share this post

Link to post
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

Sign in to follow this  


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.