Skip to content

Instantly share code, notes, and snippets.

@BanditTech
Last active February 9, 2021 11:15
Show Gist options
  • Save BanditTech/54ebfa2bd3bd7a1853fc809c684ca796 to your computer and use it in GitHub Desktop.
Save BanditTech/54ebfa2bd3bd7a1853fc809c684ca796 to your computer and use it in GitHub Desktop.
Class Gui {
; Static Lists := {HWND:{},Setting:{}}
Static Windows := {}
; Windows { HWND { {Name,Label,Options,Colors,Font,Location,OnMessages} } }
Static Elements := {}
; Elements { HWND { {Label,Value,Options,GuiName,Tooltip,Colors,Font,Hover,Binding} } }
Static Tooltips := {}
Static ActiveGUI := 1
Static TooltipSuspended := 0
Static initfamily := "Segoe UI"
Static initsize := "10"
Static initcolor := "Black"
New(Name, Title="", Options="", Colors:="",Font:=""){
global
local TempHWND
try {
This.Destroy(Name)
Gui, %Name%:New,% Options " hwndTempHWND",% Title
This.InitWindow(TempHWND,Name,Title,Options,Colors,Font)
This.SetDefault(Name)
If (This.Windows[TempHWND].Colors.1 || This.Windows[TempHWND].Colors.2)
This.GuiColor(This.Windows[TempHWND].Colors.1,This.Windows[TempHWND].Colors.2)
If IsObject(Font)
This.InitFont(Font.Family,Font.Size,Font.Color)
Else
This.InitFont()
This.BindMethodOnMessage(This,0x200,"MouseMove") ; WM_MOUSEMOVE
This.BindMethodOnMessage(This,0x2A2,"MouseMove") ; WM_NCMOUSELEAVE
} catch e {
msg := "Failed creating new with params:"
. "`nname: " name
. "`ntitle: " title
. "`noptions: " Options
. "`nColor: " Colors ~= ".+" ? Colors : IsObject(Colors) ? Colors.1 " " Colors.2
. "`nAdditional informations:"
. "`nwhat:" e.what
. "`nfile: " e.file
. "`nline: " e.line
. "`nmessage: " e.message
. "`nextra: " e.extra
Logger.addLogEntry("GUI: " msg)
MsgBox,% 4096+16, %A_ScriptName%,% msg
}
}
InitWindow(HWND,Name,Title,Options:="",Colors:="",Font:=""){
This.Windows[HWND] := {}
This.Windows[HWND].Name := Name
This.Windows[HWND].Label := Name
This.Windows[HWND].Title := Title
This.Windows[HWND].Options := Options
This.Windows[HWND].Colors := IsObject(Colors) ? Colors
: Colors!="" && Colors ~= ".+ .+" ? StrSplit(Trim(Colors)," ",,2)
: Colors!="" && Colors ~= " .+" ? ["",Trim(Colors)]
: Colors!="" && Colors ~= ".+" ? [Trim(Colors),""] : ["",""]
This.Windows[HWND].Location := {}
This.Windows[HWND].OnMessages := {}
This.Windows[HWND].Font := IsObject(Font) ? Font : This.FontInit(,,,True)
}
Add(type,L:="Label",Value:="",Options:="",TT:="",Colors:="",Font:="",ImgBtnStyle:=""){
static Colors_Default := [ "Gray" ; background
,"Black" ] ; content
static Font_Default := { family:"Segoe UI" ; Font settings
, size:"10"
, color:"Black" }
static ImgBtnSetObj_Default := [ [0, 0x274554, , 0xebebeb, , , 0xd6d6d6] ; normal
, [ , 0x355e73] ; hover
, [ , 0xffffff] ] ; press
local TempHWND, msg
; Add gui control
try {
If IsObject(Font)
This.FontSet(Font.family,Font.size,Font.color)
Else
This.FontSet()
Gui, Add,% type="ImageButton"?"Button":type,% Options " hwndTempHWND" ,% Value
This.InitElement(TempHWND,L,Value,Options,This.ActiveGUI,TT,Colors,Font)
If (This.Elements[TempHWND].Colors.1 || This.Elements[TempHWND].Colors.2)
CtlColors.Attach(TempHWND, This.Elements[TempHWND].Colors.1, This.Elements[TempHWND].Colors.2)
if (type = "ImageButton") {
if !ImageButton.Create(TempHWND, IsObject(ImgBtnStyle) ? ImgBtnStyle : ImgBtnSetObj_Default)
MsgBox % "GUI: """ This.ActiveGUI """"
. "`n" "Error: """ (ImageButton.LastError ? ImageButton.LastError : "Something Else") """"
. "`n`n" "Control: """ TempHWND """ - Control Handle: """ This.Elements[TempHWND].GuiHWND """"
. "`n" "Options: """ Options """"
. "`n" "ImageButton Style: """ JSON.Dump(IsObject(ImgBtnStyle) ? ImgBtnStyle : ImgBtnSetObj_Default) """"
. "`n`n"
}
Return TempHWND
} catch e {
msg := "Failed to create control.`n"
. "`nMessage: " (e.Message ? e.Message : "")
. "`nExtra: " (e.Extra ? e.Extra : "") "`n"
. "`nGUI Label: " (L ? L : "")
. "`nGUI HWND: " (TempHWND ? TempHWND : "")
. "`nControl type: " (type ? type : "")
. "`nOptions: " (Options ? Options : "")
. "`nContent: " (Value ? Value : "")
. "`nCtlColor: " (Colors ~= ".+" ? Colors : IsObject(Colors) ? Colors.1 " " Colors.2 : "")
. "`nFont: " (IsObject(Font) ? Font.family " " Font.size " " Font.color : This.initfamily " " This.initsize " " This.initcolor)
If !msg
msg := "Something horrible happened"
Logger.addLogEntry("GUI: " msg)
MsgBox,% 4096+16, %A_ScriptName%,% msg
}
}
InitElement(HWND,Label,Value:="",Options:="",GuiName:="",ToolT:="",Colors:="",Font:=""){
This.Elements[HWND] := {}
This.Elements[HWND].GuiHWND := DllCall("GetParent", "UInt", HWND)
This.Elements[HWND].Label := Label
This.Elements[HWND].Value := Value
This.Elements[HWND].Options := Options
This.Elements[HWND].GuiName := GuiName
This.Elements[HWND].Tooltip := ToolT
This.Elements[HWND].Colors := IsObject(Colors) ? Colors
: Colors!="" && Colors ~= ".+ .+" ? StrSplit(Trim(Colors)," ",,2)
: Colors!="" && Colors ~= " .+" ? ["",Trim(Colors)]
: Colors!="" && Colors ~= ".+" ? [Trim(Colors),""] : ["",""]
This.Elements[HWND].Hover := ""
This.Elements[HWND].Binding := {}
If IsObject(Font)
This.FontSet(Font.family,Font.size,Font.color,HWND)
Else {
This.FontSet(This.initfamily,This.initsize,This.initcolor,HWND)
}
}
SetHover(HWND,family:="",size:="",color:="",text:=""){
If !IsObject(This.Elements[HWND].Hover) {
family := family ? family : This.initfamily
size := size ? size : This.initsize
color := color ? color : This.initcolor
This.Elements[HWND].Hover := {family:family, size:size, color:color, text:text}
} Else {
If font
This.Elements[HWND].Hover.family := family
If size
This.Elements[HWND].Hover.size := size
If color
This.Elements[HWND].Hover.color := color
If text
This.Elements[HWND].Hover.text := text
}
}
Show(name:="", opts="", title="") {
local msg
try {
if name
Gui, %name%:Show, %opts%, %title%
Else
Gui, Show, %opts%, %title%
}
catch e {
msg := "Failed to show with params:"
. "`nname: " name
. "`nopts: " opts
. "`ntitle: " title
. "`nAdditional informations:"
. "`nwhat:" e.what
. "`nfile: " e.file
. "`nline: " e.line
. "`nmessage: " e.message
. "`nextra: " e.extra
Logger.addLogEntry("GUI: " msg)
MsgBox,% 4096+16, %A_ScriptName%,% msg
}
}
Destroy(name){
local msgID
Gui, %name%:Hide
for msgID in This.Windows[This.GetHWND(name)].OnMessages
OnMessage(msgID, This.Windows[This.GetHWND(name)].OnMessages[msgID], 0)
This.Windows.Remove(This.GetHWND(name))
Gui, %name%:Destroy
}
SetDefault(name) {
global
Gui,%name%:Default
This.ActiveGUI := name
}
Color(HWND,BkColor:="",TxColor:=""){
CtlColors.Change(HWND,BkColor,TxColor)
}
GuiColor(_winColor="", _ctrlColor="") {
global
if (_winColor != "" && _ctrlColor="")
Gui,% This.ActiveGUI ":Color", %_winColor%
else if (_winColor = "" && _ctrlColor != "")
Gui,% This.ActiveGUI ":Color", , %_ctrlColor%
else if (_winColor != "" && _ctrlColor != "")
Gui,% This.ActiveGUI ":Color", %_winColor%, %_ctrlColor%
If (_winColor!="")
This.Windows[This.GetHWND(This.ActiveGUI)].Colors.1 := _winColor
If (_ctrlColor!="")
This.Windows[This.GetHWND(This.ActiveGUI)].Colors.2 := _ctrlColor
}
Tooltip(HWND){
global
DetectHiddenWindows, On
If WinExist("ahk_class AutoHotkeyGUI") {
If (This.Elements.HasKey(HWND) && WinExist("ahk_id " HWND ))
ToolTip, % This.Elements[Format("{:#x}", HWND)].Tooltip
Else
Tooltip,
}
else
ToolTip,
DetectHiddenWindows, Off
return
}
TooltipOff(){
tooltip,
}
TooltipUpdate(HWND, TT:=""){
This.Elements[HWND].Tooltip := TT
}
TooltipToggle(bool:=""){
if (bool != "" && (bool == 0 || bool == 1))
This.TooltipSuspended := bool
else
This.TooltipSuspended := !This.TooltipSuspended
}
MouseMove(){
local _TT, font, size, color
static PrevTTControl := "", DisplayedTTTime := "", tooltipOff := "", ChangedHWND:=""
if !tooltipOff
tooltipOff := ObjBindMethod(this,"TooltipOff")
MouseGetPos,,,, _TT, 2
If !This.Elements.HasKey(_TT) && This.Elements.HasKey(DllCall("GetParent", "UInt", _TT))
_TT := DllCall("GetParent", "UInt", _TT)
If (This.Elements.HasKey(_TT) && IsObject(This.Elements[_TT].Hover)) {
If !ChangedHWND {
font := This.Elements[_TT].Hover.family
size := This.Elements[_TT].Hover.size
color := This.Elements[_TT].Hover.color
Gui, Font, c%color% s%size%, %font%
GuiControl, Font, %_TT%
If This.Elements[_TT].Hover.text != ""
GuiControl, , %_TT%, % This.Elements[_TT].Hover.text
ChangedHWND := _TT
}
} Else If ChangedHWND {
font := This.Elements[ChangedHWND].Font.family
size := This.Elements[ChangedHWND].Font.size
color := This.Elements[ChangedHWND].Font.color
Gui, Font, c%color% s%size%, %font%
GuiControl, Font, %ChangedHwnd%
GuiControl, , %ChangedHwnd%, % This.Elements[ChangedHwnd].Value
ChangedHWND := ""
}
If this.TooltipSuspended
Return
if (PrevTTControl!=_TT)
{
PrevTTControl:=_TT
; MsgBox % "prev " PrevTTControl " This " _TT
If (_TT) {
Gui.Tooltip(_TT)
SetTimer,% TooltipOff, -5000
DisplayedTTTime := A_TickCount
} Else {
Tooltip
}
} Else {
if (A_TickCount - DisplayedTTTime >= 5000)
Tooltip,
Else{
Gui.Tooltip(_TT)
SetTimer,% TooltipOff, -5000
}
}
return
}
SetControl(HWND,Value,SubCommand:="",Update:=1){
Global
local msg
try {
if SubCommand
GuiControl,% SubCommand,% HWND ,% Value
else
GuiControl,,% HWND ,% Value
if Update
This.Elements[HWND].Value := Value
} catch e {
msg := "Failed to set control.`n"
. "`nMessage: " e.Message
. "`nExtra: " e.Extra "`n"
. "`nGUI HWND: " HWND
. "`nGUI Label: " This.Lists.HWND[HWND]
. "`nSub Command: " SubCommand
. "`nContent: " Value
Icarus.Log(msg,"GUI: ")
MsgBox,% 4096+16, %A_ScriptName%,% msg
}
}
GetValue(HWND){
If This.Elements.HasKey(HWND)
Return This.Elements[HWND].Value
Return False
}
GetControlValue(HWND,Update:=1){
local Value
If This.Elements.HasKey(HWND){
GuiControlGet, Value, ,% HWND
If Update
This.Elements[HWND].Value := Value
Return Value
}
Return False
}
GetLabel(HWND){
If This.Elements.HasKey(HWND)
Return This.Elements[HWND].Label
Return False
}
GetHWND(L:="Label"){
local HWND, Element
for HWND, Element in This.Elements {
If (Element.Label = L)
Return HWND
}
for HWND, Element in This.Windows {
If (Element.Label = L || Element.Name = L)
Return HWND
}
Return False
}
GetControlPos(HWND) {
local ctrlPos, ctrlPosX, ctrlPosY, ctrlPosW, ctrlPosH
GuiControlGet, ctrlPos , Pos,% HWND
return {X:ctrlPosX,Y:ctrlPosY,W:ctrlPosW,H:ctrlPosH}
}
MoveControl(HWND,opts:=""){
Global
GuiControl, Move,% HWND,% opts
}
BindFunctionOnMessage(guiClass, msgID, funcName, params*) {
global
This.Windows[This.GetHWND(This.ActiveGUI)].OnMessages[msgID] := params.Count() ? %guiClass%[funcName].Bind(guiClass, params*) : %guiClass%[funcName].Bind(guiClass)
OnMessage(msgID, This.Windows[This.GetHWND(This.ActiveGUI)].OnMessages[msgID])
}
BindMethodOnMessage(guiClass, msgID, funcName, params*) {
global
This.Windows[This.GetHWND(This.ActiveGUI)].OnMessages[msgID] := params.Count() ? ObjBindMethod(guiClass, funcName, params*) : ObjBindMethod(guiClass, funcName)
OnMessage(msgID, This.Windows[This.GetHWND(This.ActiveGUI)].OnMessages[msgID])
}
BindFunctionToControl(HWND, funcName, params*) {
global
local __f
if ( params.Count() ) {
__f := Func(funcName).Bind(params*)
This.Elements[HWND].Binding := {Function: funcName, Bind:__f, Params: [params*]}
}
else {
__f := Func(funcName).Bind()
This.Elements[HWND].Binding := {Function: funcName, Bind:__f}
}
GuiControl, +g,% HWND,% __f
}
BindMethodToControl(guiClass, HWND, funcName, params*) {
global
local __f
__f := params.Count() ? ObjBindMethod(guiClass, funcName, params*) : ObjBindMethod(guiClass, funcName)
This.Elements[HWND].Binding := params.Count() ? {Method: funcName, Bind:__f, Params: [params*]} : {Method: funcName, Bind:__f}
GuiControl, +g,% HWND,% __f
}
DisableControlFunction(HWND) {
global
GuiControl, -g,% HWND
}
EnableControlFunction(HWND) {
global
local __f
if IsObject(This.Elements[HWND].Binding.Bind) {
__f := This.Elements[HWND]Binding.Bind
GuiControl, +g,% HWND,% __f
}
}
FontInit(family:="",size:="",color:="",ret:=False){
if family
This.initfamily := family
if size
This.initsize := size
if color
This.initcolor := color
if !ret
Gui, Font,% "c" This.initcolor " s" This.initsize, % This.initfamily ; initial text-settings
Else
Return {family:This.initfamily, size:This.initsize, color:This.initcolor}
}
FontSet(family:="",size:="",color:="",HWND:=""){
family := family ? family : This.initfamily
size := size ? size : This.initsize
color := color ? color : This.initcolor
If HWND
This.Elements[HWND].Font := {family:family, size:size, color:color}
Else
Gui, Font, c%color% s%size%, %family% ; set new text-settings
}
}
UseGDIP(Params*) { ; Loads and initializes the Gdiplus.dll at load-time
; GET_MODULE_HANDLE_EX_FLAG_PIN = 0x00000001
Static GdipObject := ""
, GdipModule := ""
, GdipToken := ""
Static OnLoad := UseGDIP()
If (GdipModule = "") {
If !DllCall("LoadLibrary", "Str", "Gdiplus.dll", "UPtr")
UseGDIP_Error("The Gdiplus.dll could not be loaded!`n`nThe program will exit!")
If !DllCall("GetModuleHandleEx", "UInt", 0x00000001, "Str", "Gdiplus.dll", "PtrP", GdipModule, "UInt")
UseGDIP_Error("The Gdiplus.dll could not be loaded!`n`nThe program will exit!")
VarSetCapacity(SI, 24, 0), NumPut(1, SI, 0, "UInt") ; size of 64-bit structure
If DllCall("Gdiplus.dll\GdiplusStartup", "PtrP", GdipToken, "Ptr", &SI, "Ptr", 0)
UseGDIP_Error("GDI+ could not be startet!`n`nThe program will exit!")
GdipObject := {Base: {__Delete: Func("UseGDIP").Bind(GdipModule, GdipToken)}}
}
Else If (Params[1] = GdipModule) && (Params[2] = GdipToken)
DllCall("Gdiplus.dll\GdiplusShutdown", "Ptr", GdipToken)
}
UseGDIP_Error(ErrorMsg) {
MsgBox, 262160, UseGDIP, %ErrorMsg%
ExitApp
}
Class Gui {
; Static Lists := {HWND:{},Setting:{}}
Static Windows := {}
; Windows { HWND { {Name,Label,Options,Colors,Font,Location,OnMessages} } }
Static Elements := {}
; Elements { HWND { {Label,Value,Options,GuiName,Tooltip,Colors,Font,Hover,Binding} } }
Static Tooltips := {}
Static ActiveGUI := 1
Static TooltipSuspended := 0
Static initfamily := "Segoe UI"
Static initsize := "10"
Static initcolor := "Black"
New(Name, Title="", Options="", Colors:="",Font:=""){
global
local TempHWND
try {
This.Destroy(Name)
Gui, %Name%:New,% Options " hwndTempHWND",% Title
This.InitWindow(TempHWND,Name,Title,Options,Colors,Font)
This.SetDefault(Name)
If (This.Windows[TempHWND].Colors.1 || This.Windows[TempHWND].Colors.2)
This.GuiColor(This.Windows[TempHWND].Colors.1,This.Windows[TempHWND].Colors.2)
If IsObject(Font)
This.InitFont(Font.Family,Font.Size,Font.Color)
Else
This.InitFont()
This.BindMethodOnMessage(This,0x200,"MouseMove") ; WM_MOUSEMOVE
This.BindMethodOnMessage(This,0x2A2,"MouseMove") ; WM_NCMOUSELEAVE
} catch e {
msg := "Failed creating new with params:"
. "`nname: " name
. "`ntitle: " title
. "`noptions: " Options
. "`nColor: " Colors ~= ".+" ? Colors : IsObject(Colors) ? Colors.1 " " Colors.2
. "`nAdditional informations:"
. "`nwhat:" e.what
. "`nfile: " e.file
. "`nline: " e.line
. "`nmessage: " e.message
. "`nextra: " e.extra
Logger.addLogEntry("GUI: " msg)
MsgBox,% 4096+16, %A_ScriptName%,% msg
}
}
InitWindow(HWND,Name,Title,Options:="",Colors:="",Font:=""){
This.Windows[HWND] := {}
This.Windows[HWND].Name := Name
This.Windows[HWND].Label := Name
This.Windows[HWND].Title := Title
This.Windows[HWND].Options := Options
This.Windows[HWND].Colors := IsObject(Colors) ? Colors
: Colors!="" && Colors ~= ".+ .+" ? StrSplit(Trim(Colors)," ",,2)
: Colors!="" && Colors ~= " .+" ? ["",Trim(Colors)]
: Colors!="" && Colors ~= ".+" ? [Trim(Colors),""] : ["",""]
This.Windows[HWND].Location := {}
This.Windows[HWND].OnMessages := {}
This.Windows[HWND].Font := IsObject(Font) ? Font : This.FontInit(,,,True)
}
Add(type,L:="Label",Value:="",Options:="",TT:="",Colors:="",Font:="",ImgBtnStyle:=""){
static Colors_Default := [ "Gray" ; background
,"Black" ] ; content
static Font_Default := { family:"Segoe UI" ; Font settings
, size:"10"
, color:"Black" }
static ImgBtnSetObj_Default := [ [0, "0x274554", "", "0xebebeb", , , "0xd6d6d6"] ; normal
, [0, "0x355e73"] ; hover
, [0, "0x122630"] ] ; press
local TempHWND, msg
; Add gui control
try {
If IsObject(Font)
This.FontSet(Font.family,Font.size,Font.color)
Else
This.FontSet()
Gui, Add,% type="ImageButton"?"Button":type,% Options " hwndTempHWND" ,% Value
This.InitElement(TempHWND,L,Value,Options,This.ActiveGUI,TT,Colors,Font)
If (This.Elements[TempHWND].Colors.1 || This.Elements[TempHWND].Colors.2)
CtlColors.Attach(TempHWND, This.Elements[TempHWND].Colors.1, This.Elements[TempHWND].Colors.2)
if (type = "ImageButton") {
if !ImageButton.Create(This.Elements[TempHWND].GuiHWND, IsObject(ImgBtnStyle) ? ImgBtnStyle : ImgBtnSetObj_Default)
MsgBox % "GUI: """ This.ActiveGUI """"
. "`n" "Error: """ (ImageButton.LastError ? ImageButton.LastError : "Something Else") """"
. "`n`n" "Control: """ TempHWND """ - Control Handle: """ This.Elements[TempHWND].GuiHWND """"
. "`n" "Options: """ Options """"
. "`n" "ImageButton Style: """ JSON.Dump(IsObject(ImgBtnStyle) ? ImgBtnStyle : ImgBtnSetObj_Default) """"
. "`n`n"
}
Return TempHWND
} catch e {
msg := "Failed to create control.`n"
. "`nMessage: " (e.Message ? e.Message : "")
. "`nExtra: " (e.Extra ? e.Extra : "") "`n"
. "`nGUI Label: " (L ? L : "")
. "`nGUI HWND: " (TempHWND ? TempHWND : "")
. "`nControl type: " (type ? type : "")
. "`nOptions: " (Options ? Options : "")
. "`nContent: " (Value ? Value : "")
. "`nCtlColor: " (Colors ~= ".+" ? Colors : IsObject(Colors) ? Colors.1 " " Colors.2 : "")
. "`nFont: " (IsObject(Font) ? Font.family " " Font.size " " Font.color : This.initfamily " " This.initsize " " This.initcolor)
If !msg
msg := "Something horrible happened"
Logger.addLogEntry("GUI: " msg)
MsgBox,% 4096+16, %A_ScriptName%,% msg
}
}
InitElement(HWND,Label,Value:="",Options:="",GuiName:="",ToolT:="",Colors:="",Font:=""){
This.Elements[HWND] := {}
This.Elements[HWND].GuiHWND := DllCall("GetParent", "UInt", HWND)
This.Elements[HWND].Label := Label
This.Elements[HWND].Value := Value
This.Elements[HWND].Options := Options
This.Elements[HWND].GuiName := GuiName
This.Elements[HWND].Tooltip := ToolT
This.Elements[HWND].Colors := IsObject(Colors) ? Colors
: Colors!="" && Colors ~= ".+ .+" ? StrSplit(Trim(Colors)," ",,2)
: Colors!="" && Colors ~= " .+" ? ["",Trim(Colors)]
: Colors!="" && Colors ~= ".+" ? [Trim(Colors),""] : ["",""]
This.Elements[HWND].Hover := ""
This.Elements[HWND].Binding := {}
If IsObject(Font)
This.FontSet(Font.family,Font.size,Font.color,HWND)
Else {
This.FontSet(This.initfamily,This.initsize,This.initcolor,HWND)
}
}
SetHover(HWND,family:="",size:="",color:="",text:=""){
If !IsObject(This.Elements[HWND].Hover) {
family := family ? family : This.initfamily
size := size ? size : This.initsize
color := color ? color : This.initcolor
This.Elements[HWND].Hover := {family:family, size:size, color:color, text:text}
} Else {
If font
This.Elements[HWND].Hover.family := family
If size
This.Elements[HWND].Hover.size := size
If color
This.Elements[HWND].Hover.color := color
If text
This.Elements[HWND].Hover.text := text
}
}
Show(name:="", opts="", title="") {
local msg
try {
if name
Gui, %name%:Show, %opts%, %title%
Else
Gui, Show, %opts%, %title%
}
catch e {
msg := "Failed to show with params:"
. "`nname: " name
. "`nopts: " opts
. "`ntitle: " title
. "`nAdditional informations:"
. "`nwhat:" e.what
. "`nfile: " e.file
. "`nline: " e.line
. "`nmessage: " e.message
. "`nextra: " e.extra
Logger.addLogEntry("GUI: " msg)
MsgBox,% 4096+16, %A_ScriptName%,% msg
}
}
Destroy(name){
local msgID
Gui, %name%:Hide
for msgID in This.Windows[This.GetHWND(name)].OnMessages
OnMessage(msgID, This.Windows[This.GetHWND(name)].OnMessages[msgID], 0)
This.Windows.Remove(This.GetHWND(name))
Gui, %name%:Destroy
}
SetDefault(name) {
global
Gui,%name%:Default
This.ActiveGUI := name
}
Color(HWND,BkColor:="",TxColor:=""){
CtlColors.Change(HWND,BkColor,TxColor)
}
GuiColor(_winColor="", _ctrlColor="") {
global
if (_winColor != "" && _ctrlColor="")
Gui,% This.ActiveGUI ":Color", %_winColor%
else if (_winColor = "" && _ctrlColor != "")
Gui,% This.ActiveGUI ":Color", , %_ctrlColor%
else if (_winColor != "" && _ctrlColor != "")
Gui,% This.ActiveGUI ":Color", %_winColor%, %_ctrlColor%
If (_winColor!="")
This.Windows[This.GetHWND(This.ActiveGUI)].Colors.1 := _winColor
If (_ctrlColor!="")
This.Windows[This.GetHWND(This.ActiveGUI)].Colors.2 := _ctrlColor
}
Tooltip(HWND){
global
DetectHiddenWindows, On
If WinExist("ahk_class AutoHotkeyGUI") {
If (This.Elements.HasKey(HWND) && WinExist("ahk_id " HWND ))
ToolTip, % This.Elements[Format("{:#x}", HWND)].Tooltip
Else
Tooltip,
}
else
ToolTip,
DetectHiddenWindows, Off
return
}
TooltipOff(){
tooltip,
}
TooltipUpdate(HWND, TT:=""){
This.Elements[HWND].Tooltip := TT
}
TooltipToggle(bool:=""){
if (bool != "" && (bool == 0 || bool == 1))
This.TooltipSuspended := bool
else
This.TooltipSuspended := !This.TooltipSuspended
}
MouseMove(){
local _TT, font, size, color
static PrevTTControl := "", DisplayedTTTime := "", tooltipOff := "", ChangedHWND:=""
if !tooltipOff
tooltipOff := ObjBindMethod(this,"TooltipOff")
MouseGetPos,,,, _TT, 2
If !This.Elements.HasKey(_TT) && This.Elements.HasKey(DllCall("GetParent", "UInt", _TT))
_TT := DllCall("GetParent", "UInt", _TT)
If (This.Elements.HasKey(_TT) && IsObject(This.Elements[_TT].Hover)) {
If !ChangedHWND {
font := This.Elements[_TT].Hover.family
size := This.Elements[_TT].Hover.size
color := This.Elements[_TT].Hover.color
Gui, Font, c%color% s%size%, %font%
GuiControl, Font, %_TT%
If This.Elements[_TT].Hover.text != ""
GuiControl, , %_TT%, % This.Elements[_TT].Hover.text
ChangedHWND := _TT
}
} Else If ChangedHWND {
font := This.Elements[_TT].Font.family
size := This.Elements[_TT].Font.size
color := This.Elements[_TT].Font.color
Gui, Font, c%color% s%size%, %font%
GuiControl, Font, %ChangedHwnd%
GuiControl, , %ChangedHwnd%, % This.Elements[ChangedHwnd].Value
ChangedHWND := ""
}
If this.TooltipSuspended
Return
if (PrevTTControl!=_TT)
{
PrevTTControl:=_TT
; MsgBox % "prev " PrevTTControl " This " _TT
If (_TT) {
Gui.Tooltip(_TT)
SetTimer,% TooltipOff, -5000
DisplayedTTTime := A_TickCount
} Else {
Tooltip
}
} Else {
if (A_TickCount - DisplayedTTTime >= 5000)
Tooltip,
Else{
Gui.Tooltip(_TT)
SetTimer,% TooltipOff, -5000
}
}
return
}
SetControl(HWND,Value,SubCommand:="",Update:=1){
Global
local msg
try {
if SubCommand
GuiControl,% SubCommand,% HWND ,% Value
else
GuiControl,,% HWND ,% Value
if Update
This.Elements[HWND].Value := Value
} catch e {
msg := "Failed to set control.`n"
. "`nMessage: " e.Message
. "`nExtra: " e.Extra "`n"
. "`nGUI HWND: " HWND
. "`nGUI Label: " This.Lists.HWND[HWND]
. "`nSub Command: " SubCommand
. "`nContent: " Value
Icarus.Log(msg,"GUI: ")
MsgBox,% 4096+16, %A_ScriptName%,% msg
}
}
GetValue(HWND){
If This.Elements.HasKey(HWND)
Return This.Elements[HWND].Value
Return False
}
GetControlValue(HWND,Update:=1){
local Value
If This.Elements.HasKey(HWND){
GuiControlGet, Value, ,% HWND
If Update
This.Elements[HWND].Value := Value
Return Value
}
Return False
}
GetLabel(HWND){
If This.Elements.HasKey(HWND)
Return This.Elements[HWND].Label
Return False
}
GetHWND(L:="Label"){
local HWND, Element
for HWND, Element in This.Elements {
If (Element.Label = L)
Return HWND
}
for HWND, Element in This.Windows {
If (Element.Label = L || Element.Name = L)
Return HWND
}
Return False
}
GetControlPos(HWND) {
local ctrlPos, ctrlPosX, ctrlPosY, ctrlPosW, ctrlPosH
GuiControlGet, ctrlPos , Pos,% HWND
return {X:ctrlPosX,Y:ctrlPosY,W:ctrlPosW,H:ctrlPosH}
}
MoveControl(HWND,opts:=""){
Global
GuiControl, Move,% HWND,% opts
}
BindFunctionOnMessage(guiClass, msgID, funcName, params*) {
global
This.Windows[This.GetHWND(This.ActiveGUI)].OnMessages[msgID] := params.Count() ? %guiClass%[funcName].Bind(guiClass, params*) : %guiClass%[funcName].Bind(guiClass)
OnMessage(msgID, This.Windows[This.GetHWND(This.ActiveGUI)].OnMessages[msgID])
}
BindMethodOnMessage(guiClass, msgID, funcName, params*) {
global
This.Windows[This.GetHWND(This.ActiveGUI)].OnMessages[msgID] := params.Count() ? ObjBindMethod(guiClass, funcName, params*) : ObjBindMethod(guiClass, funcName)
OnMessage(msgID, This.Windows[This.GetHWND(This.ActiveGUI)].OnMessages[msgID])
}
BindFunctionToControl(HWND, funcName, params*) {
global
local __f
if ( params.Count() ) {
__f := Func(funcName).Bind(params*)
This.Elements[HWND].Binding := {Function: funcName, Bind:__f, Params: [params*]}
}
else {
__f := Func(funcName).Bind()
This.Elements[HWND].Binding := {Function: funcName, Bind:__f}
}
GuiControl, +g,% HWND,% __f
}
BindMethodToControl(guiClass, HWND, funcName, params*) {
global
local __f
__f := params.Count() ? ObjBindMethod(guiClass, funcName, params*) : ObjBindMethod(guiClass, funcName)
This.Elements[HWND].Binding := params.Count() ? {Method: funcName, Bind:__f, Params: [params*]} : {Method: funcName, Bind:__f}
GuiControl, +g,% HWND,% __f
}
DisableControlFunction(HWND) {
global
GuiControl, -g,% HWND
}
EnableControlFunction(HWND) {
global
local __f
if IsObject(This.Elements[HWND].Binding.Bind) {
__f := This.Elements[HWND]Binding.Bind
GuiControl, +g,% HWND,% __f
}
}
FontInit(family:="",size:="",color:="",ret:=False){
if family
This.initfamily := family
if size
This.initsize := size
if color
This.initcolor := color
if !ret
Gui, Font,% "c" This.initcolor " s" This.initsize, % This.initfamily ; initial text-settings
Else
Return {family:This.initfamily, size:This.initsize, color:This.initcolor}
}
FontSet(family:="",size:="",color:="",HWND:=""){
family := family ? family : This.initfamily
size := size ? size : This.initsize
color := color ? color : This.initcolor
If HWND
This.Elements[HWND].Font := {family:family, size:size, color:color}
Else
Gui, Font, c%color% s%size%, %family% ; set new text-settings
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment