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
}
}