The example below does not have the custom GUI included for simplicity sake. If anyone has any ideas to make it better / cleaner, I'm open to ideas. I have a dual monitor setup that I have been testing on but I would like to see if it fails in different configurations. It will be used on around 20 different computers at work. Some might have 3 or 4 monitors.
Code: Select all
#SingleInstance, Force
SendMode Input
SetWorkingDir %A_ScriptDir%
Gui, +Resize +LastFound -MaximizeBox
Gui, Add, Button,x200 y8 w60 vMaxButton gMaxIt, Maximize
Gui, Add, Button,x200 y8 w60 vRestoreButton gMaxIt, Restore
GuiControl, Hide, RestoreButton
Gui, Show, h200, Max Test
Return
MaxIt:
If (Maxed = "TRUE")
RestoreIt()
Else
MaximizeIt()
Return
GuiClose:
ExitApp
Return
MaximizeIt()
{
Global CurrentWinW, CurrentWinH, CurrentWinX, CurrentWinY, Maxed
GuiControl, Hide, MaxButton
GuiControl, Show, RestoreButton
MonitorFound := "FALSE"
WinGetActiveStats, CurrentWinTitle, CurrentWinW, CurrentWinH, CurrentWinX, CurrentWinY
; Get the location of the right edge of the window.
CurrentWinRight := (CurrentWinX + CurrentWinW)
; Determine the middle of the window in case it is between monitors.
MidWinX := (CurrentWinX + (CurrentWinW / 2))
MidWinY := (CurrentWinY + (CurrentWinH / 2))
If MidWinX = 0
MidWinX := 1
If MidWinY = 0
MidWinY := 1
; Find which monitor the window is on.
SysGet, numDisplays, MonitorCount
If (MonitorFound = "FALSE")
{
Loop %numDisplays%
{
; Get the full monitor dimensions.
SysGet, MonitorFull, Monitor, %A_Index%
If (MidWinX >= MonitorFullLeft)
If (MidWinY >= MonitorFullTop)
If (MidWinX <= MonitorFullRight)
If (MidWinY <= MonitorFullBottom)
{
CurrentMonitor := A_Index
MonitorFound := "TRUE"
}
}
}
; If monitor is not found because the left or bottom is off the screen, look for the right and top.
If (MonitorFound = "FALSE")
{
Loop %numDisplays%
{
SysGet, MonitorFull, Monitor, %A_Index%
If (CurrentWinRight >= MonitorFullLeft)
If (CurrentWinY >= MonitorFullTop)
If (CurrentWinRight <= MonitorFullRight)
If (CurrentWinY <= MonitorFullBottom)
{
CurrentMonitor := A_Index
MonitorFound := "TRUE"
}
}
}
; Get the monitor working area dimensions (minus taskbar, etc.).
SysGet, MonitorWork, MonitorWorkArea, %CurrentMonitor%
; Adjust for borders.
WinX := (MonitorWorkLeft + 5)
WinY := (MonitorWorkTop + 5)
WinWidth := (MonitorWorkRight - MonitorWorkLeft) - 10
WinHeight := (MonitorWorkBottom - MonitorWorkTop) - 10
Gui,-Resize
WinMove,,, %WinX%, %WinY%, %WinWidth%, %WinHeight%
Maxed := "TRUE"
}
RestoreIt()
{
Global CurrentWinW, CurrentWinH, CurrentWinX, CurrentWinY, Maxed
GuiControl, Show, MaxButton
GuiControl, Hide, RestoreButton
Gui,+Resize -MaximizeBox
WinMove,,, %CurrentWinX%, %CurrentWinY%, %CurrentWinW%, %CurrentWinH%
Maxed := "FALSE"
}