So I've decided programming ColorBox, my own MsgBox. Fortunately I've found inspiration in Areilius AHK source (https://autohotkey.com/board/topic/9327 ... ul-msgboxs). I've liked his minimalist aproach, but I miss some MsgBox features, specially Timeout and option for until 5 buttons.
I've also needed add a custom name feature for the buttons. Special Icons (exclamation, asterisk, etc.) can be added, but I don't need it.
So, with no frills, my code: (I hope that somebody find it useful!)
Code: Select all
;***********************************************************************************************************************
; ColorBox
; Minimalist modal MsgBox replacement with font settings, background color,
; optional timeout and allowing use until 5 buttons.
; With more than 1 button, return selected button value
; (1-Yes - also true for 2 buttons | 0-No - also false for 2 buttons | 2-Cancel for 3 buttons
; 3-Extra for 4 buttons, 4-Plus for 5 buttons )
;**********************************************************************************************************************
ColorBox(Tit = "Message", Mess="Pause", NBut=1,TOut=0, DefL=1, Text1L:="Yes", Text2L = "No", Text3L="Cancel", Text4L="Extra", Text5L = "Plus"
, FontL="Lucida Console", FontOpt="cBlue w500 s12",WindowColor="", CallerGui="")
; Tit - Windows title
; Mess- Message with 1 or more lines (using 'r'n (CR/LF) inside string to change line)
; NBut - Default 1 button, but accept until 5 buttons.
; TOut - Timeout in seconds, default 0 (No Timeout)
; DefL - Default button number from 1 to NBut buttons. Default 1. <Enter> return default button. 0 no default, in that case <Enter> does nothing
; Text1L - 1nd Button text. Default "OK" with 1 button or "Yes" with 2 buttons or more - Return 1
; Text2L - 2nd Button text. Default "No" - Return 0
; Text3L - 3nd Button text. Default "Cancel" - Return 2
; Text4L - 4nd Button text. Default "Extra" - Return 3
; Text5L - 5nd Button text. Default "Plus" - Return 4
; FontL - Font name (default "Lucida Console")
; FontOpt - Font options, default is blue with weight 500 (more than normal less than bold). One can specify "italic", "bold", "underline", etc.
; Window Color - Default: light silver, can be any color like "black", "blue", etc.
; CallerGui - If that message was started from a caller Modal GUI, it demands to be disabled at the beginning and enabled again at the end.
{
Static ETimeOut ; Variable shared with timeout section
Local RetLoc, HasGui, NInd, MaxBut=5
ETimeOut := false
RetLoc := 1
HasGui := (CallerGui<>"")
Labels := ["Yes","No","Canc","Xtra","Plus"]
if (HasGui)
Gui, %CallerGui%:+Disabled
Gui, ColorBox:Destroy
Gui, ColorBox:Color,%WindowColor%
Gui, ColorBox:Font,%FontOpt%,%FontL%
Gui, ColorBox:Add,Text,,%Mess%
Gui, ColorBox:Font
GuiControlGet,Text, ColorBox:Pos,Static1
if (TOut<>0) ; Prepare for default answer also in timeout event
if (DefL<=1)
RetLoc := DefL=0 ? -1 : 1 ; Return -1 (No default) or 1 (OK)
else
Loop % MaxBut-1 {
NInd := A_Index+1 ; Return 0 (2 buttons and default 2nd Button) or (Button Number-1) if Default 3nd button and so on
if ( DefL=NInd and NBut>=NInd )
RetLoc := ( DefL=2 ? 0 : NInd-1 )
}
if (TOut<>0) ; Prepare for default answer also in timeout event
RetLoc := DefL=0 ? -1 : ( DefL=1 ? 1 : (DefL = 2 and NBut>=2 ? 0 : ( DefL = 3 and NBut=3 ? 2 : DefL ) ) )
Loop % NBut {
if ( (Text1L = "Yes") and (NBut=1) )
Text1L := "OK"
if (A_Index=1) ; TextW: Non-documented variable that stores text width
Gui, ColorBox:Add,Button,% (DefL=1 or NBut=1 ? "Default " : "") . "y+10 w75 gYes xp+" (TextW / 2) - 38 * NBut , %Text1L%
else
Gui, ColorBox:Add,Button,% (DefL=A_Index ? "Default " : "") . "yp+0 w75 g" . Labels[A_Index] . " x+" 10, % Text%A_Index%L
}
Gui, ColorBox:-SysMenu +OwnDialogs +AlwaysOnTop ; Clean and modal window message. No Minimize, maximize, close icon and AHK icon.
Gui, ColorBox:Show,,%Tit%
If (TOut<>0) ; TimeOut in seconds
SetTimer TimeOut, % TOut*1000
Gui, ColorBox:+LastFound ; Last selected window
WinWaitClose ; Wait for the GUI window closes. Make it strictly modal.
if (HasGui)
Gui, %CallerGui%:-Disabled ; Enable caller GUI back
Set TimeOut, Off
return RetLoc
Yes: ; Fisrt button
Gui, ColorBox:Destroy
RetLoc := 1
return
No: ; Second button
Gui, ColorBox:Destroy
RetLoc := 0
return
Canc: ; Third button
Gui, ColorBox:Destroy
RetLoc := 2
return
XTra: ; Fourth button
Gui, ColorBox:Destroy
RetLoc := 3
return
Plus: ; Fifth button
Gui, ColorBox:Destroy
RetLoc := 4
return
TimeOut: ; Timeout section
Gui, ColorBox:Destroy
ETimeOut := True ; Share just static variables with enclosing function
return
}
Code: Select all
ColorBox(,"It's a big mistake") ; Just a normal message with "OK" button
ColorBox(,"Continue?",2,0,0) ; A plain question, the user press YES or NO. No default answer
ColorBox(,"Continue?",2,0,1) ; A plain question, the user press YES or NO. Default answer is "Yes" with <Enter> key
ColorBox(,"Continue?",3,0,0) ; A question with 3 buttons ("Yes", "No, "Cancel") with no default, returns 1 (1nd),0 (2nd) or 2 (3nd)
ColorBox(,"Select",3,0,1,"One","Two","Three") ; A selection with 3 buttons ("One", "Two", "Three"), default 1st button, same returns.
ColorBox(,"Continue?",2,0,1,,,,,,,"cred s10") ; A plain question, the user press YES or NO, font is red with size 10.
ColorBox(,"Continue?",2,0,1,,,,,,,"cwhite bold s10","black") ; A plain question, the user press YES or NO, font is white, bold, size 10 with black background.
ColorBox(,"Continue?",2,0,1,,,,,,,,,"ParentGUI") ; A plain question, the user press YES or NO, standard font and background, called from modal GUI ParentGUI.
ColorBox(,"Select",5,5,1,"One","Two","Three","Four","Five") ; A selection with 5 buttons ("One", "Two", "Three", "Four","Five"), default 1st button,
; return 1 for "One", 0 for "Two", 2 for "Three", 3 for "Four" and 4 for "Five", with timeout of 5 seconds.
Code: Select all
#EscapeChar @
CrazyStat := ColorBox(,"This Custom MsgBox has a blue Lucida Console @r@n"
. "font and light silver background!",3,0,1,"Cool!","Damn it!","I don't mind")
If (CrazyStat=1)
ColorBox(,"Oh Yes")
else if (CrazyStat=0)
ColorBox(,"Don't bother me!")
else if (CrazyStat=2)
ColorBox(,"I want to get out")
Then one press the button "Damn it" and it shows