Classes and GUIs :confused: Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
murataygun
Posts: 104
Joined: 07 Aug 2015, 15:53

Classes and GUIs :confused:

02 Aug 2021, 05:23

Isn't there a simple way passing glabels. Why cant i just pass a method name to a glabel.

In the example blow. I just try to pass this.test() as Glabel.

Code: Select all

Test1 := new TestClass("Test1", 500, 600)
; Test2 := new TestClass("Test2", 450, 400)
; Test3 := new TestClass("Test3", 600, 400)
return

class TestClass {
    __New(Name, x, y) {
        Gui, New, +Hwndhwnd
        Gui %hwnd%: -Caption +AlwaysOnTop +ToolWindow +OwnDialogs
        Gui %hwnd%: Color, 464646
        Gui %hwnd%: Font, cFFFFFF, Consolas
        Gui %hwnd%: Add, Button, gthis.test() x231 y208 w93 h39, &Kaydet
        Gui %hwnd%: Add, Text, x10 y10 w100 h20, % Name
        Gui %hwnd%: Add, Text, x10 y30 w100 h20 hwndhPos, % x ", " y
        Gui %hwnd%: Show, % "x" x " y" y " w500 h600", guimiz

        WinSet, Transparent, 170, guimiz
        WinSet, Region, 0-0 w500 h600 R30-30, guimiz
        this.hPos := hPos, this.hwnd := hwnd, this.Name := Name
        OnMessage(0x201,  this.WM_LBUTTONDOWN.Bind(this))
    }
    WM_LBUTTONDOWN() {
        PostMessage, 0xA1, 2
        KeyWait, LButton
        WinGetPos, x,y,,, % "ahk_id " this.hwnd
        GuiControl, % this.hwnd ":", % this.hPos, % x ", " y
        ;MsgBox,,, % this.Name, 0.5
    }

    test(){
        msgbox, this is test
    }
}
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: Classes and GUIs :confused:

02 Aug 2021, 05:29

gTestClass.test
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
teadrinker
Posts: 4347
Joined: 29 Mar 2015, 09:41
Contact:

Re: Classes and GUIs :confused:

02 Aug 2021, 10:18

jNizM wrote: gTestClass.test
Not the best choice, because of:

Code: Select all

new TestClass

class TestClass {
   __New() {
      this.testValue := "Hello!"
      Gui, Add, Button, gTestClass.test, OK
      Gui, Show, w200 h100
   }
   Test() {
      MsgBox, % this.testValue
   }
}
Better:

Code: Select all

new TestClass

class TestClass {
   __New() {
      this.testValue := "Hello!"
      Gui, Add, Button,, OK
      handler := ObjBindMethod(this, "Test")
      GuiControl, +g, Button1, % handler
      Gui, Show, w200 h100
   }
   Test() {
      MsgBox, % this.testValue
   }
}
murataygun wrote:

Code: Select all

Gui %hwnd%:
This is unnecessary, just Gui.
murataygun
Posts: 104
Joined: 07 Aug 2015, 15:53

Re: Classes and GUIs :confused:

02 Aug 2021, 10:36

How can i use this method with Listview. What should i put in place of Button1, "LV1" ?
User avatar
Hellbent
Posts: 2109
Joined: 23 Sep 2017, 13:34

Re: Classes and GUIs :confused:

02 Aug 2021, 10:37

Alternatively, if you need to have access to "This" in your test method you can set it up like this. ( TestClass1 )

Code: Select all

#SingleInstance, Force
#NoEnv
SetBatchLines, -1

Class1 := New TestClass1()
Class2 := New TestClass2()
return
GuiClose:
GuiContextMenu:
*ESC::
	ExitApp


class TestClass1	{
	__New(){

		This.Value := "Blah Blah Blah"
		Gui, 1:Add, Button, xm ym w400 h200 hwndhwnd, Test

		bd := This.Test.Bind( This )
		GuiControl, 1:+g , % hwnd, % bd

		Gui, 1: Show,x400 
	}
	Test(){
		Gui, 1:+OwnDialogs 
		msgbox, % This.Value
	}
}

class TestClass2	{
	__New(){

		This.Value := "Blah Blah Blah"
		Gui, 2:Add, Button, xm ym w400 h200 hwndhwnd gTestClass2.Test, Test
		Gui, 2:Show, x800
	}
	Test(){
		Gui, 2:+OwnDialogs 
		msgbox, % This.Value
	}
}

*Edit* @teadrinker got there while I had this open.
teadrinker
Posts: 4347
Joined: 29 Mar 2015, 09:41
Contact:

Re: Classes and GUIs :confused:  Topic is solved

02 Aug 2021, 10:54

murataygun wrote: What should i put in place of Button1, "LV1" ?
No, it's better using a control hwnd:

Code: Select all

new TestClass

class TestClass {
   __New() {
      this.testValue := "Hello!"
      Gui, Add, Button, hwndhButton, OK
      handler := ObjBindMethod(this, "Test")
      GuiControl, +g, % hButton, % handler
      Gui, Show, w200 h100
   }
   Test() {
      MsgBox, % this.testValue
   }
}
murataygun
Posts: 104
Joined: 07 Aug 2015, 15:53

Re: Classes and GUIs :confused:

03 Aug 2021, 05:10

Thank you very much :thumbup:

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot] and 161 guests