attempt to wrap gui controls in object
Requires options.ahkl
Requires gui.ahkl
version .1a
minimal testing done
lots of code cleanup to do
Test Script:
Code:
Type := "Edit"
Gui1 := Gui()
o := Control(Gui1,Type)
o.v := "MyEdit1"
o.g := "MyEdit1Label"
o.w := 300
o.h := 100
o.Text := "Some Text"
;o.AltSubmit := "True"
;o.Create()
Gui1.Show()
Return
Esc::ExitApp
MyEdit1Label:
MsgBox % MyEdit1
Code:
; control.ahkl
; requires AutoHotkey_L
; by ahklerner
;version .1 alpha
; This object handles a gui's controls
; intended use is by gui object... but can be modified to suit other purposes
;
Control_GetVal(obj, name){
if name = hWnd
{
text := obj[name]
;MsgBox % text
VarSetCapacity(text, -1)
return text ? text : " "
}
if name = HasBeenCreated
return obj.HasBeenCreated
;MsgBox % "GetVal: " . name
return obj.Options[name]
}
Control_SetVal(obj, name, val){
if name = hWnd
obj.hWnd := val
else if name = HasBeenCreated
obj.HasBeenCreated := val
else if name = Text
obj.Text := val
else
obj.Options[name]:= val
if name = v
obj.Parent[val] := obj
}
Control_Delete(obj){
MsgBox "Delete"
}
Control_GetClassNN(hWndWindow,hWndControl){
DetectHiddenWindows, On
WinGet, ClassNNList, ControlList, ahk_id %hWndWindow%
Loop, PARSE, ClassNNList, `n
{
ControlGet, hwnd, hwnd,,%A_LoopField%,ahk_id %hWndWindow%
if (hWnd = hWndControl)
return A_LoopField
}
}
Control_SetText(obj,NewText){
if (obj.HasBeenCreated = True) {
hWnd := obj.hWnd
GuiNum := obj.Parent.GuiNum
controlvarname := obj.v
controlvarname = %controlvarname%
if controlvarname
GuiControl, %GuiNum%:, %controlvarname%, %NewText%
else
ControlSetText,, %NewText%,ahk_id %hWnd%
}
obj.Text := NewText
}
Control_GetText(obj,NewText){
if (obj.HasBeenCreated = True) {
hWnd := obj.hWnd
ControlGetText, out,,ahk_id %hWnd%
obj.Text := out
}
return obj.Text
}
Control_Call(obj, func, prm0="¬",prm1="",prm2="",prm3="",prm4="",prm5=""){
loop 6
num := A_Index - 1, VarSetCapacity(prm%num%, -1)
if (func = "Create"){
;MsgBox % obj.Text
Control_Create(obj,obj.Type,obj.Options.ToParam(),obj.Text)
obj.HasBeenCreated := True
}else if (func = "Text"){
prm0 = %prm0%
;GuiNum := obj.Parent.GuiNum
if (prm0 != "¬"){
hWnd := obj.hWnd
ControlSetText,, %prm0%,ahk_id %hWnd%
obj.Text := prm0
}else{
hWnd := obj.hWnd
ControlGetText, out,,ahk_id %hWnd%
obj.Text := out
return out
}
}
}
Control_Create(obj,Type,Opts="",Text=""){
Global
Local GuiNum, hWnd
;MsgBox Create Called
Type = %Type%
Opts = %Opts%
Text = %Text%
GuiNum := obj.Parent.GuiNum
Gui, %GuiNum%:Add, %Type%, %Opts% hWndhWndControl, %Text%
obj.hWnd := hWndControl
;MsgBox % "hWndControl: " . hWndControl
hWndWindow := obj.Parent.hWnd
;MsgBox % "hWndWindow: " . hWndWindow
ClassNN := Control_GetClassNN(hWndWindow,hWndControl)
ClassNN = %ClassNN%
; MsgBox % "New Control ClassNN: " . ClassNN
obj.Parent[ClassNN] := obj
}
Control(Parent,Type){
static COBase
;Local AddCount
If !COBase
COBase := Object( "__Get" , "Control_GetVal" ; default getter
, "__Set" , "Control_SetVal" ; default setter
, "GetText" , "Control_GetText" ; .GetText()
, "SetText" , "Control_SetText" ; .SetText(NewText)
; , "Text" , "Control_Text" ; .Text([NewText])
; , "__Delete" , "Control_Delete"
, "__Call" , "Control_Call")
;MsgBox % "Control():Type " . Type
AddCount := Parent.ControlCount
VarSetCapacity(AddCount, -1)
if !AddCount
AddCount = 0
AddCount++
Control := Object("Type", Type, "Parent", parent,"HasBeenCreated", false, "Index", AddCount, "hWnd", " ", "Text", " ", "Options", opt := options(), "base", COBase)
opt.Parent := Control
Control.Parent.ControlCount := AddCount
Control.Parent[AddCount] := Control
Control.Parent[AddCount] := Control
return Control
}