I spend a couple of days trying to understand how Classes in AHK works.
Thanks to a forum community I manage to get basic representation of it.
Unfortunately in Help file Classes described in very complicated way without examples.
So I decided to make basic tutorial for beginners. Looks like it is big Tutorial and I could make mistakes so if you find them pleas let me know and I'll fix them.
We will discuss, Classes, Instances, Variables, Methods, Nested Classes, Class Inheritance, __New, __Delete.
Using Classes in programming is Calling Object Oriented Programming (O.O.P.) hope you here about it.
In Classes there are two main definitions:
Class
Instance
So what is class?
Imagine that you want to build a Car. To make it you have to have Plan of it.
This plane is Class. In AHK it looks like this:
Code: Select all
Class ClassName{
}
Imagine that you build Car using Plan, and this Car is Instance.
So Plan is Class and Car is Instance. In AHK creating Instance from Class looks like this:
Code: Select all
InstanceName:= New ClassName
So what is Class Variables?
Imagine you car has name for example BMW - this is property and it looks like - CarName:=BMW.
Also it has 4 wheels it is also property. For example WheelsAmount:=4
So what is Class Method?
Imagine when turn the steering wheel the car turns - this is method, and it looks like - TurnSteerWheel().
Also when you use key to open car this is also method for example CarOpenKey().
So imagine you made plane where all that Variables and Methods was defined, now you want to build a car. This is how it looks in AHK:
Code: Select all
;First we have to declare class:
Class Car{
CarName:="BMW"
WheelsAmount:=4
TurnSteerWheel()
{
Some Commands here...
}
CarOpenKey(){
Some Commands here...
}
}
; Then create Instane of that class with name BMWConcept
BMWConcept:=New Car
Lets make simple Class of Window. Window has a lot of properties like Windows Title, Windows ID, Position, Size, e.t.c. these will be Class Variables.
Also we can make with window a lot of actions for example Activate it, Get its Title, Get its ID, Get it's position e.t.c. Actions like this will be our Class Methods.
So lets make simple Class for Notepad of one Variable and one Method.
Code: Select all
#SingleInstance force
;Declare Class
Class Window{
Title:= "Untitled - Notepad" ;Variable Title
Activate() ;Activates window with Title - This.Title
{
IfWinExist, % This.Title
WinActivate
else
MsgBox % "There is no Window: "This.Title "`nPleas Run Notepad!"
}
}
;Create Instance
SomeWin:= New Window
Return
!^a:: ;Hotkey to run Instance Method
SomeWin.Activate()
Return
Code: Select all
#SingleInstance force
;Declare Class
Class Window{
Get()
{
WingetTitle TitleVar, A ; Get title from Active window.
This.Title:=TitleVar ; Set TitleVar to This.Title
WinGet IDVar,ID,A ; Get ID from Active window.
This.ID:=IDVar ; Set IDVar to This.ID
}
Activate() ;Activates window with Title - This.Title
{
;MsgBox % This.ID
WinActivate % "ahk_id "This.ID ;Use word "This" when you reffer to variable of this Class.
Return This.ID
}
AnnounceWinProps() ;Create message box with title and ID
{
MsgBox % "Title is: " This.Title "`n ID is: " This.ID
}
}
;Create Instance
SomeWin:= New Window
return
^!a:: ;Hotkey to run Instance Method
IfWinExist, ahk_class Notepad
WinActivate ahk_class Notepad
else
{
MsgBox % "There is no Window: Notepad.`nPleas Run Notepad!"
exit
}
SomeWin.get()
SomeWin.AnnounceWinProps()
Return
^!s::
If SomeWin.ID
SomeWin.Activate()
else
MsgBox Instance SomeWindow has no ID setted, pleas run SomeWin.get() first!
return
1. Was added methot - Get(). This method retrieves Window Title and Window ID from Active window.
2. Variable title was removed, because we will use method Get() to get Title.
2. Method Activate was modified and now it use not Window Title but Window ID.
3. Method AnnounceWinProps() was added, that show us Title and ID that was retrieved by Get()
4. Hotkey ^!a was modified now it activates Notepad, than run Get() Method.
5. Hotkey ^!s was added. This hotkey activates Window with Method Activate() if ID is True(was retrieved).
Now lets add nested Class to our Class Window with couple of variables. So what is Nested Class?
Nested Class is just Class inside another Class and you can access it like this BaseClass.NestedClass To acces variable from Nested class use this: BaseClass.NestedClass.Var or Method - BaseClass.NestedClass.Method().
Code: Select all
#SingleInstance force
;Declare Class
Class Window{
Class Tmp{
A:=1
Static B:=1
}
Get()
{
WingetTitle TitleVar, A ; Get title from Active window.
This.Title:=TitleVar ; Set TitleVar to This.Title
WinGet IDVar,ID,A ; Get ID from Active window.
This.ID:=IDVar ; Set IDVar to This.ID
}
Activate() ;Activates window with Title - This.Title
{
IfWinExist, % "ahk_id "This.ID
WinActivate % "ahk_id " This.ID
else
MsgBox % "There is no Window with ID: "This.ID
}
AnnounceWinProps() ;Create message box with title and ID
{
MsgBox % "Title is: " This.Title "`n ID is: " This.ID
}
}
;Create Instance
SomeWin:= New Window
return
^!a:: ;Hotkey to run Instance Method
WinActivate ahk_class Notepad
SomeWin.get()
SomeWin.AnnounceWinProps()
Return
^!s::
If SomeWin.ID
SomeWin.Activate()
else
MsgBox Instance SomeWindow has no ID setted, pleas run SomeWin.get() first!
return
^!d::
MsgBox % "SomeWin.Tmp.A = "SomeWin.Tmp.A "`nSomeWin.Tmp.B = "SomeWin.Tmp.B
return
Lets look how our variables was declared:
Code: Select all
Class Tmp{
A:=1
Static B:=1
}
Let's move to variable B, it has word "Static" this kind of variables calls - Static Variables. This variables belong to Class itself, so that is why we could access it, and our message showed us that B:=1.
Now let's talk about Class Inheritance.
So what is it?
Imagine you have Class A and it has Var1 and Var2. You created Class B and you want this Class to derive everything from A. Another words you want Class B have the same variables and Methods that has Class A.
Let's make it:
Code: Select all
Class A{
Static Var1:=1
Static Var2:=2
}
Class B extends A{
Var3:=3
}
NewClass:= New B
MsgBox % "Var1=" NewClass.Var1 "`nVar2=" NewClass.Var2 "`nVar3=" NewClass.Var3
return
This is calling Class Inheritance.
Let's back to our Notepad project. Let's create new Class Notepad and make it inherit everything from Class Window and add to it some new Methods.
Code: Select all
#SingleInstance force
;Declare Class
Class Window{
Class Tmp{
A:=1
Static B:=1
}
Get()
{
WingetTitle TitleVar, A ; Get title from Active window.
This.Title:=TitleVar ; Set TitleVar to This.Title
WinGet IDVar,ID,A ; Get ID from Active window.
This.ID:=IDVar ; Set IDVar to This.ID
}
Activate() ;Activates window with Title - This.Title
{
IfWinExist, % "ahk_id "This.ID
WinActivate % "ahk_id " This.ID
else
MsgBox % "There is no Window with ID: "This.ID
}
AnnounceWinProps() ;Create message box with title and ID
{
MsgBox % "Title is: " This.Title "`n ID is: " This.ID
}
}
Class Notepad extends Window{
Run()
{
IfWinNotExist ahk_class Notepad
Run Notepad
Else
WinActivate
}
}
;Create Instance
SomeWin:= New Window
SomeNot:= New Notepad
return
^!a:: ;Hotkey to run Instance Method
SomeNot.Run()
SomeNot.get()
SomeNot.AnnounceWinProps()
Return
^!s::
If SomeWin.ID
SomeWin.Activate()
else
MsgBox Instance SomeWindow has no ID setted, pleas run SomeWin.get() first!
return
^!d::
MsgBox % "SomeWin.Tmp.A = "SomeWin.Tmp.A "`nSomeWin.Tmp.B = "SomeWin.Tmp.B
return
Also take a look at Hotkey - ^!a. As you can see Instanse SomeNote use all inherited methods from Class Window.
And last...
Let's imagine that we want some actions occurs when we create Instance. To initialize that actions we will use: __New, this is how it look like:
Code: Select all
Class A{
__New(){
Some commands here...
}
}
Code: Select all
#SingleInstance force
;Declare Class
Class Window{
Class Tmp{
A:=1
Static B:=1
}
Get()
{
WingetTitle TitleVar, A ; Get title from Active window.
This.Title:=TitleVar ; Set TitleVar to This.Title
WinGet IDVar,ID,A ; Get ID from Active window.
This.ID:=IDVar ; Set IDVar to This.ID
}
Activate() ;Activates window with Title - This.Title
{
IfWinExist, % "ahk_id "This.ID
WinActivate % "ahk_id " This.ID
else
MsgBox % "There is no Window with ID: "This.ID
}
AnnounceWinProps() ;Create message box with title and ID
{
MsgBox % "Title is: " This.Title "`n ID is: " This.ID
}
}
Class Notepad extends Window{
__New()
{
This.Run()
This.get()
}
Run()
{
IfWinNotExist ahk_class Notepad
Run Notepad
Else
WinActivate
}
}
;Create Instance
SomeWin:= New Window
SomeNot:= New Notepad
return
^!a:: ;Hotkey to run Instance Method
;SomeNot.Run()
;SomeNot.get()
;SomeNot.AnnounceWinProps()
Return
^!s::
If SomeNot.ID
SomeNot.Activate()
else
MsgBox Instance SomeWindow has no ID setted, pleas run SomeWin.get() first!
return
^!d::
MsgBox % "SomeWin.Tmp.A = "SomeWin.Tmp.A "`nSomeWin.Tmp.B = "SomeWin.Tmp.B
return
Also there is __Del like __New it works when Instance deletes but now I'm not sure how it works, I'll back to it later.
So lets make final touch, lets create Class Calc that will inherit Class Window also we will add to it method Run()
Code: Select all
#SingleInstance force
;Declare Class
Class Window{
Class Tmp{
A:=1
Static B:=1
}
Get()
{
WingetTitle TitleVar, A ; Get title from Active window.
This.Title:=TitleVar ; Set TitleVar to This.Title
WinGet IDVar,ID,A ; Get ID from Active window.
This.ID:=IDVar ; Set IDVar to This.ID
}
Activate() ;Activates window with Title - This.Title
{
IfWinExist, % "ahk_id "This.ID
WinActivate % "ahk_id " This.ID
else
MsgBox % "There is no Window with ID: "This.ID
}
AnnounceWinProps() ;Create message box with title and ID
{
MsgBox % "Title is: " This.Title "`n ID is: " This.ID
}
}
Class Notepad extends Window{
__New()
{
This.Run()
This.get()
}
Run()
{
IfWinNotExist ahk_class Notepad
Run Notepad
Else
WinActivate
}
}
Class Calc extends Window{
__New()
{
This.Run()
This.get()
}
Run()
{
IfWinNotExist ahk_class CalcFrame
Run Calc
Else
WinActivate
}
}
;Create Instance
SomeWin:= New Window
SomeNot:= New Notepad
SomeCalc:= New Calc
return
^!a:: ;Hotkey to run Instance Method
;SomeNot.Run()
;SomeNot.get()
;SomeNot.AnnounceWinProps()
Return
^!s::
If SomeNot.ID
SomeNot.Activate()
else
MsgBox Instance SomeWindow has no ID setted, pleas run SomeWin.get() first!
return
^!d::
If SomeCalc.ID
SomeCalc.Activate()
else
MsgBox Instance SomeWindow has no ID setted, pleas run SomeWin.get() first!
return
So this was basics that I hope will allow you to work with AHK Classes.