GuiSize Function inside Class Not Executing Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
Delta Pythagorean
Posts: 627
Joined: 13 Feb 2017, 13:44
Location: Somewhere in the US
Contact:

GuiSize Function inside Class Not Executing

Post by Delta Pythagorean » 02 Jan 2018, 00:55

[Moderator's note: Topic moved from Bug Reports.]

Exactly as it sounds. I don't know if I'm doing something wrong, but nothing's moving/sizing.

For example:

Code: Select all

New Gui_()
Class Gui_ {
	__New() {
		Static
		Gui, 1:New, +Resize
		Gui, 1:Add, Edit, x0 y0 vEdit
		Gui, 1:Show, w800 h500
	}

	GuiClose() {
		MsgBox
		ExitApp
	}

	GuiSize() {
		GuiControl, Move, Edit, % "w" A_GuiWidth " h" A_GuiHeight
	}
}
Nothing is executed for the GuiSize function. GuiClose doesn't seem to work either.

EDIT & Disclaimer: My apologies on pushing this subtopic in the Bug Reports topic. I hadn't realized I was in there at the time.
Last edited by Delta Pythagorean on 02 Jan 2018, 23:16, edited 1 time in total.

[AHK]......: v2.0.12 | 64-bit
[OS].......: Windows 11 | 23H2 (OS Build: 22621.3296)
[GITHUB]...: github.com/DelPyth
[PAYPAL]...: paypal.me/DelPyth
[DISCORD]..: tophatcat


A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
Location: France
Contact:

Re: GuiSize Function inside Class Not Executing

Post by A_AhkUser » 02 Jan 2018, 01:07

In your exemple GuiSize is not a function nor a method.

Try something like this instead:

Code: Select all

#NoEnv
SendMode Input
SetWorkingDir %A_ScriptDir%
#SingleInstance force

#Warn, ClassOverwrite, Off ; 1.1.27.00+

(G:=new Gui_()).onSize.push(Func("test"), Gui_.myMethod.bind(Gui_))

Class Gui_ {

	static instances := [] ; static property
	
	onSize := [] ; instance property
	
	__New() {
		local __hwnd
		Gui, New, +Resize +Hwnd__hwnd
		Gui, %__hwnd%:Add, Edit, x0 y0 vEdit
		Gui, %__hwnd%:Show, w800 h500
	return Gui_.instances[__hwnd] := this
	}
	myMethod(__instance, __eventInfo, __width, __height) {
	ToolTip % __instance " " __eventInfo " " __width " " __height " from " A_ThisFunc, 0, 0, 2 
	}

}
GuiSize(__hwnd, __eventInfo, __width, __height) { ; guisize is a function
for __index, __callback in (__instance:=Gui_.instances[__hwnd]).onSize
	__callback.call(__instance, __eventInfo, __width, __height)
}

test(__instance, __eventInfo, __width, __height) {
ToolTip % __instance " " __eventInfo " " __width " " __height
}

[EDIT] Added example with a method.
my scripts

User avatar
Delta Pythagorean
Posts: 627
Joined: 13 Feb 2017, 13:44
Location: Somewhere in the US
Contact:

Re: GuiSize Function inside Class Not Executing

Post by Delta Pythagorean » 02 Jan 2018, 01:44

Interesting, but I still prefer using the function inside a method. Do you know anyway of doing this?

[AHK]......: v2.0.12 | 64-bit
[OS].......: Windows 11 | 23H2 (OS Build: 22621.3296)
[GITHUB]...: github.com/DelPyth
[PAYPAL]...: paypal.me/DelPyth
[DISCORD]..: tophatcat


wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: GuiSize Function inside Class Not Executing

Post by wolf_II » 02 Jan 2018, 02:35

I could make your script work with moving the GuiXXX functions outside the class and correcting the typo: Edit <> Edit1

Code: Select all

GuiControl, Move, Edit1, % "w" A_GuiWidth " h" A_GuiHeight
I have not tried to keep the functions inside the class.

User avatar
Delta Pythagorean
Posts: 627
Joined: 13 Feb 2017, 13:44
Location: Somewhere in the US
Contact:

Re: GuiSize Function inside Class Not Executing

Post by Delta Pythagorean » 02 Jan 2018, 04:18

wolf_II wrote:Edit <> Edit1
I used vEdit to set the variable for the control, no matter what I'd set it to, it'd still be the same problem.

[AHK]......: v2.0.12 | 64-bit
[OS].......: Windows 11 | 23H2 (OS Build: 22621.3296)
[GITHUB]...: github.com/DelPyth
[PAYPAL]...: paypal.me/DelPyth
[DISCORD]..: tophatcat


A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
Location: France
Contact:

Re: GuiSize Function inside Class Not Executing

Post by A_AhkUser » 02 Jan 2018, 10:59

Delta Pythagorean wrote:Interesting, but I still prefer using the function inside a method. Do you know anyway of doing this?
If a label does not exist for a given event, a function with that name can be called instead.
source: Gui labels

So I guess you cannot do this (strictly within the field of the built-in GUI command, at least). If you still want to use a method you must have an incentive to do so. In particular, if the goal is to avoid to populate the global scope, a half-baked solution could be to specify a custom prefix for you GUI labels. Actually, further in the documentation on can read:
[...] To set a custom prefix, use Gui +Label.
So you can also do this:

Code: Select all

G1 := new GUI_(), G2 := new GUI_()
return

Class GUI_ {

	__New() {
	local __hwnd
		MsgBox, 64,, % A_ThisFunc
		Gui, New, % "+Resize +Hwnd__hwnd +Label" . this.__class . "On"
		Gui, %__hwnd%:Add, Edit, x0 y0 vEdit
		Gui, %__hwnd%:Show, w800 h500
		this.GuiSize()
	}
	GuiSize() {
		MsgBox, 64,, % A_ThisFunc
	}
	
}
GUI_OnSize(__hwnd, __eventInfo, __width, __height) {
	ToolTip % __hwnd " " __eventInfo " " __width " " __height
}
So that you reserve the variable name GUI_* in your script. See also this other solution by GeekDude - not sure to fully understand how it works exactly, though. Note that it still uses functions, and, I would be tempted to say , if even GeekDude uses function, it is likely that there's no alternative:

Yes it is possible to to this: :shock: :crazy:

Code: Select all

f()g()
f() {
MsgBox % A_ThisFunc
}g() {
MsgBox % A_ThisFunc
}
my scripts

Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: GuiSize Function inside Class Not Executing  Topic is solved

Post by Helgef » 02 Jan 2018, 14:16

Hello :wave:
A_AhkUser wrote:not sure to fully understand how it works exactly
Looks like your +Label example to me.
So I guess you cannot do this (strictly within the field of the built-in GUI command, at least)
Not with GUI commands, but we can use onmessage,

Code: Select all

class gui_ {
	__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"))
	}
	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
	}
}

