holahapi wrote: ↑08 May 2019, 05:12
After readings your links, I end up by using a
guiclose-sub outside the class to call the
class.function
I recommend against the use of labels (in this case). Btw, in your exemple, @
aGuiCLose, you
presuppose that
tc is the
target instance of you class which fired the label.
holahapi wrote: ↑08 May 2019, 05:12
Thank you for the solution. however, it is too difficult for me.[...]The best solution I want is pack everthing inside the class, and use 1 line code to create a instance.
After what has been said, it all depends on whether or not your class is intended to create many
instances. Generally, with objects, there may be multiple GUI, existing
concurrently. In this case your code must ensure, at least, that each time
it targets the right instance. The simpliest way I can think of with your specific requirement and how GuiClose label/function are designed is the following:
Code: Select all
#NoEnv
#SingleInstance force
#Warn
tc := new TestClass("blabla")
tc2 := new TestClass("hola!")
return
Class TestClass {
static _instances := [] ; keep track of instances
__New(name) {
GUI, New, % "+HWNDGUIhwnd +Label" . this.__Class ".__on" ; https://autohotkey.com/docs/commands/Gui.htm#PlusLabel
this.GUIhwnd := GUIhwnd
this.name := name
this.show()
return TestClass._instances[ GUIhwnd+0 ] := this ; keep track of GUI instances by storing them by their respective hwnd.
}
show(x:=100, y:=100, w:=100, h:=100) {
GUI % this.GUIhwnd . ":Show", % "x" x "y" y "w" w "h" h
}
destroy() {
GUI % this.GUIhwnd . ":Destroy"
TestClass._instances[this._GUIID] := "" ; remove circular references.
return 0
}
__onClose() { ; the first callback parameter - for the GUIClose label: the GUIhwnd - is passed to 'this' (see also: https://autohotkey.com/docs/Objects.htm#Custom_Classes_method)
GUIID := this, this := TestClass._instances[GUIID] ; retrieve the target instance by means of its hwnd.
MsgBox 4,, % "Are you sure you want to close the GUI '" . this.name . "'?"
IfMsgBox No
return true
return this.destroy() ; for example
}
}
It might exist a simpliest solution (I have a poor tendency to make things more complicated...
). Maybe someone think up something else.
Hope this helps.