Page 1 of 1

XPSnap V1.1 Drag window to edges (Aero Snap)

Posted: 29 Sep 2013, 22:14
by DataLife
I searched the forum for a script that mimics Windows 7 Aero Snap. I found one that uses hot keys but I wanted one that uses the mouse. So I wrote one.
Click on the window titlebar and drag to any edge. The program takes into account the position and size of the taskbar.

I posted 2 scripts. The only difference between the two is that script 1 has no new window size preview and script 2 does. I also added a hotkey to exit the program on script 2.

Let me know of any problems. I only tested it in WinXP.

thanks
DataLife

Script 1 - Click titlebar and drag window to edge. Release LButton and window will resize. No new window size preview.

Code: Select all

;Usage: Click on a titlebar and drag the window to any edge of the screen. Release the button while on the edge if you want to resize the window.
CoordMode, Mouse, Screen
SetFormat,float,6.0
*~LButton::
SysGet, MonitorWorkArea, MonitorWorkArea ;get dimensions of screen minus taskbar
CoordMode, Mouse, Relative
MouseGetPos,,YPosInWindow,hwnd ;is the mouse in the titlebar of the active window
CoordMode, Mouse, Screen
Return
*~Lbutton up::
CoordMode, Mouse, Screen
MouseGetPos, XstopPos, YstopPos, ;get the position of the mouse when the left click is released
sleep 100 ;Internet Explorer does not resize properly without a delay
;Preserve a 3X3 area on each corner to prevent confusion whether to position window on which side
;If (YstopPos>MonitorWorkAreaTop+3) mouse is at least 3 from top edge
;If (YstopPos<(MonitorWorkAreaBottom-3) mouse is at least 3 from bottom edge
;If (XstopPos>(MonitorWorkAreaLeft+3)) mouse is at least 3 from left edge
;If (XstopPos<(MonitorWorkAreaRight-3)) mouse is at least 3 from right edge
;If (YPosInWindow < 29) mouse is in titlebar
;Mouse is on left edge // determined by "If (XstopPos-MonitorWorkAreaLeft)<3)"
If ((XstopPos-MonitorWorkAreaLeft)<3)and(YstopPos>MonitorWorkAreaTop+3)and(YstopPos<(MonitorWorkAreaBottom-3))and(YPosInWindow < 29)
WinMove,ahk_id %hwnd%,,%MonitorWorkAreaLeft%,%MonitorWorkAreaTop%,((MonitorWorkAreaRight-MonitorWorkAreaLeft)/2),(MonitorWorkAreaBottom-MonitorWorkAreaTop)
;Mouse on Right edge // determined by "If (XstopPos>(MonitorWorkAreaRight-3))"
If (XstopPos>(MonitorWorkAreaRight-3))and(YstopPos>MonitorWorkAreaTop+3)and(YstopPos<(MonitorWorkAreaBottom-3))and(YPosInWindow < 29)
WinMove,ahk_id %hwnd%,,((MonitorWorkAreaRight+MonitorWorkAreaLeft)/2),MonitorWorkAreaTop,((MonitorWorkAreaRight-MonitorWorkAreaLeft)/2),(MonitorWorkAreaBottom-MonitorWorkAreaTop)
;Mouse is on the bottom edge // determined by "If (YstopPos>(MonitorWorkAreaBottom-3))"
If (YstopPos>(MonitorWorkAreaBottom-3))and(YstopPos<(MonitorWorkAreaBottom+1))and(XstopPos>(MonitorWorkAreaLeft+3))and(XstopPos<(MonitorWorkAreaRight-3))and(YPosInWindow < 29)
WinMove,ahk_id %hwnd%,,MonitorWorkAreaLeft,(((MonitorWorkAreaBottom-MonitorWorkAreaTop)/2)+MonitorWorkAreaTop) ,MonitorWorkAreaRight-MonitorWorkAreaLeft,((MonitorWorkAreaBottom-MonitorWorkAreaTop)/2)
;Mouse is on the top edge // determined by "If (YstopPos<(MonitorWorkAreaTop+3))"
If (YstopPos<(MonitorWorkAreaTop+3)) and (YstopPos>(MonitorWorkAreaTop-1))and(XstopPos>(MonitorWorkAreaLeft+3))and(XstopPos<(MonitorWorkAreaRight-3))and(YPosInWindow < 29)
WinMove,ahk_id %hwnd%,,MonitorWorkAreaLeft,MonitorWorkAreaTop,MonitorWorkAreaRight-MonitorWorkAreaLeft,((MonitorWorkAreaBottom-MonitorWorkAreaTop)/2)
Script 2 - Click titlebar and drag window to edge. Release LButton and window will resize. New window size preview.

Code: Select all

;Usage: Click on a titlebar and drag the window to any edge of the screen. Release the button while on the edge if you want to resize the window.
;Hotkey to exit app: Control Alt e
#SingleInstance,Force
#NoTrayIcon
DetectHiddenWindows, On
;---------------------------------------------
;Invisible Gui with border
Gui 2: -SysMenu +ToolWindow -Border
Gui 2: Color, EEAA99
Gui 2: +LastFound
WinSet, TransColor, EEAA99
Gui 2: show, x5000 w1 h1 , GuiOutline ;show off the screen, then hide
WinGet, GuiOutline,id,GuiOutline
WinHide, ahk_id %GuiOutline%
;----------------------------------------------
*~LButton::
Proceed = 0 ;prevent GuiOutline from showing when clicking on the titlebar while the cursor is on an edge
break = 0 ;every release of the LButton up sets this to 1, so it has to be set to 0 every LButton down
SysGet, TitleBarHeight, 4 ;placed here in case height was changed after program started
SysGet, MonitorWorkArea, MonitorWorkArea ;placed here in case taskbar was moved to different side after program started
CoordMode, Mouse, Relative
MouseGetPos,,YPosInWindow,hwnd ;is mouse in titlebar ;get window id
CoordMode, Mouse, Screen
MouseGetPos,XPosStart,YPosStart
Loop
{
if break = 1
break
MouseGetPos, XPos1, YPos1,
;Hide GuiOutline if cursor is not on any edge and if mouse is in any corner
if (((XPos1>MonitorWorkAreaLeft)and(XPos1+1<MonitorWorkAreaRight))and(YPos1>MonitorWorkAreaTop and YPos1+1<MonitorWorkAreaBottom))or(XPos1=MonitorWorkAreaLeft and YPos1=MonitorWorkAreaTop)or(YPos1=MonitorWorkAreaTop and XPos1+1=MonitorWorkAreaRight)or(XPos1+1=MonitorWorkAreaRight and YPos1+1=MonitorWorkAreaBottom)or(XPos1=MonitorWorkAreaLeft and YPos1+1=MonitorWorkAreaBottom)
{
winhide,ahk_id %GuiOutline% ;hide GuiOutline when mouse is not on any edge
WinMove,ahk_id %GuiOutline%,,-1,-1, ;move hidden window offscreen, so when it is shown again on a different edge without releasing the Lbutton the window outline does not show for a split second on the previous edge it was shown on.
}
if (XPos1>MonitorWorkAreaLeft and (XPos1+1<MonitorWorkAreaRight)and(YPos1>MonitorWorkAreaTop+1)and(YPos1<MonitorWorkAreaBottom-1))
Proceed = 1 ;if the mouse starts at left or right edge and moves at least 1 and then back to the edge, GuiOutline is shown and window can resize
GetKeyState,KeyState,lbutton, P
if KeyState = D ;without this line, the GuiOutline shows up if LButton is click on the titlebar while on any edge
{
;Show GuiOutline on Left
If ((XPos1-MonitorWorkAreaLeft)<1)and(YPos1>MonitorWorkAreaTop+1)and(YPos1<(MonitorWorkAreaBottom-1))and(YPosInWindow < TitleBarHeight)and((XPosStart>XPos1)or(YPosStart< YPos1)or(Proceed = 1))
{
WinMove,ahk_id %GuiOutline%,,%MonitorWorkAreaLeft%,%MonitorWorkAreaTop%,((MonitorWorkAreaRight-MonitorWorkAreaLeft)/2),( MonitorWorkAreaBottom-MonitorWorkAreaTop)
if break = 1 ;break when Lbutton is release
break
Gui 2: show, noactivate
}
;Show GuiOutline on Right
If ((XPos1>MonitorWorkAreaRight-2)and(YPos1 > MonitorWorkAreaTop)and(YPos1<MonitorWorkAreaBottom-2)and(YPosInWindow<TitleBarHeight)and Proceed=1)
{
WinMove,ahk_id %GuiOutline%,,((MonitorWorkAreaRight+MonitorWorkAreaLeft)/2),MonitorWorkAreaTop,((MonitorWorkAreaRight-MonitorWorkAreaLeft)/2),(MonitorWorkAreaBottom-MonitorWorkAreaTop)
if break = 1 ;break when Lbutton is release
break
Gui 2: show, noactivate
}
;Show GuiOutline on Top
If (YPos1=MonitorWorkAreaTop)and(Proceed=1)and(XPos1>MonitorWorkAreaLeft)and(XPos1<MonitorWorkAreaRight-1)
{
WinMove,ahk_id %GuiOutLine%,,MonitorWorkAreaLeft,MonitorWorkAreaTop,MonitorWorkAreaRight-MonitorWorkAreaLeft,((MonitorWorkAreaBottom-MonitorWorkAreaTop)/2)
if break = 1 ;break when Lbutton is release
break
Gui 2: show, noactivate
}
;Show GuiOutline on Bottom
If (YPos1+1=MonitorWorkAreaBottom)and(Proceed=1)and(XPos1>MonitorWorkAreaLeft)and(XPos1+1<MonitorWorkAreaRight)
{
WinMove,ahk_id %GuiOutLine%,,MonitorWorkAreaLeft,(((MonitorWorkAreaBottom-MonitorWorkAreaTop)/2)+MonitorWorkAreaTop) ,MonitorWorkAreaRight-MonitorWorkAreaLeft,((MonitorWorkAreaBottom-MonitorWorkAreaTop)/2)
if break = 1 ;break when Lbutton is release
break
Gui 2: show, noactivate
}
WinSet,alwaysontop, on, ahk_id %guioutline%
}
}
Return
*~Lbutton up::
winhide,ahk_id %GuiOutline%
break = 1
;Mouse is on the left edge and not in a corner
If (XPos1=MonitorWorkAreaLeft)and(YPos1>MonitorWorkAreaTop+3)and(YPos1<(MonitorWorkAreaBottom-3))and(YPosInWindow<TitleBarHeight)and((XPosStart>XPos1)or(Proceed=1))
WinMove,ahk_id %hwnd%,,%MonitorWorkAreaLeft%,%MonitorWorkAreaTop%,((MonitorWorkAreaRight-MonitorWorkAreaLeft)/2),(MonitorWorkAreaBottom-MonitorWorkAreaTop)
;Mouse is on the right side and not in a corner
If (XPos1+1=MonitorWorkAreaRight)and(YPos1>(MonitorWorkAreaTop+3))and(YPos1<(MonitorWorkAreaBottom-3))and(YPosInWindow< TitleBarHeight)and((XPosStart<XPos1)or(Proceed=1))
WinMove,ahk_id %hwnd%,,((MonitorWorkAreaRight+MonitorWorkAreaLeft)/2),MonitorWorkAreaTop,((MonitorWorkAreaRight-MonitorWorkAreaLeft)/2),(MonitorWorkAreaBottom-MonitorWorkAreaTop)
;Mouse is on the bottom edge and not in a corner
If (YPos1 + 1 = MonitorWorkAreaBottom) and(XPos1>(MonitorWorkAreaLeft+3))and(XPos1<(MonitorWorkAreaRight-3))and(YPosInWindow<TitleBarHeight)and((YPosStart< YPos1)or(Proceed=1))
WinMove,ahk_id %hwnd%,,MonitorWorkAreaLeft,(((MonitorWorkAreaBottom-MonitorWorkAreaTop)/2)+MonitorWorkAreaTop) ,MonitorWorkAreaRight-MonitorWorkAreaLeft,((MonitorWorkAreaBottom-MonitorWorkAreaTop)/2)
;Mouse is on the top edge and not in a corner
If (YPos1 = MonitorWorkAreaTop)and(XPos1>(MonitorWorkAreaLeft+3))and(XPos1<(MonitorWorkAreaRight-3))and(YPosInWindow<titlebarheight)and((YPosStart>YPos1)or(Proceed=1))
WinMove,ahk_id %hwnd%,,MonitorWorkAreaLeft,MonitorWorkAreaTop,MonitorWorkAreaRight-MonitorWorkAreaLeft,((MonitorWorkAreaBottom-MonitorWorkAreaTop)/2)
return
^!e::
ExitApp