Below is another nice example of a class gui.
The purpose is to have a Gui is a class that when typing in Control Edit_1, it copies the value of Edit_1 to Edit_3.
The purpose is to have an interactive Gui, if you click or modify some values, it will autofill some other controls to help the user.
A question, is it possible to Set the controlclass of a control?
if you use "Gui, Add, Edit" it sets the ClassNN always as Edit1, Edit2, Edit3, I would prefer more meaningfull names.
This way I would not need to save the hwnd's of the controls in an array (arrHandles) to acces them later.
Code: Select all
#NoEnv
#SingleInstance, Force
; Modification of examples of post https://www.autohotkey.com/boards/viewtopic.php?t=55952&start=20
MainGui := new GUI("Main", "Two Buttons ...", "+AlwaysOnTop")
MainGui.Show(50, 50)
return
Esc:: ; end of auto-execute section
ExitApp
;===============================================================================
class GUI { ; object oriented GUI structure
;===============================================================================
__New(Name, WinTitle := "", Options := ""){ ; constructor
static
this.arrHandles := {}
this.Name := Name
Gui, % this.Name ":New", HWNDhGui, %WinTitle%
Gui, +Labelmy +Resize %Options%
Gui, Font, s16
This.Add("Edit", "Edit_1", "w250", "Edit_onTyping", "Hello, World!")
This.Add("Button", "Button_1", "wp hp","Button_onClick", "1")
This.Add("Button", "Button_2", "wp hp","Button_onClick", "2")
This.Add("Edit", "Edit_3", "w250", "Edit_onTyping")
This.Add("Edit", "Edit_4", "w250", "Edit_onTyping")
; house keeping
this.Handle := hGui
this.WinTitle := WinTitle
OnMessage(0x05, ObjBindMethod(this, "GuiSize")) ; WM_SIZE
}
__Delete(){
Gui, % this.Name ":Destroy"
}
Show(x := "Center", y := "Center"){
Gui, % this.Name ":Show", x%x% y%y%
}
GuiSize(wParam, lParam, msg, hwnd){ ; resize events
if this.Handle != hwnd
return
/* wParam = "event info"
0: The window has been restored or dragged by its edges.
1: The window has been minimized.
2: The window has been maximized.
*/
; lParam = (width and height)
w := lParam & 0xFFFF ; low word (bits 16..32)
h := lParam >> 16 ; high word (bits 1..15)
ToolTip, Resizing ...`n%w%`n%h%`n%wParam%
}
Edit_onTyping(hwnd, GuiEvent, EventInfo){
if (A_GuiControl="Edit_1"){
Tooltip, % "Trying to write @" outVar "@ in control Edit_3 on " this.Name
hEdit_3 := this.arrHandles["Edit_3"]
GuiControlGet, outVar,, %hwnd%
GuiControl,, %hEdit_3%, %outVar%
}
else{
GuiControlGet, outVar,, %hwnd%
Tooltip, % "You are typing @" outVar "@ in Edit box on " this.Name " - " A_GuiControl
}
}
Button_onClick(){
Tooltip, % "You pressed button " A_GuiControl " on " this.Name
}
Add(ControlType:="Edit", Name_Control:="Edit1", Options:="", Function:="Edit_onTyping", Value:=""){
static
Gui, Add, %ControlType%, HWNDh%Name_Control% v%Name_Control% %Options%,%Value%
Handle_Control := h%Name_Control%
this.arrHandles[Name_Control]:=Handle_Control
ControlHandler := ObjBindMethod(this, Function)
GuiControl +g, %Handle_Control%, %ControlHandler%
}
GuiControlGet(cmd := "", value := ""){
GuiControlGet, ov, %cmd%, % this.Handle, % value
return ov
}
myClose(){ ; {Alt+F4} pressed, [X] clicked
;myclose: ;if any "myclose:" label is used outside this "class", a "duplicated" label found msgbox shows up!
This.Delete()
return
}
} ; end of class