The idea is that all scripts can make use of a function that calls the GUI factory script.
Satellite scripts communicate with the 'GUI factory' script via an object.
The benefits are that scripts calling the function do not have to 'look after' the GUIs in any way and do not become Persistent, they just use one line to call the function. This would also make it easy to create special GUIs with hyperlinks, or multiple edit fields, or multiline edit fields, or with custom colours/fonts etc.
The issue is if multiple GUIs need to exist simultaneously, for one script or for multiple scripts, if anybody is able to work out a solution for this. I'm basically stuck on this. I hope others see potential value in this idea.
Communicating between scripts via an object is based on:
ObjRegisterActive - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?t=6148
Btw is this object multidimensional? I did a test which suggested it isn't, which meant the script had to be rewritten slightly.
Run the factory script first then (multiple copies of) the satellite script.
Code: Select all
;GUI factory script, creates GUIs
#SingleInstance force
#Persistent
;ObjRegisterActive - AutoHotkey Community
;https://autohotkey.com/boards/viewtopic.php?t=6148
; Register our object so that other scripts can get to it. The second
; parameter is a GUID which I generated. You should generate one unique
; to your script. You can use [CreateGUID](http://goo.gl/obfmDc).
vGUID := "{6B39CAA1-A320-4CB0-8DB4-352AA81E460E}"
ObjRegisterActive(ActiveObject, vGUID)
OnExit("Revoke")
Return
Revoke()
{
ObjRegisterActive(ActiveObject, "")
ExitApp
}
class ActiveObject
{
Message(oArray)
{
global
DetectHiddenWindows, Off
;MsgBox % oArray.ID
vID := oArray.ID
vIDGui%vID% := oArray.ID
vDefault%vID% := oArray.InputDefault
vPrompt%vID% := oArray.Prompt
vWinTitle%vID% := oArray.WinTitle
vType%vID% := oArray.Type
vFontName%vID% := oArray.FontName
vFontSize%vID% := oArray.FontSize
;MsgBox % vPrompt%vID%
vWait%vID% := 1
Gui, New, % "+HwndhWnd" vID
Gui, Gui%vID%:Font, % "s " vFontSize%vID%, % vFontName%vID%
Gui, Gui%vID%:Color, F0F0F0
Gui, Gui%vID%:+0x94CC0A4C +E0x00010100 +LabelGui
Gui, Gui%vID%:Add, Text, x0 y0 w300 h100, % vPrompt%vID%
if InStr(vType%vID%, "i")
Gui, Gui%vID%:Add, Edit, x0 y100 w300 h47 vvEditGui%vID%, % vDefault%vID%
Gui, Gui%vID%:Add, Button, x5 y170 w140 h40 Default gButtonOK, OK
Gui, Gui%vID%:Add, Button, x155 y170 w140 h40 gButtonCancel, Cancel
Gui, Gui%vID%:Show, w300 h230, % vWinTitle%vID%
;MsgBox % hWnd%vID%
;WinWaitClose, % "ahk_id " hWnd%vID%
while vWait%vID%
Sleep 10
Return
ButtonOK:
ButtonCancel:
GuiClose:
GuiEscape:
Gui, %A_Gui%:Submit
Gui, %A_Gui%:Destroy
;this["RetButton" A_Gui] := ""
if (A_ThisLabel = "ButtonOK")
this["RetButton" A_Gui] := "OK"
if (A_ThisLabel = "ButtonCancel")
this["RetButton" A_Gui] := "Cancel"
if (A_ThisLabel = "GuiClose") || (A_ThisLabel = "GuiEscape")
this["RetClose" A_Gui] := 1
else
this["RetClose" A_Gui] := 0
;MsgBox % "RetInput" A_Gui
;MsgBox % vEdit%A_Gui%
this["RetInput" A_Gui] := vEdit%A_Gui%
vWait%vID% := 0
Return
}
}
;==================================================
ObjRegisterActive(Object, CLSID, Flags:=0) {
static cookieJar := {}
if (!CLSID) {
if (cookie := cookieJar.Remove(Object)) != ""
DllCall("oleaut32\RevokeActiveObject", "uint", cookie, "ptr", 0)
return
}
if cookieJar[Object]
throw Exception("Object is already registered", -1)
VarSetCapacity(_clsid, 16, 0)
if (hr := DllCall("ole32\CLSIDFromString", "wstr", CLSID, "ptr", &_clsid)) < 0
throw Exception("Invalid CLSID", -1, CLSID)
hr := DllCall("oleaut32\RegisterActiveObject"
, "ptr", &Object, "ptr", &_clsid, "uint", Flags, "uint*", cookie
, "uint")
if hr < 0
throw Exception(format("Error 0x{:x}", hr), -1)
cookieJar[Object] := cookie
}
;==================================================
Code: Select all
;GUI satellite script, asks for GUIs to be created
#SingleInstance force
;ObjRegisterActive - AutoHotkey Community
;https://autohotkey.com/boards/viewtopic.php?t=6148
;MsgBox
;https://autohotkey.com/docs/commands/MsgBox.htm
;InputBox
;https://autohotkey.com/docs/commands/InputBox.htm
;MsgBox
;https://lexikos.github.io/v2/docs/commands/MsgBox.htm
;InputBox
;https://lexikos.github.io/v2/docs/commands/InputBox.htm
;==================================================
vGUID := "{6B39CAA1-A320-4CB0-8DB4-352AA81E460E}"
oJEENfy := ComObjActive(vGUID)
;Return
;q::
SplitPath, A_ScriptName, , , , vWinTitle
vType := "i" ;an InputBox
vCount++ ;the nth GUI box created by the script
vID := (A_ScriptHwnd+0) "_" vType vCount
oArray := {}
oArray.ID := vID
oArray.WinTitle := vWinTitle
oArray.Type := vType
oArray.FontName := "Arial"
oArray.FontSize := 18
oArray.Prompt := "`r`nhello world`r`nbox " vCount
oArray.InputDefault := "default text"
oArray.RetButton := ""
oArray.RetClose := 0
oArray.RetInput := ""
;===============
oJEENfy.Message(oArray)
vInfo := "button chosen: " oJEENfy["RetButtonGui" vID] "`r`n"
vInfo .= "was window closed: " oJEENfy["RetCloseGui" vID] "`r`n"
vInfo .= "user input text: " oJEENfy["RetInputGui" vID]
MsgBox % vInfo
Return
;==================================================