MsgBox Creator for AHK v2 - a115

Share the finished AutoHotkey v2 Scripts and libraries you made here. Please put the current version of AutoHotkey v2 you used in Square Brackets at the start of the topic title.
User avatar
boiler
Posts: 6601
Joined: 21 Dec 2014, 02:44

MsgBox Creator for AHK v2 - a115

06 Jul 2020, 08:48

This is a v2 version of the MsgBox creator used in SciTE4AutoHotkey and first posted by Thalon here. I always found it useful, so I wanted a version for v2. It of course is modified to output v2 code, and it has a couple extra v2-specific options. This version was made for use with any editor since it just copies the code to the clipboard, although it can be modified to be more integrated into specific editors.

MsgBox Creator for AHK v2_MBCv1_1.png
MsgBox Creator for AHK v2_MBCv1_1.png (52.82 KiB) Viewed 2338 times

The code:

Code: Select all

/*
	MsgBox Creator for AHK v2
	
	based on Thalon's original "Messagebox-Creator" script
	thanks to fincs for the icon from SciTE4AutoHotkey

	modified for v2 output and translated into v2 code by boiler

	v1.0	- initial release
	v1.1	- added option for single quotes vs. double quotes as suggested by Helgef
			- automatically saves user preferences of the function format options:
				(parenthesis, numeric/string options, single/double quotes)
			- remembers last window position for next time it is run
*/

#SingleInstance Ignore

TraySetIcon("MsgBox.ico", 1)

Paren := IniRead("MsgBox Creator settings.ini", "Main", "Paren", 1)
NumOpt := IniRead("MsgBox Creator settings.ini", "Main", "NumOpt", 1)
DoubleQu := IniRead("MsgBox Creator settings.ini", "Main", "DoubleQu", 1)
WinPosX := IniRead("MsgBox Creator settings.ini", "Main", "WinPosX", "NONE")
WinPosY := IniRead("MsgBox Creator settings.ini", "Main", "WinPosY", "NONE")

global TestMode := 0

MainGui := Gui.New(, "MsgBox Creator for AHK v2")
MainGui.Add("Text", "x10 y10 Section", "Title:")
MainGui.Add("Edit", "xs+0 ys+15 section w400 vTitle").OnEvent("Change", "CreateMsgBoxFunction")
MainGui.Add("Text", "xs+0 ys+25 section", "Text:")
MainGui.Add("Edit", "xs+0 ys+15 section r3 w400 vText WantTab").OnEvent("Change", "CreateMsgBoxFunction")

MainGui.Add("Groupbox", "x10 y120 h215 w190 section", "Buttons")
MainGui.Add("Radio", "xs+10 ys+20 section vButtonSelection1 Checked", "OK").OnEvent("Click", "CreateMsgBoxFunction")
MainGui.Add("Radio", "xs+0 ys+25 section vButtonSelection2", "OK/Cancel").OnEvent("Click", "CreateMsgBoxFunction")
MainGui.Add("Radio", "xs+0 ys+25 section vButtonSelection3", "Abort/Retry/Ignore").OnEvent("Click", "CreateMsgBoxFunction")
MainGui.Add("Radio", "xs+0 ys+25 section vButtonSelection4", "Yes/No/Cancel").OnEvent("Click", "CreateMsgBoxFunction")
MainGui.Add("Radio", "xs+0 ys+25 section vButtonSelection5", "Yes/No").OnEvent("Click", "CreateMsgBoxFunction")
MainGui.Add("Radio", "xs+0 ys+25 section vButtonSelection6", "Retry/Cancel").OnEvent("Click", "CreateMsgBoxFunction")
MainGui.Add("Radio", "xs+0 ys+25 section vButtonSelection7", "Cancel/Try Again/Continue").OnEvent("Click", "CreateMsgBoxFunction")
MainGui.Add("Checkbox", "xs+0 ys+25 vButtonSelectionHelp", "Help button").OnEvent("Click", "CreateMsgBoxFunction")

