Jump to content

My (simple) custom message box


photonbuddy
 Share

Recommended Posts

I have decided that I need myself a simple, yet elegant (IMHO anyway!) message box.

Whilst it doesn't have all the icons and options of the standard message box, it does what I want it to do.

This will allow up to 5 buttons, allows for a default button, and simply returns the number of the button clicked.

If you have suggestions on how to improve, or streamline, this function, please let me know.

Example usage:

#include <GuiConstants.au3>
#NoTrayIcon

$msgreturn=_DisplayMsg("This is a test to see if it works!!","Cancel|&No|Yes|No Idea",400,100)
If @error=1 then
    [Do something]
endif
Exit

And here is the actual function

;===========================================================================================
;
; Usage: _DisplayMsg($text,$button,[msgwidth,[msgheight,[msgxpos,[msgypos]]]])
;
; Where:
;     $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
;     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 250
;     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
;     msgxpos    =  Where you want the window positioned horizontally. Default is centered.
;     msgypos    =  Where you want the window positioned vertically. Default is centered.
;
;     Success:  Returns the button pressed, starting at 1, counting from the LEFT.
;     Failure:  Returns 0 if more than 1 default button is set, or an error with the window
;                 occurs.
;
;===========================================================================================


Func _DisplayMsg($text, $button,$msgwidth=300,$msgheight=80,$msgxpos=-1,$msgypos=-1)
    Local $buttonarray,$msgbutton[5]=[1,1,1,1,1],$buttoncount,$defbutton=0
    If StringInStr($button,"&",0,2)<>0 Then
        SetError(1)
        Return 0
    EndIf
    $buttonarray=StringSplit($button,"|")
    If $buttonarray[0]>5 Then $buttonarray[0]=5
    If 88*$buttonarray[0]+8>$msgwidth Then
        $msgwidth=88*$buttonarray[0]+8
    EndIf
    $msggui = GUICreate("", $msgwidth, $msgheight, $msgxpos, $msgypos, $WS_popup + $WS_DLGframe, $WS_EX_TOOLWINDOW + $WS_EX_TOPMOST)
    If $msggui=0 Then Return 0
    GUICtrlCreateLabel($text, 8, 8, $msgwidth-16, $msgheight-40, $SS_CENTER)
    $buttonxpos=(($msgwidth/$buttonarray[0])-80)/2
    For $buttoncount=0 To $buttonarray[0]-1
        $buttonwork=$buttonarray[$buttoncount+1]
        If StringLeft($buttonwork,1)="&" Then
            $defbutton=$BS_DEFPUSHBUTTON
            $buttonarray[$buttoncount+1]="[ " & StringTrimLeft($buttonwork,1) & " ]"
        EndIf
        $msgbutton[$buttoncount] = GUICtrlCreateButton($buttonarray[$buttoncount+1], $buttonxpos+($buttoncount*80)+($buttoncount*$buttonxpos*2), $msgheight-32, 80, 24,$defbutton)
        $defbutton=0
    Next
    GUISetState()
    While 1
        $mmsg = GUIGetMsg()
        Select
            Case $mmsg = $msgbutton[0]
                GUIDelete($msggui)
                Return 1
            Case $mmsg = $msgbutton[1]
                GUIDelete($msggui)
                Return 2
            Case $mmsg = $msgbutton[2]
                GUIDelete($msggui)
                Return 3
            Case $mmsg = $msgbutton[3]
                GUIDelete($msggui)
                Return 4
            Case $mmsg = $msgbutton[4]
                GUIDelete($msggui)
                Return 5
        EndSelect
    WEnd
EndFunc
Link to comment
Share on other sites

rly pointless there are so many better alternatives

I'm sure there are, but this does what I want, which is what's important to me.

If everyone stopped making scripts just because it has already been done, these forums would be pretty quiet.

Edited by photonbuddy
Link to comment
Share on other sites

I'm sure there are, but this does what I want, which is what's important to me.

