Custom Minimalist MsgBox

Post your working scripts, libraries and tools for AHK v1.1 and older
paulobuchsbaum
Posts: 13
Joined: 27 Feb 2016, 22:14

Custom Minimalist MsgBox

10 Apr 2016, 18:27

MsgBox is a standard function in Autohotkey, that uses default windows settings. It has very small font size, no font selection and no choice for colors and buttons names.

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
}
Some examples:

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.
One example with screenshots.

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")
Image

Then one press the button "Damn it" and it shows

Image
Last edited by paulobuchsbaum on 29 Jun 2016, 23:00, edited 18 times in total.
carno
Posts: 265
Joined: 20 Jun 2014, 16:48

Re: Custom Minimalist MsgBox

11 Apr 2016, 07:05

Could you offer a few real-world examples?
User avatar
boiler
Posts: 16952
Joined: 21 Dec 2014, 02:44

Re: Custom Minimalist MsgBox

11 Apr 2016, 08:09

It's really easy to use. Try making some yourself. For example:
ColorBox("My ColorBox", "Hello world")
then just experiment from there.

This could come in handy. Thanks for sharing.
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Custom Minimalist MsgBox

11 Apr 2016, 08:45

Code above wrote:With more than 1 button, return selected button number (1, 2 or 3)
In fact the return values are:
[*] 1 for first button
[*] 0 for second button
[*] 2 for third button

Thank you for this function. Perhaps you could match the description with the code?
Otherwise, very useful.
Guest

Re: Custom Minimalist MsgBox

11 Apr 2016, 11:42

Nice :) Thanks for sharing.

Maybe add another parameter where you can specify the gui to disable to make it modal ;)
Then just add "Gui %guitodisable%:+Disabled" at the beginning and a few "Gui %guitodisable%:-Disabled" where needed (#5 in current script).
Saves you time to make it modal for each script.

Also, I renamed "Gui 66" to "Gui Colorbox" to avoid conflicts.

As said before the button order is switched but that's easy enough to fix.

Icons would be a nice addition indeed and maybe a custom non-system font?
[Class] CustomFont - https://autohotkey.com/boards/viewtopic.php?t=813
paulobuchsbaum
Posts: 13
Joined: 27 Feb 2016, 22:14

Re: Custom Minimalist MsgBox

12 Apr 2016, 23:21

Guest wrote:Nice :) Thanks for sharing.

