Ever had the annoying problem of windows not showing up in the desktop area or hanging off of the edge of the screen? This script is especially useful for those situations that can occur if you physically change your monitor setup, screen resolution, or Alt-Tab out from a game.
This script simply requires two mouse clicks to define a box. The first click defines the upper-left corner and the second defines the lower-right corner. All windows hanging out of or not located in this region are moved and resized to fit into the user-defined box.
Code:
#SingleInstance Force ;Allow only single instance
;Use these variables to check for reasonable settings later
ReasonableWidth:=200
ReasonableHeight:=200
KeyWait LButton,T0.5 ;don't capture the mouse click that started the script
;Get upper-left corner of screen
ToolTip Select Upper-Left Corner of active screen.
KeyWait LButton,D T10
if ErrorLevel = 1 ;timed out
ExitApp
CoordMode Mouse,Screen
MouseGetPos UpperLeftX,UpperLeftY
ToolTip
KeyWait LButton,T0.5 ;wait for the mouse button to be released or 0.5 seconds
;Get lower-right corner of screen
ToolTip Select Lower-Right Corner of active screen.
KeyWait LButton,D T10
if ErrorLevel = 1 ;timed out
ExitApp
CoordMode Mouse,Screen
MouseGetPos LowerRightX,LowerRightY
ToolTip
MaxWidth := LowerRightX - UpperLeftX
MaxHeight := LowerRightY - UpperLeftY
;Check for reasonable values
if(MaxWidth < ReasonableWidth)
MaxWidth := ReasonableWidth
if(MaxHeight < ReasonableHeight)
MaxHeight := ReasonableHeight
WinGet, id, List
Loop, %id%
{
if (A_Index > 1)
{
this_id := id%A_Index% ;for this iteration of the loop
WinGetPos x,y,width,height,ahk_id %this_id%
if (width <> 0) AND (height <> 0)
{
if (width > MaxWidth)
width := MaxWidth
if (height > MaxHeight)
height := MaxHeight
if (x < UpperLeftX) OR (x > LowerRightX)
x := UpperLeftX
if (y < UpperLeftY) OR (y > LowerRightY)
y := UpperLeftY
if ( (x+width) > MaxWidth)
x := UpperLeftX
if ( (y+height) > MaxHeight)
y := UpperLeftY
WinMove ahk_id %this_id%,,%x%,%y%,%width%,%height%
}
}
}
ExitApp
Esc:: ;Esc aborts the user-defined box entry
ExitApp