How to add a method to check if the window is (+/- minimized) Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
Hellbent
Posts: 2102
Joined: 23 Sep 2017, 13:34

How to add a method to check if the window is (+/- minimized)

12 Jun 2019, 21:14

I have this class that I am working on for a upcoming project and I would like to add in a method that checks if a window is minimized and back.

Right now if I minimize the gui in one monitor and then bring it back up while i'm in another monitor it will go back to the monitor that it was minimized in and I have to move my cursor back to that monitor before I can get it to go to the new one.

I know that I can use a window message to do this, but I'm not sure about how to go about adding it as part of the class.

Here is a working example of the class in a script.
All it does is re-position a small gui on whatever monitor my cursor is in.

Code: Select all

#SingleInstance,Force


Gui,1:+AlwaysOnTop
Gui,1:Color,444444
Gui,1:Show,w300 h150,Monitor Position Testing

;##############################################################
Monitors := New MonitorClass()
Monitors.Set_Window_Move_Timer(GUINAME:=1,TCount:=300,xpOff:=350,ypOff:=100,xr:=2,yr:=1)
;##############################################################

return
GuiClose:
GuiContextMenu:
*ESC::
	ExitApp
	
	
*numpad1::
	(Toggle:=!Toggle)?(Monitors.Turn_Off_Window_Move_Timer()):(Monitors.Turn_On_Window_Move_Timer())
	return



;-------------------------------------------------------------------------------------------------
;-------------------------------------------------------------------------------------------------
;-------------------------------------------------------------------------------------------------

