Yep, the last one should work.
Here is my final (so far) solution — a completely customizable message box with up to 12 buttons (arranged vertically if 4 or more). The buttons are added within a loop, and the scripts needs only one single subroutine to capture the button clicked. Results are written to the Windows registry so they can be easily queried by almost any scripting language (DOS batch, Powershell, VB(A), AHK, ...) and a bunch of other programs.
As the text of the buttons is freely choosable it makes no sense to use the numbering convention used for e.g. the standard AHK and VB message boxes.
Not implemented is the standard MsgBox capability to add a little icon (hand, question mark, exclamation mark, asterisk). Of course it could be easily added using an additional input parameter and an extra line to the box' header:
Code: Select all
GUI, Add, Picture, x5 y15 w15 h15, % p[5]
and some according adjustments (5 parameters instead of 4 before the actual buttons begin).
The script is tested and works as far as I can evaluate it.
A few MsgBox statements are commented out and serve as means of debug if required.
Good luck in using it ...
Code: Select all
#SingleInstance Force
#NoEnv
SetRegView 64
; Completely customizable MessageBox
; p1 = %1% ; Window title
; p2 = %2% ; Header
; p3 = %3% ; Standard button
; p4 = %4% ; Timeout
; p5 = %5% ; Text1
; p6 = %6% ; Text2
; p7 = %7% ; Text3
; ...
; 4 or more buttons will be arranged vertically
; № of parameters
NoPar := A_Args.Length()
; № of actual buttons
NoBut := Round(NoPar-4,0)
; Array for input parameters
p := []
count := 0
Loop, %0% ; For each parameter:
{
count += 1
param := %A_Index% ; Fetch the contents of the variable whose name is contained in A_Index.
p[count] := param
; MsgBox, ,Parameter, Parameter no %A_Index% is %param%.
}
; Precautionally convert no of standard button and timeout to numbers
p[3] += 0
p[4] += 0
; MsgBox % NoPar . "`n" . p[1] . "`n" . p[2] . "`n" . p[3] . "`n" . p[4] . "`n" . p[5] . "`n" . p[6] . "`n" . p[7] . "`n" . p[8] . "`n" . p[9] . "`n" . p[10] . "`n" . p[11] . "`n" . p[12] . "`n" . p[13] . "`n" . p[14] . "`n" . p[15] . "`n" . p[16] . "`n" . p[17] . "`n" . p[18] . "`n" . p[19] . "`n" . p[20]
if (NoPar < 5)
{
MsgBox % "The script needs at least 5 parameters, but only " . NoPar . " are present."
ExitApp
}
if (A_Args.Length() > 16)
{
MsgBox % "More than 12 choices aren't envisaged. But " NoBut " are present!"
ExitApp
}
; Header (button explanation) in red
Gui, Font, s12 cRed, Verdana Bold
Gui, Add, Text, x25 y15, % p[2]
Gui, Font, s10 cBlack, Verdana
; Buttons
if (NoPar < 8)
{
; Max 3 buttons => horizontal arrangement
subInd := 0
xStart := 25
Loop, % NoBut
{
x1 := xStart+(subInd*135)
subInd += 1
actInd := Round(subInd+4, 0)
; Default Button
dFlt := (subInd = p[3]) ? "Default" : ""
; MsgBox % "subInd: " . subInd . "`nInd: " . actInd . "`nx1: " . x1
GUI, Add, Button, % dflt " x" x1 " y50 w120 h40 gmySub", % p[actInd]
GUI, Show, Autosize, % p[1]
}
}
else if ((NoPar >= 8) && (NoPar < 17))
{
; More than 3 and a maximum of 12 buttons => vertical arrangement
subInd := 0
yStart := 50
Loop, % NoBut
{
y1 := yStart+(subInd*58)
subInd += 1
actInd := Round(subInd+4, 0)
; Default Button
dFlt := (subInd = p[3]) ? "Default" : ""
; MsgBox % "subInd: " . subInd . "`nInd: " . actInd
GUI, Add, Button, % dFlt " x25 y" y1 " w120 h40 gmySub", % p[actInd]
GUI, Show, Autosize, % p[1]
}
}
; Timeout
if (p[4] > 1)
{
SetTimer, ExitAHK, % p[4]
}
return
;====================
; Subs and functions
;====================
mySub:
myAction := A_GuiControl
; Reverse look-up of the parameter № of the button clicked
NoDirect := HasVal(p, myAction)
; Calculating the № of the button (subtract 4 leading input parameters)
NoCorr := NoDirect-4
; MsgBox % "You clicked: " . myAction . ".`nIndex: " . NoCorr
; Writing results to Windows registry
RegWrite, Reg_SZ, HKEY_LOCAL_MACHINE, System\ScriptEngine, MsgOut, % NoCorr
RegWrite, Reg_SZ, HKEY_LOCAL_MACHINE, System\ScriptEngine, Action, % myAction
Gui, Destroy
return
ExitAHK:
; GUI run into Timeout
RegWrite, Reg_SZ, HKEY_LOCAL_MACHINE, System\ScriptEngine, MsgOut, 98
RegWrite, Reg_SZ, HKEY_LOCAL_MACHINE, System\ScriptEngine, Action, Timeout
ExitApp
Return
HasVal(haystack, needle)
{
; Returns the index of the last found item ('needle') within in the array 'haystack' —
; or 0 if nothing was found, 'haystack' is no object or empty
if !(IsObject(haystack)) || (haystack.Length() = 0)
{
return 0
}
for index, value in haystack
{
if (value = needle)
{
return index
}
}
return 0
}