Static variables for GUI control associated variables?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
Location: France
Contact:

Re: Static variables for GUI control associated variables?

23 Sep 2018, 13:36

It does not work for me either - without being able to explain it. I manage to make it work monitoring the WM_SYSCOMMAND message, though (specifically, one of its wParam value, namely SC_CLOSE):

Code: Select all

G1 := new GUI(), G2 := new GUI()
G3 := new gui_("hello method")

Class GUI {

	__New() {
		local _hwnd
		Gui, New, % "+Resize +Hwnd_hwnd +Label" . this.__class . "On"
		Gui, Add, Edit, x0 y0 vEdit
		Gui, Show, w800 h500
	}
	
}
class gui_ { ;cf. https://autohotkey.com/boards/viewtopic.php?f=5&t=42292&p=192277#profile192277
	__new(n){
		this.n := n
		gui new, +resize +hwndh
		this.h := h
		
		gui % this.h ":show", w200 h100
		onmessage(WM_SIZE := 0x05, objbindmethod(this,"guiSize"))
		onmessage(WM_SYSCOMMAND := 0x112, objbindmethod(this,"guiClose"))
		; onmessage(WM_CLOSE := 0x10, objbindmethod(this,"guiClose"))
	}
	guiSize(wParam, lParam, msg, hwnd){
		if this.h != hwnd
			return
			/*
			0: The window has been restored, or resized normally such as by dragging its edges.
			1: The window has been minimized.
			2: The window has been maximized.
			*/
			; width and height
			w := lParam & 0xFFFF
			h := lParam >> 16 
		tooltip % w "`t" h "`t" wParam "`n" this.n
	}
	guiClose(wParam, lParam, msg, hwnd) { ; 0x112 = WM_SYSCOMMAND, 0xF060 = SC_CLOSE
		if this.h != hwnd
			return ; we return nothing in order to process any GuiClose label, if registered
		return GUIOnClose(hwnd)
	}
}

GUIOnSize(_hwnd, _eventInfo, _width, _height) {
	ToolTip % _hwnd "," _eventInfo "," _width "," _height
}
GUIOnClose(_hwnd) {
	MsgBox 4,, Are you sure you want to hide the GUI whose ID is %_hwnd%?
    IfMsgBox No
        return 1
}
my scripts
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Static variables for GUI control associated variables?

23 Sep 2018, 14:43

@A_AhkUser Thanks for your code and the Gui, +Label hint. :)
with Gui, +Label I can get my GUI class to work with one external function. No more hard-coded labels.

Code: Select all

#NoEnv
#SingleInstance, Force

    MainGui := new GUI("Main", "Two Buttons ...", "+AlwaysOnTop")
    AUXGui := new GUI("AUX", "... and an EditBox.", "-MinimizeBox -MaximizeBox")

    MainGui.Show(50, 50)
    AUXGui.Show(400, 50)

Esc:: ; end of auto-execute section
ExitApp



;===============================================================================
class GUI ; object oriented GUI structure
;===============================================================================
{
    __New(Name, WinTitle := "", Options := "") ; constructor
    {
        this.Name := Name
        Gui, % this.Name ":New", HWNDhGui, %WinTitle%
        Gui, +Labelmy +Resize %Options%
        
        Gui, Font, s16
		Gui, Add, Edit, HWNDhEdit w250, Hello, World!
        Gui, Add, Button, HWNDhButton1 wp hp, 1
        Gui, Add, Button, HWNDhButton2 wp hp, 2

        ; define actions
		onTyping := this.Edit_onTyping.Bind(this)
		onClick := this.Button_onClick.Bind(this)

        ; bind to controls
		GuiControl +g, %hEdit%, % onTyping
		GuiControl +g, %hButton1%, % onClick
		GuiControl +g, %hButton2%, % onClick

        ; 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)
    {
        GuiControlGet, outVar,, %hwnd%
        Tooltip, % "You are typing @" outVar "@ in Edit box on " this.Name
    }

    Button_onClick()
    {
        Tooltip, % "You pressed button " A_GuiControl " on " this.Name
    }

} ; end of class



;-------------------------------------------------------------------------------
myClose() ; {Alt+F4} pressed, [X] clicked
;-------------------------------------------------------------------------------
{
    ExitApp
}
User avatar
Scoox
Posts: 125
Joined: 11 May 2014, 09:12
Location: China

Re: Static variables for GUI control associated variables?

24 Sep 2018, 02:06

Been busy these past few days, thanks for the replies, I will take a detailed look asap.
User
Posts: 407
Joined: 26 Jun 2017, 08:12

Re: Static variables for GUI control associated variables?

24 Sep 2018, 08:36

wolf_II wrote:@A_AhkUser Thanks for your code and the Gui, +Label hint. :)
with Gui, +Label I can get my GUI class to work with one external function. No more hard-coded labels.
You should use this:

myclose:
ExitApp
return


instead of:

myclose(){
ExitApp
}


Because, "Labels" has priority over "Functions" in this case (Gui Labels, Gui control labels, etc)!

If the below code is included in your script, when a gui close button is triggered, the "msgbox, myclose" shows up instead the script exiting itself!

myclose:
msgbox, myclose
return