class MonitorClass	{
	__New(){
		This._SetMonCount()
		This._SetPrimeMonitor()
		This._Set_Bounds()
	}
	_SetMonCount(){
		local MC
		SysGet, MC, MonitorCount 
		This.MonitorCount := MC
	}
	_SetPrimeMonitor(){
		local PM
		SysGet, PM, MonitorPrimary
		This.PrimeMonitor := PM
	}
	_Set_Bounds(){
		local bmon,bmonLeft,bmonRight,bmonTop,bmonBottom
		This.Monitors:=[]
		Loop,% This.MonitorCount	{
			SysGet, bmon, Monitor, % A_Index
			This.Monitor[A_Index]:=	{ Left		: 	bmonLeft
									, Top		: 	bmonTop
									, Right		: 	bmonRight
									, Bottom	: 	bmonBottom 	}
		}
	}
	Get_Current_Monitor(){
		local x,y
		CoordMode,Mouse,Screen
		MouseGetPos,x,y
		Loop,% This.MonitorCount	{
			if(x>=This.Monitor[A_Index].Left&&x<=This.Monitor[A_Index].Right&&y>=This.Monitor[A_Index].Top&&y<=This.Monitor[A_Index].Bottom){
				return A_Index
			}
		}
	}
	Get_New_Window_Position(curMon,xpOff,ypOff,xr,yr){
		local tposa:="",tposa:={}
		if(xr=1) ; 1 xr = relative to the left side ; 2 xr = relative to the right side
			tposa.x:=This.Monitor[curMon].Left+xpOff
		else 
			tposa.x:=This.Monitor[curMon].Right-xpOff
		if(yr=1) ; 1 yr = relative to the top  ; 2 yr = relative to the bottom
			tposa.y:=This.Monitor[curMon].Top+ypOff
		else 
			tposa.y:=This.Monitor[curMon].Bottom-ypOff
		return tposa
	}
	Set_Window_Move_Timer(GUINAME:=1,TCount:=500,xpOff:=0,ypOff:=0,xr:=1,yr:=1){
		
		local Window_Timer
		
		This.Window_Move_Obj:=	{ 	Interval		:	TCount
								,	GUINAME			:	GUINAME
								,	XPOFF			:	xpOff
								,	YPOFF			:	ypOff
								,	XRelative		:	xr
								,	YRelative		:	yr	
								,	Current_Monitor	:	This.Get_Current_Monitor()
								,	Old_Monitor		:	This.Get_Current_Monitor()	
								,	NEW_GUI_POS		:	This.Get_New_Window_Position(This.Get_Current_Monitor(),xpOff,ypOff,xr,yr)	}
								
		This.Window_Timer := Window_Timer :=  ObjBindMethod(This, "_Window_Move_Timer")
		
		This._Set_TFTime()
		
		SetTimer,%Window_Timer%,%TCount%
		
	}
	_Window_Move_Timer(){
		This.Window_Move_Obj.Current_Monitor := This.Get_Current_Monitor()
		if(This.Window_Move_Obj.Current_Monitor!=This.Window_Move_Obj.Old_Monitor){
			This.Window_Move_Obj.NEW_GUI_POS:=This.Get_New_Window_Position(This.Window_Move_Obj.Current_Monitor,This.Window_Move_Obj.XPOFF,This.Window_Move_Obj.YPOFF,This.Window_Move_Obj.XRelative,This.Window_Move_Obj.YRelative)
			This._Move_Window()
			This.Window_Move_Obj.Old_Monitor := This.Window_Move_Obj.Current_Monitor
		}
	}
	_Set_TFTime(){
		This.Window_Move_Obj.Current_Monitor := This.Get_Current_Monitor()
		This.Window_Move_Obj.NEW_GUI_POS:=This.Get_New_Window_Position(This.Window_Move_Obj.Current_Monitor,This.Window_Move_Obj.XPOFF,This.Window_Move_Obj.YPOFF,This.Window_Move_Obj.XRelative,This.Window_Move_Obj.YRelative)
		This.Window_Move_Obj.Old_Monitor := This.Window_Move_Obj.Current_Monitor
		This._Move_Window()
	}
	_Move_Window(){
		Gui,% This.Window_Move_Obj.GUINAME ":Show",% "x" This.Window_Move_Obj.NEW_GUI_POS.X " y" This.Window_Move_Obj.NEW_GUI_POS.Y " NA"
	}
	Turn_Off_Window_Move_Timer(){
		local Window_Timer
		Window_Timer := This.Window_Timer
		SetTimer,%Window_Timer%,Off
	}
	Turn_On_Window_Move_Timer(){
		local Window_Timer
		Window_Timer := This.Window_Timer
		This._Set_TFTime()
		SetTimer,%Window_Timer%,On
	}
}
Thanks.
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: How to add a method to check if the window is (+/- minimized)

12 Jun 2019, 21:27

GuiSize wrote:Launched when the window is resized, minimized, maximized, or restored... A_EventInfo and ErrorLevel will both contain one of the following digits:

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.
User avatar
Hellbent
Posts: 2102
Joined: 23 Sep 2017, 13:34

Re: How to add a method to check if the window is (+/- minimized)

12 Jun 2019, 21:36

tmplinshi wrote:
12 Jun 2019, 21:27
GuiSize wrote:Launched when the window is resized, minimized, maximized, or restored... A_EventInfo and ErrorLevel will both contain one of the following digits:

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.
Thank you.

How would I go about adding this to the class?
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: How to add a method to check if the window is (+/- minimized)

12 Jun 2019, 21:44

Code: Select all

GuiSize(GuiHwnd, EventInfo, Width, Height)
{
	if (EventInfo = 1)
		Tooltip The window has been minimized.
	else if (EventInfo = 0)
		Tooltip The window has been restored
}
Just replace the Tooltip with whatever you want to do.
User avatar
Hellbent
Posts: 2102
Joined: 23 Sep 2017, 13:34

Re: How to add a method to check if the window is (+/- minimized)

12 Jun 2019, 22:12

@tmplinshi

Thank you for your reply.

I think that we have our wires crossed.
I would like to add a method to the class that does this, I know that I can have external functions and subroutines that handle it, but I want it in the class. Truth be told, the project that I wrote this for isn't even going to have a option to minimize the window, but I would like to know how I could do it because I can learn from it and infer other things that aren't really related.