If everyone stopped making scripts just because it has already been done, these forums would be pretty quiet.

i agree, anyways nice job..

i modified a bit..

#include <GuiConstants.au3>
$msgreturn=_DisplayMsg('custommsg',"This is a test to see if it works!!","Cancel|&No|Yes|No Idea",400,100,-1,-1,3,-1,-1)
ConsoleWrite($msgreturn)
Exit

Func _DisplayMsg($title, $text, $button,$msgwidth=300,$msgheight=80,$msgxpos=-1,$msgypos=-1,$ntimeout=-1,$style=-1,$exstyle=-1)
    $nOldOpt = Opt('Guioneventmode',0)
    Local $buttonarray,$msgbutton[5]=[1,1,1,1,1],$buttoncount,$defbutton=0, $retvalue
    If $exstyle = -1  Then $style = -1; $WS_popup + $WS_DLGframe (dont like this style)
    If $exstyle = -1  Then $exstyle = -1; $WS_EX_TOOLWINDOW + $WS_EX_TOPMOST
    If StringInStr($button,"&",0,2)<>0 Then
        SetError(1)
        Return 0
    EndIf
    $buttonarray=StringSplit($button,"|")
    If $buttonarray[0]>5 Then $buttonarray[0]=5
    If 88*$buttonarray[0]+8>$msgwidth Then
        $msgwidth=88*$buttonarray[0]+8
    EndIf
    $msggui = GUICreate($title, $msgwidth, $msgheight, $msgxpos, $msgypos, $style, $exstyle)
    If $msggui=0 Then Return 0
    GUICtrlCreateLabel($text, 8, 8, $msgwidth-16, $msgheight-40, $SS_CENTER)
    $buttonxpos=(($msgwidth/$buttonarray[0])-80)/2
    For $buttoncount=0 To $buttonarray[0]-1
        $buttonwork=$buttonarray[$buttoncount+1]
        If StringLeft($buttonwork,1)="&" Then
            $defbutton=$BS_DEFPUSHBUTTON
            $buttonarray[$buttoncount+1]="[ " & StringTrimLeft($buttonwork,1) & " ]"
        EndIf
        $msgbutton[$buttoncount] = GUICtrlCreateButton($buttonarray[$buttoncount+1], $buttonxpos+($buttoncount*80)+($buttoncount*$buttonxpos*2), $msgheight-32, 80, 24,$defbutton)
        $defbutton=0
    Next
    GUISetState()
    If $ntimeout <> -1 Then $timeout = TimerInit()
    While 1
        $mmsg = GUIGetMsg()
        Select
            Case $mmsg = $GUI_EVENT_CLOSE
                $retvalue = 0
            Case $mmsg = $msgbutton[0]
                $retvalue = 1
            Case $mmsg = $msgbutton[1]
                $retvalue = 2
            Case $mmsg = $msgbutton[2]
                $retvalue = 3
            Case $mmsg = $msgbutton[3]
                $retvalue = 4
            Case $mmsg = $msgbutton[4]
                $retvalue = 5
            Case Else
                If TimerDiff($timeout)/1000 >= $ntimeout Then 
                    $retvalue = -1
                EndIf
        EndSelect
        If $retvalue <> '' Then ExitLoop
    WEnd
    $nOldOpt = Opt('Guioneventmode',$nOldOpt)
    Return $retvalue
EndFunc
Don't bother, It's inside your monitor!------GUISetOnEvent should behave more like HotKeySet()
Link to comment
Share on other sites

i agree, anyways nice job..

i modified a bit..

I was thinking of doing the styles thing like you have done, but then I would have to remember what all the styles are :shocked:

In my experience, once you have something like this function, you'll put your preferred styles in and use them almost exclusivly.

The time-out function could be handy, but I would probably only use that to select the default button (if one has been chosen).

I've also made the button field optional, with the default set to "&Ok".

I was also going to have it so you could pass-in a different button-width as well.

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