MainGui.Add("Groupbox", "x220 y120 h215 w190 section", "Icons")
Icon1Ctrl := MainGui.Add("Radio", "xs+10 ys+25 section vIcon1 Checked", "No Icon")
Icon1Ctrl.OnEvent("Click", "SelectIcon")
Icon2Ctrl := MainGui.Add("Radio", "xs+0 ys+40 vIcon2", "Stop/Error")
Icon2Ctrl.OnEvent("Click", "SelectIcon")
Icon3Ctrl := MainGui.Add("Radio", "xs+0 ys+80 vIcon3", "Question")
Icon3Ctrl.OnEvent("Click", "SelectIcon")
Icon4Ctrl := MainGui.Add("Radio", "xs+0 ys+120 vIcon4", "Exclamation")
Icon4Ctrl.OnEvent("Click", "SelectIcon")
Icon5Ctrl := MainGui.Add("Radio", "xs+0 ys+160 vIcon5", "Info")
Icon5Ctrl.OnEvent("Click", "SelectIcon")

MainGui.Add("Picture", "xs+90 ys-10 h30 w20").OnEvent("Click", (*) => SelectIcon(Icon1Ctrl))
MainGui.Add("Picture", "xs+90 ys+30 icon4 w32 h32", A_WinDir "\system32\user32.dll").OnEvent("Click", (*) => SelectIcon(Icon2Ctrl))
MainGui.Add("Picture", "xs+90 ys+70 icon3 w32 h32", A_WinDir "\system32\user32.dll").OnEvent("Click", (*) => SelectIcon(Icon3Ctrl))
MainGui.Add("Picture", "xs+90 ys+110 icon2 w32 h32", A_WinDir "\system32\user32.dll").OnEvent("Click", (*) => SelectIcon(Icon4Ctrl))
MainGui.Add("Picture", "xs+90 ys+150 icon5 w32 h32", A_WinDir "\system32\user32.dll").OnEvent("Click", (*) => SelectIcon(Icon5Ctrl))

MainGui.Add("Groupbox", "x430 y20 h120 w190 section", "Modality")
MainGui.Add("Radio", "xs+10 ys+20 section Checked vModality1", "Normal").OnEvent("Click", "CreateMsgBoxFunction")
MainGui.Add("Radio", "xs+0 ys+25 section vModality2", "Task Modal").OnEvent("Click", "CreateMsgBoxFunction")
MainGui.Add("Radio", "xs+0 ys+25 section vModality3", "System Modal (always on top)").OnEvent("Click", "CreateMsgBoxFunction")
MainGui.Add("Radio", "xs+0 ys+25 section vModality4", "Always on top").OnEvent("Click", "CreateMsgBoxFunction")

MainGui.Add("Groupbox", "x430 y160 h45 w190 section", "Default Button")
MainGui.Add("Radio", "xs+10 ys+20 section Checked vDefault1", "1st").OnEvent("Click", "CreateMsgBoxFunction")
MainGui.Add("Radio", "xs+65 ys+0 section vDefault2", "2nd").OnEvent("Click", "CreateMsgBoxFunction")
MainGui.Add("Radio", "xs+65 ys+0 section vDefault3", "3rd").OnEvent("Click", "CreateMsgBoxFunction")

MainGui.Add("Groupbox", "x430 y220 h45 w190 section", "Alignment")
MainGui.Add("Checkbox", "xs+10 ys+20 vAlignment1 section", "Right-justified").OnEvent("Click", "CreateMsgBoxFunction")
MainGui.Add("Checkbox", "xs+100 ys+0 vAlignment2", "Right-to-left").OnEvent("Click", "CreateMsgBoxFunction")

MainGui.Add("Groupbox", "x430 y280 h45 w100 section", "Timeout")
MainGui.Add("Edit", "xs+10 ys+17 w80 vTimeout").OnEvent("Change", "CreateMsgBoxFunction")
MainGui.Add("UpDown", "Range-1-2147483", "-1")