I know that I can use OnMessage(0x05,"WM_SIZE") for this, but I don't know how I would go about putting this into a class, or any other message for that matter. So seeing how it is done here in this example will inform the future.

Even if it turns out that a message isn't used here, I'm sure that there will still be something that I pick up from it.
User avatar
Hellbent
Posts: 2102
Joined: 23 Sep 2017, 13:34

Re: How to add a method to check if the window is (+/- minimized)

12 Jun 2019, 22:53

wolf_II wrote:
12 Jun 2019, 22:30
https://www.autohotkey.com/boards/viewtopic.php?p=232713#p232713
Lexikos wrote:A value of -32000 usually means the window is minimized.
That works.

Code: Select all

if(This.Window_Move_Obj.Current_Monitor!=This.Window_Move_Obj.Old_Monitor&&This.GetGuiPos()!=-32000)


Code: Select all

#SingleInstance,Force


Gui,1:+AlwaysOnTop +hwndGUIHWND
Gui,1:Color,444444
Gui,1:Show,w300 h150,Monitor Position Testing

;##############################################################
Monitors := New MonitorClass()
Monitors.Set_Window_Move_Timer(GUINAME:=1,GUIHWND:=GUIHWND,TCount:=300,xpOff:=350,ypOff:=100,xr:=2,yr:=1)
;##############################################################

return
GuiClose:
GuiContextMenu:
*ESC::
	ExitApp
	
*numpad1::
	(Toggle:=!Toggle)?(Monitors.Turn_Off_Window_Move_Timer()):(Monitors.Turn_On_Window_Move_Timer())
	return



;-------------------------------------------------------------------------------------------------
;-------------------------------------------------------------------------------------------------
;-------------------------------------------------------------------------------------------------

