Reasons for hwnd not being stored via Gui,Add

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
somsoc
Posts: 10
Joined: 19 Jul 2017, 18:47

Reasons for hwnd not being stored via Gui,Add

14 Aug 2017, 06:47

I've not run into this issue before, but right now, with a pretty simple Gui which has one image and one text control, if I add the controls using something like

Code: Select all

Gui,1:Add,Text,X100 Y100 vGuiTextDebug HwndstringHwndTextDebug,Debug text goes here
the Hwnd variable is blank.

This gave me a lot of difficulty until I used the 'round the houses' method of getting the classNN of the control and using that to retrieve the Hwnd.

I've never had AHK fail to store the hwnd for a control before so I'm wondering if anyone knows what might be causing this?

Here is the long winded method functions in case anyone needs them:

Code: Select all

;temporarily passes focus to controls so may cause issues if they have actions assigned to them
getClassNN(controlname,window="A")
{
	;store the focused control before changing
	ControlGetFocus,CONTROL_FOCUSED,%window%
	
	;switch focus to passed control
	GuiControl,Focus,%controlname%
	;get classNN of whatever the focused control is in the window
	ControlGetFocus,Class_NN,%window%

	;return focus to the previous control
	GuiControl,Focus,%CONTROL_FOCUSED%
	
	return Class_NN
}
and then to retrieve and store the hwnd:

Code: Select all

stringClassNN := getClassNN("GuiTextDebug")
ControlGet, stringHwndTextDebug, Hwnd,, %stringClassNN%,A
just me
Posts: 9458
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Reasons for hwnd not being stored via Gui,Add

14 Aug 2017, 07:13

No problem her:

Code: Select all

Gui,1:Add,Text,X100 Y100 vGuiTextDebug HwndstringHwndTextDebug,Debug text goes here
MsgBox, %stringHwndTextDebug%
Which AHK version do you use?
User avatar
evilC
Posts: 4823
Joined: 27 Feb 2014, 12:30

Re: Reasons for hwnd not being stored via Gui,Add

14 Aug 2017, 09:10

When working with GuiControls that are part of an AHK gui, you should not be using the ControlXXX commands, you should be using the GuiControlXXX commands.

The ControlXXX commands are for controls of another application.
somsoc
Posts: 10
Joined: 19 Jul 2017, 18:47

Re: Reasons for hwnd not being stored via Gui,Add

15 Aug 2017, 12:27

just me wrote:No problem her:

Code: Select all

Gui,1:Add,Text,X100 Y100 vGuiTextDebug HwndstringHwndTextDebug,Debug text goes here
MsgBox, %stringHwndTextDebug%
Which AHK version do you use?
I use version 1.1.24.05.

And for some reason this problem seems to have fixed itself. Obviously something I did before building my GUI was throwing it off but now the 'HwndstringHwndTextDebug' option does correctly store the hwnd when the text element is added.

I could go through and compare versions to find out what changed... (a few small things like initializing the GUI with 'Gui,1:+HwndstringGuiHwnd' instead of 'Gui,1:Color,...', some minor changes to functions above it) but.. eh... I have no idea.
evilC wrote:When working with GuiControls that are part of an AHK gui, you should not be using the ControlXXX commands, you should be using the GuiControlXXX commands.

The ControlXXX commands are for controls of another application.

Code: Select all

Gui,1:Default
GuiControlGet,stringHwndTextDebug,Hwnd,GuiTextDebug
returns a blank variable for me.

The reason I use the other method is either because in the past GuiControl also didn't work or because it wasn't added to the version of AHK I was using a few years back, so I had to use the ControlGet method. Either way, the ControlGet using ClassNN command is the only working method for me right now.
User avatar
evilC
Posts: 4823
Joined: 27 Feb 2014, 12:30

Re: Reasons for hwnd not being stored via Gui,Add

15 Aug 2017, 13:02

Code: Select all

Gui,1:Default
GuiControlGet,stringHwndTextDebug,Hwnd,GuiTextDebug
I see no code that creates a GuiControl, so of course hwnd would be blank

This code would get the value of the GuiControl using the Hwnd stored in the variable stringHwndTextDebug

Code: Select all

Gui, Add,Text,X100 Y100 vGuiTextDebug HwndstringHwndTextDebug,Debug text goes here
GuiControlGet, value, , stringHwndTextDebug
somsoc
Posts: 10
Joined: 19 Jul 2017, 18:47

Re: Reasons for hwnd not being stored via Gui,Add

15 Aug 2017, 13:58

evilC wrote:

Code: Select all

Gui,1:Default
GuiControlGet,stringHwndTextDebug,Hwnd,GuiTextDebug
I see no code that creates a GuiControl, so of course hwnd would be blank

This code would get the value of the GuiControl using the Hwnd stored in the variable stringHwndTextDebug

Code: Select all

Gui, Add,Text,X100 Y100 vGuiTextDebug HwndstringHwndTextDebug,Debug text goes here
GuiControlGet, value, , stringHwndTextDebug
I know :) I just didn't show the Gui,Add part.

Well maybe we're not talking about the same thing - until today, I didn't have a way of storing the hwnd for this text element without using the method above - I don't need to retrieve the value of the text element, I needed the hwnd of the element!

But now it is being correctly stored with the Gui,Add line and hwnd<var> option.

Getting hwnd with GuiControlGet (as above) doesn't work in my script.

At any rate it's not an issue right now but it's weird.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: sachinme and 350 guests