MainGui.Add("Button", "x540 y285 h40 w80 vTest", "&Test").OnEvent("Click", "Test")
MainGui.Add("Button", "x430 y345 h45 w100 Default", "&Copy result to clipboard").OnEvent("Click", "CopyCode")
MainGui.Add("Button", "x540 y345 h45 w80", "&Reset").OnEvent("Click", (*) => Reset(MainGui))

MainGui.Add("Groupbox", "x10 y340 w105 h50 section", "Function Format")
ParenOptCtrl := MainGui.Add("Checkbox", "xs+10 ys+22 w90 vParen", "Parentheses")
ParenOptCtrl.OnEvent("Click", (*) => SaveOptions(MainGui))

MainGui.Add("Groupbox", "x125 ys w140 h50 section", "Options Format")
NumOptCtrl := MainGui.Add("Radio", "xs+10 ys+22 w60 vNumOpt", "Numeric")
NumOptCtrl.OnEvent("Click", (*) => SaveOptions(MainGui))
StrOptCtrl := MainGui.Add("Radio", "x+10 yp w50 vStrOpt", "String")
StrOptCtrl.OnEvent("Click", (*) => SaveOptions(MainGui))

MainGui.Add("Groupbox", "x275 ys w135 h50 section", "Quotation Marks")
DoubleQuOptCtrl := MainGui.Add("Radio", "xs+10 ys+22 w60 vDoubleQu", "Double")
DoubleQuOptCtrl.OnEvent("Click", (*) => SaveOptions(MainGui))
SingleQuOptCtrl := MainGui.Add("Radio", "x+5 yp w50 vSingleQu", "Single")
SingleQuOptCtrl.OnEvent("Click", (*) => SaveOptions(MainGui))

MainGui.Add("Groupbox", "x10 y395 w610 h75 section", "Result")
MainGui.SetFont(, "Courier New")
MainGui.SetFont(, "Lucida Sans Typewriter") ; preferred
MainGui.Add("Edit", "xs+10 ys+20 w590 r3 vFunctionText")

ParenOptCtrl.Value := Paren
if NumOpt
	NumOptCtrl.Value := 1
else
	StrOptCtrl.Value := 1
if DoubleQu
	DoubleQuOptCtrl.Value := 1
else
	SingleQuOptCtrl.Value := 1

MainGui.OnEvent("Close", (*) => ExitApp())
if (WinPosX = "NONE") || (WinPosY = "NONE")
	MainGui.Show()
else
	MainGui.Show("x" WinPosX " y" WinPosY)
CreateMsgBoxFunction(MainGui) ; initialize function string
OnMessage 0x232, "WM_EXITSIZEMOVE"
return

CreateMsgBoxFunction(this, *) {
	Try
		saved := this.Submit(0)
	Catch {
		this := this.Gui
		saved := this.Submit(0)
	}
	loop 7 {
		if (saved.ButtonSelection%A_Index% = 1) {
			buttonSelection := A_Index - 1
			break
		}
	}
	help := (saved.ButtonSelectionHelp ? 16384 : 0)
	loop 5 {
		if (saved.Icon%A_Index% = 1) {
			icon := [0, 16, 32, 48, 64][A_Index]
			break
		}
	}
	loop 4 {
		if (saved.Modality%A_Index% = 1) {
			modality := [0, 8192, 4096, 262144][A_Index]
			break
		}
	}
	loop 3 {
		if (saved.Default%A_Index% = 1) {
			default := [0, 256, 512][A_Index]
			break
		}
	}
	title := EscapeCharacters(saved.Title, saved.DoubleQu)
	text := EscapeCharacters(saved.Text, saved.DoubleQu)
	timeout := saved.Timeout
	alignment := (saved.Alignment1 = 1) * 524288 + (saved.Alignment2 = 1) * 1048576
	if saved.StrOpt
		options :=	StrReplace(RTrim(["O", "OC", "ARI", "YNC", "YN", "RC", "CTC"][buttonSelection + 1] . " "
						. Map(0, "", 16, "IconX", 32, "Icon?", 48, "Icon!", 64, "Iconi")[icon] . " "
						. Map(0, "", 256, "Default2", 512, "Default3", 768, "Default4")[default] . " "
						. (modality + alignment + help ? modality + alignment + help : "")), "  ", " ")
	else
		options := buttonSelection + icon + modality + default + alignment + help
	
	if TestMode
		return {Options: options, Title: title, Text: text, Timeout: timeout}

	if (timeout = -1) || (timeout = "")
		timeout := ""
	else 
		timeout := "T" StrReplace(timeout, ",", ".") ; allows "," as decimal point

	Qu := saved.DoubleQu ? Chr(34) : "'"
	functionText :=	"MsgBox" . (saved.Paren ? "(" : " ") ; insert paren if selected
						. (text ? Qu . text . Qu : "") "," ; insert quotes around text if it exists
						. (title ? " " . Qu . title . Qu : "") . "," ; insert space and insert quotes around title if it exists
						. ((options . timeout) ? " " . Qu : "") ; insert space and open quote if there are options or timeout
						. (options ? options : "") ; if sum of options is 0, leave it blank, otherwise put in options
						. (options && timeout ? " " : "") ; if there is both an option number and a timeout, insert a space
						. timeout . ((options . timeout) ? Qu : "") ; add timeout if it exists and close quote if necessary
	functionText := RTrim(functionText, ",") ; remove unnecessary commas on the right
						. (saved.Paren ? ")" : "") ; add closing paren if selected
	this.__Item["FunctionText"].Value := functionText
}

