Using ControlSetText() for windows in a specific order. Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
hiahkforum
Posts: 45
Joined: 08 Feb 2024, 04:21

Using ControlSetText() for windows in a specific order.

27 Apr 2024, 21:35

I have already described my problem in this topic: viewtopic.php?f=82&t=128881, but since not everyone could get to it in the background of the main topic, I think it's appropriate to create a separate one.

I have a GUI that can be activated up to 6 times, and my goal is that when one of the windows is closed, the numbers in all the other Edit1's change according to the number of windows remaining. Right now I have only achieved that the numbers change in random order, as the array returned by WinGetList() doesn't store the HWNDs in the order the windows are opened. I tried to fix this with Map(), giving each HWND the correct number, but that didn't work for some reason and the for-loop still retrieved the numbers in the wrong order. It is very important for me to change the numbers correctly, as the next step I want to change the positioning of the windows based on these numbers.
guiwindows.png
guiwindows.png (185.67 KiB) Viewed 344 times

Code: Select all

f::{
    Static Order := 0, First := 0, Third := 0
    if Order > 5 {
        return
    }
    G := Gui('-MinimizeBox +Owner', 'Static Title'), G.SetFont('s15 bold', 'Arial')
    G.Add('Edit', 'r1 w35 +Border +Center +Disabled', ++Order)
    G.OnEvent('Close', Close), G.OnEvent('Escape', Close)

    if !WinExist('ahk_class AutoHotkeyGUI') {
        G.Show('w425 h285'), First := 1, Third := 1
    }
    else {
		IDs := WinGetList('ahk_class AutoHotkeyGUI'), WinGetPos(&X, &Y, &W, &H, IDs[1])
		(IDs.Length = 1) ? (First ? (WinMove(X-W/2+9,,,, IDs[1]), G.Show('w425 h285 x' X+W/2-9 ' y' Y), First := 0) : G.Show('w425 h285 x' X+W-17 ' y' Y)) :
		(IDs.Length = 2) ? (Third ? (WinMove(, Y-H/2+4,,, IDs[1]), WinMove(, Y-H/2+4,,, IDs[2]), G.Show('w425 h285 x' X-W/2-8 ' y' Y+H/2-5), Third := 0) : G.Show('w425 h285 x' X-W/2-8 ' y' Y+H-9)) :
		(IDs.Length = 3) ? (WinMove(X-W/2+27,,,, IDs[1]), G.Show('w425 h285 x' X+W/2+9 ' y' Y)) :
		(IDs.Length = 4) ? G.Show('w425 h285 x' X-W*2+35 ' y' Y-H/2) :
		(IDs.Length = 5) ? G.Show('w425 h285 x' X+W*3-52 ' y' Y) : ''
	}

	Close(*) {
		--Order, G.Destroy()
		for This_ID in IDsR := WinGetList('ahk_class AutoHotkeyGUI') {  ; <----- Look here!
			ControlSetText(IDsR.Length-A_Index+1, 'Edit1', This_ID)
		}
	}
}
Noitalommi_2
Posts: 260
Joined: 16 Aug 2023, 10:58

Re: Using ControlSetText() for windows in a specific order.  Topic is solved

27 Apr 2024, 23:08

Hi.@hiahkforum

The script example in my last post regarding your original topic here, shows a way of keeping track of each gui using an array of Hwnds.
This example here isn't much different from the other one, except that there is a separate array for the controls.
It changes the numbering of each Gui depending on the Gui count.

Code: Select all

#Requires AutoHotkey 2.0
#SingleInstance


GuiHwnds := []
CtrlHwnds := []

F4::ExitApp
F1::{

    MyGui := Gui("-DPIScale")
	GuiHwnds.Push(MyGui.Hwnd)
	MyGui.AddText("vCount", "#=" GuiHwnds.Length)
	CtrlHwnds.Push(MyGui["Count"].Hwnd)
	MyGui.AddText(, "GuiHwnd=" MyGui.Hwnd)
	MyGui.OnEvent('Close', Close)
	MyGui.Show("w400 h100")

	Close(GuiObj) {

		for Index, Hwnd in GuiHwnds
			if GuiObj.Hwnd = Hwnd {

				GuiHwnds.RemoveAt(Index)
				CtrlHwnds.RemoveAt(Index)
				break
			}

		for Index, Hwnd in GuiHwnds
			ControlSetText "#=" Index, CtrlHwnds[Index], "ahk_id" Hwnd
	}
}
hiahkforum
Posts: 45
Joined: 08 Feb 2024, 04:21

Re: Using ControlSetText() for windows in a specific order.

28 Apr 2024, 16:59

Oh, sh*, now I see! Thank you, @Noitalommi_2! :bravo: Honestly, I tried to understand your previous script, but since there was too much stuff in there, and I'm new to programming or just not smart enough, I thought it was just an analogue of the Window Spy script, which is also useful, but not directly related to my problem. :crazy: Now your example is very clear and I see everything.

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: jollyjoe, mikeyww, ntepa and 23 guests