new gui_("hello method")

f()g(),
Concatenate wrote:When the dot is omitted, there should be at least one space between the items to be merged
Note, should :lol:
:arrow: in v2, this problem is gone,
GuiCreate() wrote: EventObj
An "event sink", or object to bind events to. If EventObj is specified, OnEvent, OnNotify and OnCommand can be used to register methods of EventObj to be called when an event is raised.
src

Cheers.

A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
Location: France
Contact:

Re: GuiSize Function inside Class Not Executing

Post by A_AhkUser » 02 Jan 2018, 15:16

Hi Helgef,
Helgef wrote:not sure to fully understand how it works exactly
I didn't mean by this example that the method is called; rather Gui_OnSize function is called off course. I left the method in order to display its name to show that it cannot share the same name as the function (and of any function btw) and, thus, cannot in any manner be triggered by a gui event as a gui label.

Otherwise, off course, each time an instance is created, an anonymous GUI is created, yet:
If no name is given, the following applies:
•Special labels such as GuiClose have the default "Gui" prefix unless overridden by +LabelPrefix in the options.
source: Gui, New
so that Gui_OnSize is triggered by any GUI size event (A_Gui containing the HWND of the GUI, source of the event, hwnd besides automatically passed to the callback).
Hegel wrote: :arrow: in v2
It is not the first time you get me on the 'right' track; at that point I consider this a a form of proselytism... :lol:

The example using onmessage is really nice; I'm glad you shared this snippet. Thanks ;)

Cheers
my scripts

User avatar
Delta Pythagorean
Posts: 627
Joined: 13 Feb 2017, 13:44
Location: Somewhere in the US
Contact:

Re: GuiSize Function inside Class Not Executing

Post by Delta Pythagorean » 02 Jan 2018, 23:28

I see that OnMessage seems to work best and keeps up with the resize quite well, thank you @HelGef!

[AHK]......: v2.0.12 | 64-bit
[OS].......: Windows 11 | 23H2 (OS Build: 22621.3296)
[GITHUB]...: github.com/DelPyth
[PAYPAL]...: paypal.me/DelPyth
[DISCORD]..: tophatcat


william_ahk
Posts: 481
Joined: 03 Dec 2018, 20:02

Re: GuiSize Function inside Class Not Executing

Post by william_ahk » 29 Nov 2021, 08:59

Helgef wrote:
02 Jan 2018, 14:16
Not with GUI commands, but we can use onmessage,

Code: Select all

class gui_ {
	__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"))
	}
	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
	}
}

new gui_("hello method")


Cheers.
Looks like WM_SIZE doesn't get triggered upon the window creation? Is it possible to make this kind of functionality work in a class?

Code: Select all

Gui, Add, Text,, cardboard v1 configuration
Gui, Add, Text,, cardboard v2 configuration
Gui, Show, Hide
return

GuiSize:
Gui, Show, % "x" A_ScreenWidth - A_GuiWidth " y" A_GuiHeight
return

Post Reply

Return to “Ask for Help (v1)”