which will greatly speed up creating GUI's. I am in the process of creating a program to organize and keep track of our CNC machines tooling where I work, which will include all sorts of goodies, but consequently will end up having hundreds of controls, hence why this will be such a great
for creating such a program.
The problem I have run in to is that it didn't yet have the functionality to reload a saved GUI in its entirety to continue where I left off. So I've made some modifications that now let you open a saved script and completely reload all options, variables, and controls.
Due to the way AutoGUI formats the code, scripts that have not been created with AutoGUI may need to be rearranged to fit this format to be loaded correctly (like having all GUI building done at the beginning and separated from the rest of the code with the "; End of the GUI section" delimiter for eg.)
I have used the latest version 1.1.3, and tested it as much as I can and have obtained consistent results with loading GUI's. I'm sure that my code could probably be a little tighter but I've only been using AHK for a couple of months and am still learning so any upgrades or corrections to the code I have added would be welcome.
Use the "Open GUI..." command from the File Menu (I removed the MsgBox about using the Window Cloning
Code: Select all
ParseScript(Source) {
m.Source := ""
;Added to disable these vars unless in the script beibg loaded -RH
Script.NoEnv := 0
Script.Warn := ""
Script.SingleInstance := ""
Script.WorkingDir := ""
Script.SendMode := ""
hAnchor := {}
Loop Parse, Source, `n
{
CtlLoad := A_LoopField
IfInString A_LoopField, %Delimiter% ;Detects when delimiter is reached - RH
GuiLoaded = 1
IfInString A_LoopField, hwnd ;Removes hwnd varible from string used to get control options, otherwise controls are not registered correctly -RH
{
CtlLoad := ""
Loop Parse, A_LoopField, %A_Space%
IfNotInString A_LoopField, hWnd
CtlLoad .= " " . A_LoopField
}
IfInString A_LoopField, Gui Add`, Tab2`, ;Sets the Tabindex and Tabcontrol numbers for the current controls -RH
fTabControl++
If (RegExMatch(A_LoopField, "iO)^\s*?Gui\s*\,?\s*(\w+\:)?\s?Tab,\s?(?P<TabIndex>([\w\+\-\s]+))", Rex))
fTabIndex := RegExReplace(Rex.TabIndex, "[\r\n]*")
If !Guiloaded
{
; Get Window Events in script to set proper checkboxes -RH
IfInString A_LoopField, Close:
g.Window.GuiClose := 1
IfInString A_LoopField, Escape:
g.Window.GuiEscape := 1
IfInString A_LoopField, Size:
g.Window.GuiSize := 1
IfInString A_LoopField, ContextMenu:
g.Window.GuiContextMenu := 1
IfInString A_LoopField, DropFiles:
g.Window.GuiDropFiles := 1
IfInString A_LoopField, OnClipboardChange:
g.Window.OnClipboardChange := 1
;Get Script Options to set correct Checkboxes and set correct item in dropdown choices -RH
IfInString A_LoopField, #NoEnv
Script.NoEnv := 1
IfInString A_LoopField, #Warn
If (SubStr(A_LoopField,9) = "")
Script.Warn := "MsgBox"
Else
Script.Warn := RegExReplace(SubStr(A_LoopField,9), "[\r\n]*")
IfInString A_LoopField, #SingleInstance
Script.SingleInstance := SubStr(A_LoopField,17)
IfInString A_LoopField, #NoTrayIcon
Script.NoTrayIcon := 1
IfInString A_LoopField, SetWorkingDir
{
Script.WorkingDir := SubStr(A_LoopField,15)
SetWorkingDir % RegExReplace(SubStr(A_LoopField,15), "[\r\n]*")
}
IfInString A_LoopField, SendMode
Script.SendMode := RegExReplace(SubStr(A_LoopField,10), "[\r\n]*")
IfInString A_LoopField, SetBatchLines
Script.NoBatchLines := 1
IfInString A_LoopField, ListLines
Script.ListLinesOff := 1
IfInString A_LoopField, #Persistent
Script.Persistent := 1
IfInString A_LoopField, Menu Tray`, UseErrorLevel
Script.IgnoreMenuErrors := 1
;Get Font Info, for some reason needs to be applied 2 lines after it appears in script to be be applied to the correct control - RH
If (fft = A_Index-2)
{
g[hwnd].fontoptions := flfont
fft = 0
}
IfInString A_LoopField, Font`,
{
flFont := Substr(A_Loopfield,11)
fft := A_Index
}
}
; Menu item
If (RegExMatch(A_LoopField, "iO)^Menu\s*\,?\s*(?P<MenuName>([\w]+)),\s?Add(,\s?(?P<MenuItem>.*),\s?(?P<GoSub>([\w\:]+)))?", Rex)) {
m.Source .= "Menu " . Rex.MenuName . ", Add, " . Rex.MenuItem . ", " . Rex.GoSub . "`n"
Menu % Rex.MenuName, Add, % Rex.MenuItem, % Rex.GoSub
} Else If (RegExMatch(A_LoopField, "iO)^Menu\s*\,?\s*(?P<MenuName>([\w]+)),\s?Add", Rex)) {
m.Source .= "Menu " . Rex.MenuName . ", Add`n"
Menu % Rex.MenuName, Add
}
;Added to Load Window options - RH
If (RegExMatch(A_LoopField, "iO)^\s*?Gui\s*\,?\s*(\w+\:)?\s?New,\s?(?P<Options>([\w\+\-\s]+))", Rex)) {
g.Window.Options := RegExReplace(Rex.Options, "([\,\s]?g\w+|[\,\s]?v\w+)")
g.Window.Options := StrReplace(g.window.options, "+OwnDialo", "+OwnDialogs")
IfInString A_LoopField, : New
g.Window.Name := SubStr(A_LoopField, 5,InStr(A_LoopField, ":")-5)
Loop Parse, % g.Window.Options, %A_Space%
{
IfInString A_LoopField, Label
g.Window.Label := SubStr(A_LoopField,6)
IfInString A_LoopField, hwnd
g.Window.hWndvar := SubStr(A_LoopField,5)
}
g.Window.Options := StrReplace(g.window.options, "Label"g.window.label)
g.Window.Options := StrReplace(g.window.options, "hWnd"g.window.hWndVar)
}
IfInString A_LoopField, Gui Color`, ;Loads the background Colour -RH
g.Window.Color := SubStr(A_LoopField,12)
IfInString A_LoopField, Menu Tray`, Icon ;Loads the Tray Icon - RH
Loop Parse, A_LoopField, `,
{
If A_Index = 3
g.Window.Icon := SubStr(A_LoopField,2)
If A_Index = 4
g.Window.IconIndex := SubStr(A_LoopField,2)
}
; Control
If (RegExMatch(CtlLoad, "iO)^\s*?Gui\s*\,?\s*(\w+\:)?\s?Add,\s?(?P<Type>([A-Za-z]+)\d?),\s?(?P<Options>([\w\+\-\s]+))(,\s?(?P<Text>(.+)))?", Rex)) {
Options := RegExReplace(Rex.Options, "([\,\s]?g\w+|[\,\s]?v\w+)")
TabPos := [fTabIndex, fTabControl] ; Controls Tab placement of controls -RH
IfInString A_LoopField, Gui Add`, Tab2`,
TabPos := ""
If (Rex.Type = "StatusBar" && StatusBarExist()) {
GuiControl %Child%: Show, msctls_statusbar321
} Else {
If fTabIndex ; Puts the controls into the correct tab in gui window - RH
Gui %Child%: Tab, % TabPos[1], % TabPos[2]
Gui %Child%: Add, % Rex.Type, % "hWndhWnd " . Options, % Rex.Text
}
If (Rex.Type = "TreeView") {
Parent := TV_Add("TreeView")
TV_Add("Child", Parent)
}
;Added to load properties - RH
fMatch := "gvxywh0E"
Loop parse, A_LoopField, `,
If A_index = 3
strf := A_LoopField
Loop parse, strf, %A_Space% ;this loop makes sure these options arent stores as vVar or gLabel - RH
{ ;and gets the hWndVar, style, exstyle values
If Substr(A_Loopfield,1,1) = "v"
If (A_LoopField != "vscroll" OR A_LoopField != "vertical")
fvVar := Substr(A_Loopfield,2)
Else
fOptions .= A_LoopField . " "
If Substr(A_Loopfield,1,1) = "g"
If (A_LoopField != "grid")
fgLabel := Substr(A_Loopfield,2)
Else
fOptions .= A_LoopField . " "
If Substr(A_Loopfield,1,4) = "hwnd"
fhwndVar := Substr(A_Loopfield,5)
If Substr(A_Loopfield,1,1) = "0"
fStyle := A_LoopField
If Substr(A_Loopfield,1,1) = "E"
fExStyle := A_LoopField
IfNotInString fMatch, % Substr(A_Loopfield,1,1)
fOptions .= A_LoopField . " "
}
If fhWndVar ;Stores the hwndvariable and hwnd to attach an anchor later if one exists - RH
ObjRawSet(hAnchor, fhWndVar, hWnd)
GuiControlGet c, %Child%: Pos, %hWnd%
g.Register(hWnd, Rex.Type, "", Rex.Text, cx, cy, cw, ch, fhWndVar, fvVar, fgLabel, fOptions, fStyle, fExStyle, flFont, "", TabPos)
g.ControlList.Insert(hWnd)
fExStyle := "" ;Resets all the control option variables for the next control - RH
fStyle := ""
fhwndVar := ""
fvVar := ""
fgLabel := ""
fOptions := ""
strf := ""
}
IfInString A_LoopField, Anchor( ;Detects if there are anchored controls, sets the anchor flag and loads the anchor coords into the controls variables list -RH
{
StringReplace scr, A_Loopfield, `", , All ;"; extra quote in comment stops of syntax hilighting incorretly for the following lines :) - RH
(RegExMatch(scr, "iO)^\s*?Anchor\s*\(?\s*(?P<CtlhWndVar>([\w\+\-\s]+)),\s?(?P<Anchor>(.+))", Rex))
g.Anchor := True
g[hAnchor[rex.ctlhwndvar]].Anchor := SubStr(Rex.Anchor,1,-2)
}
If (RegExMatch(A_LoopField, "iO)^Gui\s?\,?\s?Show,\s?(?P<Options>([\w\s]+)),\s?(?P<Title>(.+))", Rex)) {
Gui %Child%: Show, % Rex.Options, % Rex.Title
g.Window.Title := Rex.Title
}
}
SetWorkingDir %A_ScriptDir%
If (m.Source != "") {
m.Source .= "Gui Menu, MenuBar`n"
Gui %Child%: Menu, MenuBar
}
If (g.ToolbarIcons.MaxIndex()) {
AddToolbar(g.ToolbarIL, g.ToolbarItems, g.ToolbarOptions)
}
g.UpdateSource()
Properties_Reload()
}
Code: Select all
ShowProperties:
If (!WinExist("ahk_id " . hPropWnd)) {
Gui Properties: New, LabelProperties hWndhPropWnd -MinimizeBox Owner%Child%
SetIconEx(hPropWnd, IconLib, 24)
ClassNNList := ""
For Each, Item In g.ControlList {
ClassNNList .= g[Item].ClassNN . "|"
}
Gui Add, Picture, gBlinkControlBorder x12 y13 w16 h16 Icon5, % IconLib
Gui Add, DropDownList, hWndhCbxClassNN vCbxClassNN gOnDropDownChange x40 y12 w203, % "Window||" . ClassNNList
Gui Add, Button, hWndhReloadBtn gReloadSub x248 y11 w23 h23
GuiButtonIcon(hReloadBtn, IconLib, 90, "L1 T1")
Try {
Gui Add, % "Tab3", hWndhPropTab vCurrentTab x6 y41 w270 h303 AltSubmit, General|Options|Window|Events|Script
} Catch {
Gui Add, Tab2, hWndhPropTab vCurrentTab x6 y41 w270 h303 AltSubmit, General|Options|Window|Events|Script
}
Control Style, 0x54010040,, ahk_id %hPropTab%
Gui Tab, 1 ; General
Gui Add, GroupBox, vGrpText x16 y65 w250 h48, Text
Gui Add, Edit, vEdtText x26 y83 w230 h21
Gui Add, GroupBox, x16 y116 w250 h99, Variables
Gui Add, Text, x26 y132 w60 h21 +0x200, hWnd var:
Gui Add, Edit, vEdtHWndVar x95 y132 w160 h21
Gui Add, Text, vTxtVVar x26 y158 w60 h21 +0x200, v-Var:
Gui Add, Edit, vEdtvVar x95 y158 w160 h21
Gui Add, Text, vTxtGLabel x26 y184 w60 h21 +0x200, g-Label:
Gui Add, Edit, vEdtgLabel x95 y184 w160 h21
Gui Add, GroupBox, x16 y217 w250 h74, Position
Gui Add, Text, x57 y232 w16 h22 +0x200, &X:
Gui Add, Edit, vEdtX x75 y232 w50 h21
Gui Add, UpDown, gAdjustPosition Range-65536-65536, 1
Gui Add, Text, x157 y232 w16 h22 +0x200, &Y:
Gui Add, Edit, vEdtY x175 y232 w50 h21
Gui Add, UpDown, gAdjustPosition Range-65536-65536, 1
Gui Add, Text, x57 y260 w16 h22 +0x200, &W:
Gui Add, Edit, vEdtW x75 y260 w50 h21
Gui Add, UpDown, gAdjustPosition Range-65536-65536, 1
Gui Add, Text, x157 y260 w16 h22 +0x200, &H:
Gui Add, Edit, vEdtH x175 y260 w50 h21
Gui Add, UpDown, gAdjustPosition Range-65536-65536, 1
Gui Add, GroupBox, x16 y293 w250 h40, Anchor
Gui Add, CheckBox, vChkAnchorX gRequireHWndVar x75 y305 w40 h23, X
Gui Add, CheckBox, vChkAnchorY gRequireHWndVar x115 y305 w40 h23, Y
Gui Add, CheckBox, vChkAnchorW gRequireHWndVar x155 y305 w40 h23, W
Gui Add, CheckBox, vChkAnchorH gRequireHWndVar x195 y305 w40 h23, H
;Loads these variables if menu is created by Opening GUI from File Menu -RH
If GuiLoaded
{
GuiControl,, EdtText, % g.Window.Title
GuiControl,, EdthwndVar, % g.Window.hwndvar
GuiControl,, EdtgLabel, % g.Window.Label
GuiControl,, EdtvVar, % g.Window.Name
}
Gui Tab, 2 ; Options
Gui Add, GroupBox, vGrpOptions x16 y71 w250 h60, Options
Gui Add, Button, hWndhOptionsBtn x28 y92 w24 h24 gShowContextMenu, ...
GuiButtonIcon(hOptionsBtn, IconLib, 91, "L1 T1")
Gui Add, Edit, vEdtOptions x54 y94 w200 h21
Gui Add, GroupBox, x16 y144 w250 h84, Styles
Gui Add, Text, x33 y165 w60 h23 +0x200, Style:
Gui Add, Edit, hWndhEdtStyle vEdtStyle x96 y166 w82 h22
SendMessage 0x1501, True, "Default",, ahk_id %hEdtStyle%
Gui Add, Button, vBtnChangeStyle gShowStylesDialog x184 y165 w75 h23, Change...
Gui Add, Text, x33 y193 w60 h23 +0x200, Extended:
Gui Add, Edit, hWndhEdtExStyle vEdtExStyle x96 y193 w82 h22
SendMessage 0x1501, True, "Default",, ahk_id %hEdtExStyle%
Gui Add, Button, vBtnChangeExStyle gShowStylesDialog x184 y193 w75 h23, Change...
Gui Add, GroupBox, x16 y242 w250 h84, Font
Gui Add, Edit, vEdtFont x27 y264 w227 h23 Center ReadOnly, Default
Gui Add, Button, gShowFontDialog x180 y296 w75 h23, Change...
Gui Tab, 3 ; Window
Gui Add, CheckBox, vChkResizable x20 y72 w110 h23, Resizable
Gui Add, CheckBox, vChkNoMinimize x20 y97 w110 h24, No minimize button
Gui Add, CheckBox, vChkNoMaximize x20 y122 w111 h22, No maximize button
Gui Add, CheckBox, vChkNoSysMenu x20 y147 w111 h23, No system menu
Gui Add, CheckBox, vChkNoTheme x20 y172 w110 h23, No theme
Gui Add, CheckBox, vChkCenter x140 y72 w120 h23 Checked, Center on screen
Gui Add, CheckBox, vChkAlwaysOnTop x140 y97 w120 h23, Always on top
Gui Add, CheckBox, vChkOwnDialogs x140 y122 w120 h23, Own dialogs
Gui Add, CheckBox, vChkToolWindow x140 y147 w120 h23, Tool window
Gui Add, CheckBox, vChkNoDPIScale x140 y172 w120 h23, No DPI scale
Gui Add, CheckBox, vChkBGColor x20 y200 w111 h24, Background color:
Gui Add, ListView, vColorPreview x140 y201 w20 h20 -Hdr Border, Color
Color := DllCall("GetSysColor", "UInt", 15) ; COLOR_3DFACE
Color := (Color & 0xFF00) + ((Color & 0xFF0000) >> 16) + ((Color & 0xFF) << 16)
GuiControl % "+Background" . ToHex(Color), ColorPreview
Gui Add, Button, gChooseBGColor x168 y199 w90 h23, Change Color
Gui Add, CheckBox, vChkTrayIcon x20 y228 w111 h23, Window/tray icon:
Gui Add, Text, x140 y230 w20 h20 +0x1000
Gui Add, Picture, vTrayIcon gShowIconPath x142 y232 w16 h16 , % A_AhkPath
Gui Add, Button, gChooseTrayIcon x168 y228 w90 h23, Change Icon
;Added to Load Window Variables - RH
If GuiLoaded
{
If (g.Window.Color != "") {
Color := g.Window.Color
GuiControl,, ChkBGColor, 1
GuiControl % "+Background" . ToHex(Color), ColorPreview
}
If (g.Window.Icon != "") {
GuiControl,, ChkTrayIcon, 1
GuiControl,, TrayIcon, % "*Icon" . g.Window.IconIndex . " " . g.Window.Icon
}
svar := g.window.options
IfInString svar, Resize, GuiControl,, ChkResizable, 1
IfInString svar, MinimizeBox, GuiControl,, ChkNoMinimize, 1
IfInString svar, MaximizeBox, GuiControl,, ChkNoMaximize, 1
IfInString svar, SysMenu, GuiControl,, ChkNoSysMenu, 1
IfInString svar, AlwaysOnTop, GuiControl,, ChkAlwaysOnTop, 1
IfInString svar, OwnD, GuiControl,, ChkOwnDialogs, 1
IfInString svar, ToolWindow, GuiControl,, ChkToolWindow, 1
IfInString svar, Theme, GuiControl,, ChkNoTheme, 1
IfInString svar, DPIScale, GuiControl,, ChkNoDPIScale, 1
}
Gui Tab, 4 ; Events
Gui Add, GroupBox, x16 y71 w250 h100, Standard
Gui Add, CheckBox, vChkGuiClose x30 y90 w100 h23 Checked, GuiClose
Gui Add, CheckBox, vChkGuiEscape x30 y115 w100 h23 Checked, GuiEscape
Gui Add, CheckBox, vChkGuiSize x30 y140 w100 h23, GuiSize
Gui Add, CheckBox, vChkGuiContextMenu x140 y90 w100 h23, GuiContextMenu
Gui Add, CheckBox, vChkGuiDropFiles x140 y115 w100 h23, GuiDropFiles
Gui Add, CheckBox, vChkOnClipboardChange x140 y140 w120 h23, OnClipboardChange
;Added to Load Event Variables - RH
If GuiLoaded
{
If g.Window.OnClipboardChange
GuiControl,, ChkOnClipboardChange, 1
If g.Window.GuiClose
GuiControl,, ChkGuiClose, 1
If g.Window.GuiEscape
GuiControl,, ChkGuiEscape, 1
If g.Window.GuiSize
GuiControl,, ChkGuiSize, 1
If g.Window.GuiContextMenu
GuiControl,, ChkGuiContextMenu, 1
If g.Window.GuiDropFiles
GuiControl,, ChkGuiDropFiles, 1
}
Gui Tab, 5 ; Script
Gui Add, CheckBox, vChkNoEnv x20 y72 w113 h23 Checked, #NoEnv
Gui Add, CheckBox, vChkNoTrayIcon x140 y72 w113 h23, #NoTrayIcon
Gui Add, CheckBox, vChkWarn x20 y98 w113 h23 Checked, #Warn
Gui Add, DropDownList, vCbxWarn x140 y98 w120, Off|MsgBox||StdOut|OutputDebug
Gui Add, CheckBox, vChkSingleInstance x20 y126 w113 h23, #SingleInstance
Gui Add, DropDownList, vCbxSingleInstance x140 y124 w120, Force||Ignore|Off
Gui Add, CheckBox, vChkSetWorkingDir x20 y153 w113 h23 Checked, SetWorkingDir
Gui Add, Edit, vEdtSetWorkingDir x140 y153 w121 h21, `%A_ScriptDir`%
Gui Add, CheckBox, vChkSendMode x20 y179 w113 h23, SendMode
Gui Add, DropDownList, vCbxSendMode x140 y180 w120, Input||Play|Event|InputThenPlay
Gui Add, CheckBox, vChkSetBatchLines x20 y207 w113 h23, SetBatchLines -1
Gui Add, CheckBox, vChkPersistent x20 y233 w113 h23, #Persistent
Gui Add, CheckBox, vChkListLines x140 y207 w113 h23, ListLines Off
Gui Add, CheckBox, vChkIgnoreMenuErrors x140 y233 w113 h23, Ignore Menu Errors
;Added to Load Script Variables - RH
If GuiLoaded
{
If (Script.NoEnv != 0)
GuiControl,, ChkNoEnv, 1
Else
GuiControl,, ChkNoEnv, 0
If (Script.NoTrayIcon != 0)
GuiControl,, ChkNoTrayIcon, 1
If (Script.Warn != "") {
GuiControl,, ChkWarn, 1
GuiControl ChooseString, CbxWarn, % Script.Warn
}
Else
GuiControl,, ChkWarn, 0
If (Script.SingleInstance != "") {
GuiControl,, ChkSingleInstance, 1
GuiControl ChooseString, CbxSingleInstance, % Script.SingleInstance
}
Else
GuiControl,, ChkSingleInstance, 0
If (Script.WorkingDir != "") {
GuiControl,, ChkSetWorkingDir, 1
GuiControl,, EdtSetWorkingDir, % Script.WorkingDir
}
Else
GuiControl,, ChkSetWorkingDir, 0
If (Script.SendMode != "") {
GuiControl,, ChkSendMode, 1
GuiControl ChooseString, CbxSendMode, % Script.SendMode
}
Else
GuiControl,, ChkSendMode, 0
If (Script.NoBatchLines != 0)
GuiControl,, ChkSetBatchLines, 1
If (Script.Persistent != 0)
GuiControl,, ChkPersistent, 1
If (Script.ListLinesOff != 0)
GuiControl,, ChkListLines, 1
If (Script.IgnoreMenuErrors != 0)
GuiControl,, ChkIgnoreMenuErrors, 1
}
Gui Tab
GuiLoaded := 0 ;Resets to option to defaults for the next file thats opened - RH
Gui Add, Button, hWndhHelpBtn x8 y350 w24 h23 gMenuHandler
GuiButtonIcon(hHelpBtn, "shell32.dll", 24, "L1 T1")
Gui Add, Button, gReset x38 y350 w75 h23, Reset
Gui Add, Button, gPropertiesClose x118 y350 w75 h23, Close
Gui Add, Button, gApplyProperties x198 y350 w75 h23 Default, Apply
WinGetPos ax, ay, aw,, ahk_id %hAutoWnd%
IniRead px, %IniFile%, Properties, x, % (ax + aw - 281 - 40)
IniRead py, %IniFile%, Properties, y, % ay + 40
Gui Properties: Show, % "x" . px . " y" . py . " w281 h382", Properties
} Else {
NA := (A_ThisFunc != "" && A_ThisFunc != "OnToolbar") ? "NA" : ""
Gui Properties: Show, % NA
}
ClassNN := g[g_Control].ClassNN
If (ClassNN = "") {
ClassNN := "Window"
}
GuiControl Properties: ChooseString, CbxClassNN, % ClassNN
Gosub OnDropDownChange
Return