Sign in to follow this  
Followers 0
FreeRider

MsgBox... (customizable)

1 post in this topic

Hi All,

Here is a modified and renamed version of a function, found on this forum, which allows to customize buttons text.

This can be used when you do not want to have the standard text of Windows (Yes/No/Cancel) or when you need to provide message in a other language than the one

of the installed Windows.

Here is the function code...

#cs===============================================================================
 Function Name: _MsgBoxCustom
 Description:     Allow to modify standard Msgbox text for buttons
                   - Automatically sets the number of buttons flag
Parameter(s):       $iFlag = Standard flag (see autoit help) except buttons flag as this flag is function of the buttons nbr.
                    $STitle = Title of the Message box
                    $SText = Text of the Message box
                    $sButText = Text of the buttons (if more than one pipe ["|"] must be used as separator... ex : "Ja|Nein|Vielleicht")
 Requirement(s):    None
 Return Value(s):   An array as follow
                    - $Array[0] = Nbr of buttons
                    - $Array[1] = Text for Button 1
                    - $Array[2] = Text for Button 2 ===> Only if a 2 buttons box is required
                    - $Array[3] = Text for Button 3 ===> Only if a 3 buttons box is required
                    - $Array[n] = Where "n" is the last element of the array (index is not always the same, it's buttons depending).
                                  Return code of the MsgBox Autoit function 
                    
 Author(s):         Don't remember (this function is based on a function found on the forum but I can't find the topic again and don't remember the original name)
                    If the author reads this topic, he just can leave a message and I'll include his name...
                    Modified by : Christophe Savard (France)
                    Modifcations :
                        - $ButText replace the 3 parameter for Button 1 to 3
                        - Test and "clean", if necessary, the $ButText parameter (eliminate leading/trailing and doubles white space)
                        handling several errors and diplaying a formated message instead or only returning an error code.
                        - Returns an array with extendend informations instead of the MsgBox function return code only.
#ce===============================================================================
Func _MsgBoxCustom($iFlag, $sTitle, $sText, $sButText, $iMBTimeOut = 0, $xMBpos = "", $yMBpos = "")
    If Not IsNumber ($iFlag) then
        Seterror (1 )
        MsgBox(0,"Erreur","Error : " & @error & @CRLF & "Flag is missing.")
        Return -1
    ElseIf Not IsNumber ($iMBTimeOut) then
        Seterror (2 )
        MsgBox(0,"Erreur","Error : " & @error & @CRLF & "Timout must be a number.")
        Return -1
    ElseIf $xMBpos <> "" and IsNumber ($xMBpos) = 0 then
        Seterror (3 )
        MsgBox(0,"Erreur","Error : " & @error & @CRLF & "Positioning variable " & '"' & "x" & '"' & " error.")
        Return -1
    ElseIf $yMBpos <> "" and IsNumber ($yMBpos) = 0 then
        Seterror (4 )
        MsgBox(0,"Erreur","Error : " & @error & @CRLF & "Positioning variable " & '"' & "y" & '"' & " error.")
        Return -1
    ElseIf $sButText = "" Then
        SetError (5)
        MsgBox(0,"Erreur","Error : " & @error & @CRLF & "A message text is required.")
        Return -1
    Endif
    
    $sButText = StringStripWS($sButText,7); removes leading/Trailing and doubles spaces between words
    If StringInStr($sButText,'||',0,1) Then
        SetError (6)
        MsgBox(0,"Erreur","Error : " & @error & @CRLF & "Format separator error.")
        Return -1
    ElseIf StringRight($sButText,1)="|" Or StringLeft($sButText,1) = "|" Then
        SetError (7)
        MsgBox(0,"Erreur","Error : " & @error & @CRLF & "No Leading/trailing separator is allowed.")
        Return -1
    EndIf
        
    $sButton = StringSplit($sButText,"|",1); Splits the $sButton content to determine the required number of buttons and the text for the each one
    If $sButton[0]=1 Then; Defines the button flag depending of the nbr of buttons => Therefore possible return codes are limited to 1, 6, 7 and 2
        $ButFlag = 0; Flag for 1 button (ret code 1)
    ElseIf $sButton[0]=2 Then
        $ButFlag = 4; Flag for 2 buttons (Ret code 6 or 7)
    Else
        $ButFlag = 3; Flag for 3 buttons (Ret code 6 or 7 and 2)
    EndIf
    
    #region external script 
    Local $MBFile = FileOpen(@TempDir & '\MiscMMB.txt', 2)
    Local $MBLine1 = 'Opt("TrayIconHide",1)'
    Local $MBLine2 = 'Opt("WinWaitDelay", 0)'
    Local $MBLine3 = 'WinWait("' & $sTitle & '")'
    Local $MBLine4 = 'ControlSetText("' & $sTitle & '", "", "Button1", "' & $sButton[1] & '")'
    
    If $sButton[0] = 2 Then
        Local $MBLine5 = 'ControlSetText("' & $sTitle & '", "", "Button2", "' & $sButton[2] & '")'
    ElseIf $sButton[0] = 3 Then
        Local $MBLine5 = 'ControlSetText("' & $sTitle & '", "", "Button2", "' & $sButton[2] & '")'
        Local $MBLine6 = 'ControlSetText("' & $sTitle & '", "", "Button3", "' & $sButton[3] & '")'
    EndIf
    Local $MBline7 = 'WinMove("' & $sTitle & '", ""' & ', ' & $xMBpos & ', ' & $yMBpos & ')'
    Local $MBline8 = '$pos = WingetPos("' & $sTitle & '", "")'
    Local $MBline9 = 'WinMove("' & $sTitle & '", ""' & ', ' & $xMBpos & ',$pos[1])'
    Local $MBline10 ='WinMove("' & $sTitle & '", ""' & ', $pos[0], ' & $yMBpos & ')'
    
    If $sButton[0] = 1 Then; 1 Button
        FileWrite(@TempDir & '\MiscMMB.txt',$MBLine1 & @CRLF & $MBLine2 & @CRLF & $MBLine3 & @CRLF & $MBLine4)
    ElseIf $sButton[0] = 2 Then; 2 buttons
        FileWrite(@TempDir & '\MiscMMB.txt',$MBLine1 & @CRLF & $MBLine2 & @CRLF & $MBLine3 & @CRLF & $MBLine4 & @CRLF & $MBLine5)
    ElseIf $sButton[0] = 3 Then; 3 buttons
        FileWrite(@TempDir & '\MiscMMB.txt',$MBLine1 & @CRLF & $MBLine2 & @CRLF & $MBLine3 & @CRLF & $MBLine4 & @CRLF & $MBLine5 & @CRLF & $MBLine6)
    EndIf
    
    If $xMBpos <> "" and $yMBpos <> "" then; Only if the custom MsgBox is not at the same position than to original
        FileWriteLine(@TempDir & '\MiscMMB.txt', @crlf & $MBLine7)
    ElseIf $xMBpos <> "" and $yMBpos = "" then
        FileWriteLine (@TempDir & '\MiscMMB.txt', @crlf & $MBLine8)
        FileWriteLine (@TempDir & '\MiscMMB.txt', @crlf & $MBLine9)
    Elseif $xMBpos = "" and $yMBpos <> "" then
        FileWriteLine (@TempDir & '\MiscMMB.txt', @crlf & $MBLine8)
        FileWriteLine (@TempDir & '\MiscMMB.txt', @crlf & $MBLine10)
    Endif
    #endregion external script 
    
    $MBPID1 = Run(@AutoItExe & ' /AutoIt3ExecuteScript ' & EnvGet('TEMP') & '\MiscMMB.txt'); Run the script which will wait for the MsgBox to handle
    $MBBox = MsgBox(262144 + $iFlag + $ButFlag, $sTitle, $sText, $iMBTimeOut); Run the native MsgBox Autoit function
    FileClose($MBFile)
    Do
        FileDelete(@TempDir & '\MiscMMB.txt')
    Until Not FileExists(@TempDir & '\MiscMMB.txt')
    
    ReDim $sButton [UBound($sButton)+1]; Redim the $Sbutton to host the return code of the Msgbox Function
    $sButton [UBound($sButton)-1] = $MBBox; Adds the return code from Msgbox autoit function to the array to return
    Return $sButton ; Returns
EndFunc;Func _MsgBoxCustom

Here is a sample code ... which works fine. I even made a test with special patterns in the button names such "[Continue]" or "*** Continue ***" instead of "Continue" and it also works...

$ButText="Continuer|Abandonner|Re-essayer"
$MsgBoxValue = _MsgBoxCustom(64+4096,"Titre de la boite de message...","Ceci est le texte de la boite de message personalisée !",$ButText)
If @error Then;.... Do what you want here

If $MsgBoxValue[UBound($MsgBoxValue)-1] = 1 Or $MsgBoxValue[UBound($MsgBoxValue)-1] = 6 Then
    MsgBox(0, 'CLick', 'Vous avez choisi ' & '"' & $MsgBoxValue[1] & '"')
ElseIf $MsgBoxValue[UBound($MsgBoxValue)-1] = 7 Then
    MsgBox(0, 'CLick', 'Vous avez choisi ' & '"' & $MsgBoxValue[2] & '"')
ElseIf $MsgBoxValue[UBound($MsgBoxValue)-1] = 2 Then
    MsgBox(0, 'CLick', 'Vous avez choisi ' & '"' & $MsgBoxValue[3] & '"')
EndIf

Any suggestion or improvement proposal is welcome... Enjoy scripting.

FreeRider


FreeRiderHonour & Fidelity

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  
Followers 0