class MonitorClass	{
	__New(){
		This._SetMonCount()
		This._SetPrimeMonitor()
		This._Set_Bounds()
	}
	_SetMonCount(){
		local MC
		SysGet, MC, MonitorCount 
		This.MonitorCount := MC
	}
	_SetPrimeMonitor(){
		local PM
		SysGet, PM, MonitorPrimary
		This.PrimeMonitor := PM
	}
	_Set_Bounds(){
		local bmon,bmonLeft,bmonRight,bmonTop,bmonBottom
		This.Monitors:=[]
		Loop,% This.MonitorCount	{
			SysGet, bmon, Monitor, % A_Index
			This.Monitor[A_Index]:=	{ Left		: 	bmonLeft
									, Top		: 	bmonTop
									, Right		: 	bmonRight
									, Bottom	: 	bmonBottom 	}
		}
	}
	Get_Current_Monitor(){
		local x,y
		CoordMode,Mouse,Screen
		MouseGetPos,x,y
		Loop,% This.MonitorCount	{
			if(x>=This.Monitor[A_Index].Left&&x<=This.Monitor[A_Index].Right&&y>=This.Monitor[A_Index].Top&&y<=This.Monitor[A_Index].Bottom){
				return A_Index
			}
		}
	}
	Get_New_Window_Position(curMon,xpOff,ypOff,xr,yr){
		local tposa:="",tposa:={}
		if(xr=1) ; 1 xr = relative to the left side ; 2 xr = relative to the right side
			tposa.x:=This.Monitor[curMon].Left+xpOff
		else 
			tposa.x:=This.Monitor[curMon].Right-xpOff
		if(yr=1) ; 1 yr = relative to the top  ; 2 yr = relative to the bottom
			tposa.y:=This.Monitor[curMon].Top+ypOff
		else 
			tposa.y:=This.Monitor[curMon].Bottom-ypOff
		return tposa
	}
	Set_Window_Move_Timer(GUINAME:=1,GUIHWND:="",TCount:=500,xpOff:=0,ypOff:=0,xr:=1,yr:=1){
		
		local Window_Timer
		
		This.Window_Move_Obj:=	{ 	Interval		:	TCount
								,	GUINAME			:	GUINAME
								,	GUIHWND			:	GUIHWND
								,	XPOFF			:	xpOff
								,	YPOFF			:	ypOff
								,	XRelative		:	xr
								,	YRelative		:	yr	
								,	Current_Monitor	:	This.Get_Current_Monitor()
								,	Old_Monitor		:	This.Get_Current_Monitor()	
								,	NEW_GUI_POS		:	This.Get_New_Window_Position(This.Get_Current_Monitor(),xpOff,ypOff,xr,yr)	}
								
		This.Window_Timer := Window_Timer :=  ObjBindMethod(This, "_Window_Move_Timer")
		
		This._Set_TFTime()
		
		SetTimer,%Window_Timer%,%TCount%
		
	}
	_Window_Move_Timer(){
		This.Window_Move_Obj.Current_Monitor := This.Get_Current_Monitor()
		if(This.Window_Move_Obj.Current_Monitor!=This.Window_Move_Obj.Old_Monitor&&This.GetGuiPos()!=-32000){
			This.Window_Move_Obj.NEW_GUI_POS:=This.Get_New_Window_Position(This.Window_Move_Obj.Current_Monitor,This.Window_Move_Obj.XPOFF,This.Window_Move_Obj.YPOFF,This.Window_Move_Obj.XRelative,This.Window_Move_Obj.YRelative)
			This._Move_Window()
			This.Window_Move_Obj.Old_Monitor := This.Window_Move_Obj.Current_Monitor
		}
	}
	_Set_TFTime(){
		This.Window_Move_Obj.Current_Monitor := This.Get_Current_Monitor()
		This.Window_Move_Obj.NEW_GUI_POS:=This.Get_New_Window_Position(This.Window_Move_Obj.Current_Monitor,This.Window_Move_Obj.XPOFF,This.Window_Move_Obj.YPOFF,This.Window_Move_Obj.XRelative,This.Window_Move_Obj.YRelative)
		This.Window_Move_Obj.Old_Monitor := This.Window_Move_Obj.Current_Monitor
		This._Move_Window()
	}
	_Move_Window(){
		Gui,% This.Window_Move_Obj.GUINAME ":Show",% "x" This.Window_Move_Obj.NEW_GUI_POS.X " y" This.Window_Move_Obj.NEW_GUI_POS.Y " NA"
	}
	Turn_Off_Window_Move_Timer(){
		local Window_Timer
		Window_Timer := This.Window_Timer
		SetTimer,%Window_Timer%,Off
	}
	Turn_On_Window_Move_Timer(){
		local Window_Timer
		Window_Timer := This.Window_Timer
		This._Set_TFTime()
		SetTimer,%Window_Timer%,On
	}
	GetGuiPos(){
		local x,y
		WinGetPos,x,y,,,% "ahk_id " This.Window_Move_Obj.GUIHWND 
		return x
	}
}




Thanks.
just me
Posts: 9423
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: How to add a method to check if the window is (+/- minimized)

13 Jun 2019, 05:22

IsIconic function:

Code: Select all

If DllCall("IsIconic", "Ptr", GuiHwnd, "UInt")	; the window is minimized (iconic)
   ...
Else											; the window is not minimized
   ...
User avatar
Hellbent
Posts: 2102
Joined: 23 Sep 2017, 13:34

Re: How to add a method to check if the window is (+/- minimized)

13 Jun 2019, 18:40

just me wrote:
13 Jun 2019, 05:22
IsIconic function:

Code: Select all

If DllCall("IsIconic", "Ptr", GuiHwnd, "UInt")	; the window is minimized (iconic)
   ...
Else											; the window is not minimized
   ...
Thank you just me.

That works great.

Code: Select all

if(This.Window_Move_Obj.Current_Monitor!=This.Window_Move_Obj.Old_Monitor&&!DllCall("IsIconic", "Ptr", This.Window_Move_Obj.GUIHWND, "UInt"))

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Rohwedder and 135 guests