Maybe add another parameter where you can specify the gui to disable to make it modal ;)
Then just add "Gui %guitodisable%:+Disabled" at the beginning and a few "Gui %guitodisable%:-Disabled" where needed (#5 in current script).
Saves you time to make it modal for each script.

Also, I renamed "Gui 66" to "Gui Colorbox" to avoid conflicts.

As said before the button order is switched but that's easy enough to fix.

Icons would be a nice addition indeed and maybe a custom non-system font?
[Class] CustomFont - https://autohotkey.com/boards/viewtopic.php?t=813
Thank for your words. I've replaced "66" with "ColorBox".
The Disable and Enable caller GUI is also a very insteresting feature to add. I've did it. Thanks for sharing!
The icon and the custom font I prefer skip for now in order to keep a minimalist aproach.
Last edited by paulobuchsbaum on 12 Apr 2016, 23:34, edited 1 time in total.
paulobuchsbaum
Posts: 13
Joined: 27 Feb 2016, 22:14

Re: Custom Minimalist MsgBox

12 Apr 2016, 23:22

wolf_II wrote:
Code above wrote:With more than 1 button, return selected button number (1, 2 or 3)
In fact the return values are:
[*] 1 for first button
[*] 0 for second button
[*] 2 for third button

Thank you for this function. Perhaps you could match the description with the code?
Otherwise, very useful.
Thanks, I've changed it!
paulobuchsbaum
Posts: 13
Joined: 27 Feb 2016, 22:14

Re: Custom Minimalist MsgBox

12 Apr 2016, 23:38

boiler wrote:It's really easy to use. Try making some yourself. For example:
ColorBox("My ColorBox", "Hello world")
then just experiment from there.

This could come in handy. Thanks for sharing.
Thank you. I was using in my own code. I will add some examples in the post.
SpecialGuest

Re: Custom Minimalist MsgBox

12 Apr 2016, 23:54

paulobuchsbaum wrote: Thank for your words. I've replaced "66" with "ColorBox".
The Disable and Enable caller GUI is also a very insteresting feature to add. I've did it. Thanks for sharing!
The icon and the custom font I prefer skip for now in order to keep a minimalist aproach.
You're very welcome 8-)
paulobuchsbaum
Posts: 13
Joined: 27 Feb 2016, 22:14

Re: Custom Minimalist MsgBox

13 Apr 2016, 09:29

carno wrote:Could you offer a few real-world examples?
I've added some examples.
carno
Posts: 265
Joined: 20 Jun 2014, 16:48

Re: Custom Minimalist MsgBox

13 Apr 2016, 09:33

paulobuchsbaum wrote:
carno wrote:Could you offer a few real-world examples?
I've added some examples.
Thanks, I noticed you have also updated the original source script.
User avatar
Gio
Posts: 1247
Joined: 30 Sep 2013, 10:54
Location: Brazil

Re: Custom Minimalist MsgBox

13 Apr 2016, 10:18

Very interesting. Thanks for sharing.

Also, i got an error when i first executed it (target label "gFazSim" does not exist).

Easily fixed though.
guest3456
Posts: 3463
Joined: 09 Oct 2013, 10:31

Re: Custom Minimalist MsgBox

13 Apr 2016, 11:31

screenshot?

paulobuchsbaum
Posts: 13
Joined: 27 Feb 2016, 22:14

Re: Custom Minimalist MsgBox

13 Apr 2016, 14:24

Gio wrote:Very interesting. Thanks for sharing.

Also, i got an error when i first executed it (target label "gFazSim" does not exist).

Easily fixed though.
I've already fix it. Thanks. I'm Brazilian and "FazSim" is in "Do Yes" in English.
User avatar
Gio
Posts: 1247
Joined: 30 Sep 2013, 10:54
Location: Brazil

Re: Custom Minimalist MsgBox

13 Apr 2016, 14:44

paulobuchsbaum wrote:I've already fix it. Thanks. I'm Brazilian and "FazSim" is in "Do Yes" in English.
I thought so when i saw your name (and the name of the function). I'm from Brazil too :thumbup:
paulobuchsbaum
Posts: 13
Joined: 27 Feb 2016, 22:14

Re: Custom Minimalist MsgBox

13 Apr 2016, 14:59

Gio wrote:
paulobuchsbaum wrote:I've already fix it. Thanks. I'm Brazilian and "FazSim" is in "Do Yes" in English.
I thought so when i saw your name (and the name of the function). I'm from Brazil too :thumbup:
It's funny.

Recalls me that joke that one Portuguese in Germany was advised that it's just speak Portuguese slow that the Germans would understand it.
So the Portuguese has got a cab and start to speak "Po ... de ..... ria ... me .... le ... var .. .pa ... ra.... o...ho... tel ... Star?" and the cab driver answer
"cla ... ro ... que ... sim"
Conclusion: Both are ....
paulobuchsbaum
Posts: 13
Joined: 27 Feb 2016, 22:14

Re: Custom Minimalist MsgBox

13 Apr 2016, 15:00

guest3456 wrote:screenshot?
Thanks. You are right. It's there.
roysubs
Posts: 428
Joined: 29 Sep 2018, 16:37

Re: Custom Minimalist MsgBox

17 Nov 2018, 04:17

Does not work...

I put the code into a new script and then added the examples to the top of the script so that I could run through them. Immediately hit an error:
"Error at line 92: Line Text: Set TimeOut, Off. Error: This line does not contain a recognised action. The program will exit".

A fix would be appreciated. I tried playing with the code, but don't know how to fix. Quite interested to see working code for this.
burque505
Posts: 1736
Joined: 22 Jan 2017, 19:37

Re: Custom Minimalist MsgBox

17 Nov 2018, 11:59

@roysubs, there is indeed a typo in the code. Here's a fixed version, only one small change. At line 80 in ColorBox.ahk (probably line 92 in your script with the examples at the top), changed from

Code: Select all

Set TimeOut, Off
to

Code: Select all

SetTimer TimeOut, Off
ColorBox.ahk:
Spoiler
Now the examples should work for you.
Take a look at MagicBox by alguimist too, it's great.
Regards,
burque505

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: Leli196 and 137 guests