[Edit]:

You can even use this inside the class:

myClose() ; {Alt+F4} pressed, [X] clicked
{
myclose: ;if any "myclose:" label is used outside this "class", a "duplicated" label found msgbox shows up!
ExitApp
}

Code: Select all

#NoEnv
#SingleInstance, Force

    MainGui := new GUI("Main", "Two Buttons ...", "+AlwaysOnTop")
    AUXGui := new GUI("AUX", "... and an EditBox.", "-MinimizeBox -MaximizeBox")

    MainGui.Show(50, 50)
    AUXGui.Show(400, 50)

Esc:: ; end of auto-execute section
ExitApp



;===============================================================================
class GUI ; object oriented GUI structure
;===============================================================================
{
    __New(Name, WinTitle := "", Options := "") ; constructor
    {
        this.Name := Name
        Gui, % this.Name ":New", HWNDhGui, %WinTitle%
        Gui, +Labelmy +Resize %Options%
        
        Gui, Font, s16
		Gui, Add, Edit, HWNDhEdit w250, Hello, World!
        Gui, Add, Button, HWNDhButton1 wp hp, 1
        Gui, Add, Button, HWNDhButton2 wp hp, 2

        ; define actions
		onTyping := this.Edit_onTyping.Bind(this)
		onClick := this.Button_onClick.Bind(this)

        ; bind to controls
		GuiControl +g, %hEdit%, % onTyping
		GuiControl +g, %hButton1%, % onClick
		GuiControl +g, %hButton2%, % onClick

        ; 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)
    {
        GuiControlGet, outVar,, %hwnd%
        Tooltip, % "You are typing @" outVar "@ in Edit box on " this.Name
    }

    Button_onClick()
    {
        Tooltip, % "You pressed button " A_GuiControl " on " this.Name
    }


   myClose()      ; {Alt+F4} pressed, [X] clicked
   {
    myclose:      ;if any "myclose:" label is used outside this "class", a "duplicated" label found msgbox shows up!
    ExitApp
   }

} ; end of class
just me
Posts: 9490
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Static variables for GUI control associated variables?

25 Sep 2018, 04:50

If you only want to have 'named' controls outside of the global namespace:

AHK 1.1.20+ provides the option to use functions as event handlers. It also provides the option to access GUI controls via HWND.

Function specific parameters are passed to the functions, but AHK also creates the traditional 'label' variables like A_Gui, A_GuiControl, etc. They can be used to access the controls via a non-global name. You just have to associate the name with the HWND of the control in a GUI specific array. That might meet the requirements posted in th OP.

Code: Select all

#NoEnv
GuiMainCreate()
GuiMainShow()
Return

GuiMainClose() {
   Gui, +OwnDialogs
   MsgBox, Gui %A_Gui% will be closed!
   Gui, Destroy
   ExitApp
}

GuiMainCreate() {
   Static
   Gui, Main:New, +LabelGuiMain +hwndHGUI, Test Main
   Gui, +AlwaysOnTop
   Gui, Font, s16
   Gui, Add, Edit, vMyEdit hwndHEDT gGuiMainEvents x10 y10 w200 , Hello world!
   Gui, Add, Button, vMyButton1 hwndHBTN1 gGuiMainEvents x10 y50 w200 h40, Reset Edit
   Gui, Add, Button, vMyButton2 hwndHBTN2 gGuiMainEvents x10 y100 w200 h40, Clear Edit
   Values := {HGUI: HGUI, MyEdit: HEDT, MyButton1: HBTN1, MyButton2: HBTN2}
   GuiStore("Main", Values)
}

GuiMainShow() {
   Gui, Main:Show
}

GuiMainEvents(CtrlHwnd, GuiEvent, EventInfo, ErrLevel := 0) {
   ThisGui := GuiStore(A_Gui)
   If (A_GuiControl = "MyButton1")
   {
      GuiControlGet, Caption, , %CtrlHwnd%
      HEDT := ThisGui["MyEdit"]
      GuiControl, -g, %HEDT%
      GuiControl, , %HEDT%, Hello world!
      GuiControl, +g%A_ThisFunc%, %HEDT%
      Tooltip You pressed <%Caption%> on %A_Gui% with HWND %CtrlHwnd%
   }
   Else If (A_GuiControl = "MyButton2")
   {
      GuiControlGet, Caption, , %CtrlHwnd%
      HEDT := ThisGui["MyEdit"]
      GuiControl, -g, %HEDT%
      GuiControl, , %HEDT%
      GuiControl, +g%A_ThisFunc%, %HEDT%
      Tooltip You pressed <%Caption%> on %A_Gui% with HWND %CtrlHwnd%
   }
   Else If (A_GuiControl = "MyEdit")
   {
      GuiControlGet, Content, , %CtrlHwnd%
      Tooltip You are typing text in Edit box on %A_Gui% with HWND %CtrlHwnd%`n`n%Content%
   }
}

GuiStore(Name, Values := "") {
   Static GuiValues := {}
   If (Values = "Delete")
      GuiValues.Delete(Name)
   Else If IsObject(Values)
      GuiValues[Name] := Values
   Return GuiValues[Name]
}

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: pafec and 161 guests