SelectIcon(thisCtrl, *) {
	thisCtrl.Value := 1
	CreateMsgBoxFunction(thisCtrl.Gui)
}

EscapeCharacters(str, doubleQu) {
	if doubleQu
		str := StrReplace(str, Chr(34), "``" Chr(34))
	else
		str := StrReplace(str, "'", "``" "'")
	return StrReplace(str, "`n", "``n")
}

Test(thisCtrl, *) {
	TestMode := 1
	info := CreateMsgBoxFunction(thisCtrl)
	TestMode := 0
	thisCtrl.Gui.Opt("+OwnDialogs")
	title := info.Title ? info.Title : "(Name of script)"
	text := StrReplace(info.Text, "``n", "`n")
	if (thisCtrl.Gui.__Item["DoubleQu"].Value = 1)
		text := StrReplace(text, "```"", "`"")
	else
		text := StrReplace(text, "``'", "`'")
	MsgBox text, title, info.Options . (info.Timeout = -1 ? "" : " T" info.Timeout)
}

CopyCode(thisCtrl, *) {
	saved := thisCtrl.Gui.Submit(0)
	A_Clipboard := saved.FunctionText
	MsgBox "The MsgBox code has been copied to the clipboard.`n`nIt is ready to be pasted into your editor.", "MsgBox Code Copied", "4160 T3"
}

Reset(thisGui, *) {
	thisGui.__Item["Title"].Value := ""
	thisGui.__Item["Text"].Value := ""
	thisGui.__Item["ButtonSelection1"].Value := 1
	thisGui.__Item["ButtonSelectionHelp"].Value := 0
	thisGui.__Item["ButtonSelectionHelp"].Value := 0
	thisGui.__Item["Icon1"].Value := 1
	thisGui.__Item["Modality1"].Value := 1
	thisGui.__Item["Default1"].Value := 1
	thisGui.__Item["Alignment1"].Value := 0
	thisGui.__Item["Alignment2"].Value := 0
	thisGui.__Item["Timeout"].Value := -1
	thisGui.__Item["NumOpt"].Value := 1
	thisGui.__Item["Paren"].Value := 1
	CreateMsgBoxFunction(thisGui)
}

SaveOptions(thisGui, *) {
	saved := thisGui.Submit(0)
	IniWrite saved.Paren, "MsgBox Creator settings.ini", "Main", "Paren"
	IniWrite saved.NumOpt, "MsgBox Creator settings.ini", "Main", "NumOpt"
	IniWrite saved.DoubleQu, "MsgBox Creator settings.ini", "Main", "DoubleQu"
	CreateMsgBoxFunction(thisGui)
}

WM_EXITSIZEMOVE(*) {
	global MainGui
	MainGui.GetPos(x, y)
	IniWrite x, "MsgBox Creator settings.ini", "Main", "WinPosX"
	IniWrite y, "MsgBox Creator settings.ini", "Main", "WinPosY"
}

The zip file below includes the associated icon (taken from SciTE4AutoHotkey -- thanks, fincs). The forum doesn't allow a .ico file to be dropped in directly, so I zipped it along with the .ahk file:
MsgBox Creator for AHK v2_MBCv1_1.zip
(3.69 KiB) Downloaded 30 times

v1.1 Changes:
  • added option for single quotes vs. double quotes as suggested by Helgef
  • automatically saves user preferences of the function format options (parenthesis, numeric/string options, single/double quotes)
  • remembers last window position for next time it is run
Last edited by boiler on 10 Jul 2020, 13:59, edited 1 time in total.
User avatar
kczx3
Posts: 1198
Joined: 06 Oct 2015, 21:39

Re: MsgBox Creator for AHK v2 - a115

06 Jul 2020, 09:43

Cool, thanks! I love continuing to see more v2 stuff created! Keep it coming as this is what is needed to create momentum and encourage others to use v2 as well.
toralf
Posts: 792
Joined: 27 Apr 2014, 21:08
Location: Germany

Re: MsgBox Creator for AHK v2 - a115

06 Jul 2020, 14:16

Is there a difference to MagicBox?
ciao
toralf
User avatar
boiler
Posts: 6601
Joined: 21 Dec 2014, 02:44

Re: MsgBox Creator for AHK v2 - a115

06 Jul 2020, 14:27

This is a tool for allowing you to create the parameters for AHK's built-in MsgBox function for AHK v2 by making selections from a graphical interface.

MagicBox by Alguimist allows you to build other types of message boxes with more features. I don't know if Alguimist plans to make a version of it for AHK v2.
Helgef
Posts: 4467
Joined: 17 Jul 2016, 01:02
Contact:

Re: MsgBox Creator for AHK v2 - a115

09 Jul 2020, 01:50

Good work, thanks for sharing :thumbsup:. I usually use ' instead of " to quote text, an option for that would be nice.

Cheers.
User avatar
boiler
Posts: 6601
Joined: 21 Dec 2014, 02:44

Re: MsgBox Creator for AHK v2 - a115

09 Jul 2020, 07:10

Thanks! Good suggestion. I will add that option and have it escape that character if used within the quoted text if necessary (and of course not have it escape the double quote in that case).
User avatar
boiler
Posts: 6601
Joined: 21 Dec 2014, 02:44

Re: MsgBox Creator for AHK v2 - a115

10 Jul 2020, 14:00

Edited first post to update to v1.1 with these changes:
  • added option for single quotes vs. double quotes as suggested by Helgef
  • automatically saves user preferences of the function format options (parenthesis, numeric/string options, single/double quotes)
  • remembers last window position for next time it is run
(Also, thanks for the comment @kczx3! Missed that before.)
Larkal
Posts: 13
Joined: 08 Mar 2019, 00:36

Re: MsgBox Creator for AHK v2 - a115

19 Jul 2020, 07:47

Awesome! Thanks, amazing work! I used to use the one for AHK 1 as well, much appreciated.
garry
Posts: 2460
Joined: 22 Dec 2013, 12:50

Re: MsgBox Creator for AHK v2 - a115

19 Jul 2020, 08:48

@boiler thank you , very useful , I use longtime Thalon's MSGBOX-creator script
it's not easy for me to learn AHK_v2
User avatar
boiler
Posts: 6601
Joined: 21 Dec 2014, 02:44

Re: MsgBox Creator for AHK v2 - a115

19 Jul 2020, 10:55

Thanks, @garry. I think the more you use v2, the more you’ll find it’s more consistent than v1.

Return to “AutoHotkey v2 Scripts and Functions”

Who is online

Users browsing this forum: No registered users and 2 guests