AutoHotkey Community

It is currently May 27th, 2012, 9:11 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 14 posts ] 
Author Message
PostPosted: December 4th, 2010, 11:17 pm 
Offline

Joined: April 27th, 2008, 5:28 pm
Posts: 489
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:
;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:
;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


http://www.autohotkey.net/~DataLife/XPSnap.ahk
Edit 02-23-2011 - - Added 2nd script
Edit 11-02-2011 - - Added link to the script on Autohotkey.net to enable linking with AutoHotkey.net Website Generator

_________________
Check out my scripts.
(MyIpChanger) (XPSnap) (SavePictureAs)

All my scripts are tested on Windows 7, AutoHotkey_L 32 bit Ansi unless otherwise stated.


Last edited by DataLife on February 12th, 2012, 6:50 am, edited 7 times in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 6th, 2010, 3:30 am 
Offline

Joined: April 8th, 2009, 8:23 pm
Posts: 3036
Location: Rio de Janeiro - RJ - Brasil
1) Would be great if you could auto-snap a window when it reaches a certain position, without having to relead LButton.
2) Check the behaviour I'm getting:
http://img541.imageshack.us/img541/7012/buggy0.png

_________________
"Read the manual. Read it again. Search the forum.
Try something before asking. Show what you've tried.
"
Image
Antonio França
My stuff: Google Profile


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 6th, 2010, 11:17 pm 
Offline

Joined: April 8th, 2009, 8:23 pm
Posts: 3036
Location: Rio de Janeiro - RJ - Brasil
I have created this script, which should work fine (and without any kind of flickering).

Created: 06/december/2010 20:16h BRT
Modified: -


Code:
; by MasterFocus - 06/DEC/2010 - 20:16h BRT
; http://www.autohotkey.com/forum/viewtopic.php?p=405187#405187
CoordMode, Mouse
SetBatchLines, -1
SetWinDelay, -1
ListLines, Off
Process, Priority, , High
; if the Monitor Work Area may change, put the
; following 2 lines inside the label's if-statement
SysGet, MWA_, MonitorWorkArea
scrW := MWA_Right-MWA_Left , scrH := MWA_Bottom-MWA_Top
;-------------------------------------------------
~*$LButton::
  MouseGetPos, , , hWin
  WinGet, State, MinMax, ahk_id %hWin%
  If State = 0  ; Only if the window isn't maximized/minimized
    SetTimer, MyLabel, 10
  KeyWait, LButton
  SetTimer, MyLabel, Off
  Confine(0,0,0,0,0)
Return
;-------------------------------------------------
MyLabel:
  WinGetPos, winX, winY, winW, winH, ahk_id %hWin%
  If Check(winX,winY,hWin)
  {
    MouseGetPos, mouseX, mouseY
    Confine( 1 , mouseX-winX , mouseY-winY , mouseX+scrW-winW-winX , mouseY+scrH-winH-winY )
  }
  Else
    SetTimer, MyLabel, Off ; avoid the unnecessary timed subroutine
Return
;=================================================
Check(X,Y,HWND) { ; http://www.autohotkey.com/forum/topic22178.html
   SendMessage, 0x84,, (X & 0xFFFF)|(Y & 0xFFFF) << 16 ,, ahk_id %HWND%
   Return ErrorLevel = 2 ; over the title bar
}
;=================================================
Confine(C,X1,Y1,X2,Y2) { ; http://www.autohotkey.com/forum/viewtopic.php?p=293413#293413
  VarSetCapacity(R,16,0),NumPut(X1,&R+0),NumPut(Y1,&R+4),NumPut(X2,&R+8),NumPut(Y2,&R+12)
  Return C ? DllCall("ClipCursor",UInt,&R) : DllCall("ClipCursor")
}


The script checks if the mouse is over the caption/titlebar whenever you click with LButton.
If yes, the timed subroutine keeps confining the mouse's limits until LButton is released.
It doesn't really "auto-snap" the window, but the effect is pratically the same.

_________________
"Read the manual. Read it again. Search the forum.
Try something before asking. Show what you've tried.
"
Image
Antonio França
My stuff: Google Profile


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 6th, 2010, 11:34 pm 
Offline

Joined: April 27th, 2008, 5:28 pm
Posts: 489
That is very nice. I like the feature of not be able to drag it off the screen.

One issue I see is if a window is already on one of the edges I am not able to grab the side of the window and make the window smaller. I can only make it larger.

With your script would you be able to make the window resize to half the screen when it gets to the edge? That was my whole purpose of my script was to be able to arrange two windows side by side taking up the full area of the screen

thanks
DataLife

_________________
Check out my scripts.
(MyIpChanger) (XPSnap) (SavePictureAs)

All my scripts are tested on Windows 7, AutoHotkey_L 32 bit Ansi unless otherwise stated.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 6th, 2010, 11:39 pm 
Offline

Joined: April 8th, 2009, 8:23 pm
Posts: 3036
Location: Rio de Janeiro - RJ - Brasil
DataLife wrote:
That was my whole purpose

Hmmm... Sorry, I didn't knew this behaviour since I haven't experienced "Windows 7 Snap" so far (got XP at home).
You should be able to figure something out like this:
Code:
  ...
  KeyWait, LButton
  SetTimer, MyLabel, Off
  Confine(0,0,0,0,0)
  GoSub, AnotherLabel
Return

You could add another label to check the window's position and resize it accordingly.

_________________
"Read the manual. Read it again. Search the forum.
Try something before asking. Show what you've tried.
"
Image
Antonio França
My stuff: Google Profile


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 7th, 2010, 12:19 am 
Offline

Joined: April 27th, 2008, 5:28 pm
Posts: 489
There are some nice ahk scripts out there that move the windows to the edges and resizes them very nicely. But they all use hotkeys.

I want to be able to "click and drag" to move a window to the edge then resize it to half the screen.

The problem is Windows is constantly keeping the window at its original size when it is being dragged around the screen. This is not seen until my script resizes the window when the mouse reaches the edge and when the lbutton is released.

thanks for your interest.
DataLife

_________________
Check out my scripts.
(MyIpChanger) (XPSnap) (SavePictureAs)

All my scripts are tested on Windows 7, AutoHotkey_L 32 bit Ansi unless otherwise stated.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 24th, 2011, 8:42 pm 
Offline

Joined: April 27th, 2008, 5:28 pm
Posts: 489
I added a script to the first post.

Script 1 resizes the window to half the screen when the window is dragged by the titlebar to the edge and released.

Script 2 does the same thing but also shows a preview of the area the window will take when the key is released. This one mimics Windows 7 Aero Snap functionality better.

DataLife

_________________
Check out my scripts.
(MyIpChanger) (XPSnap) (SavePictureAs)

All my scripts are tested on Windows 7, AutoHotkey_L 32 bit Ansi unless otherwise stated.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 18th, 2011, 6:04 am 
Offline
User avatar

Joined: October 19th, 2011, 2:51 pm
Posts: 23
Location: Mars
Absolutely good, I have been looking for one like this for my XP, it's really convenient :D .


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 18th, 2011, 6:13 am 
Offline

Joined: April 27th, 2008, 5:28 pm
Posts: 489
mKnight wrote:
Absolutely good, I have been looking for one like this for my XP, it's really convenient :D .


Thanks very much. I am glad someone is using this script.

_________________
Check out my scripts.
(MyIpChanger) (XPSnap) (SavePictureAs)

All my scripts are tested on Windows 7, AutoHotkey_L 32 bit Ansi unless otherwise stated.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 18th, 2011, 3:07 pm 
Offline
User avatar

Joined: May 18th, 2010, 3:10 pm
Posts: 1179
Location: Sweden
DataLife wrote:
mKnight wrote:
Absolutely good, I have been looking for one like this for my XP, it's really convenient :D .


Thanks very much. I am glad someone is using this script.


Seems very useful and I wanted to see if I could use it for something (defining more zones, for example), but it doesn't work to me on Win7. When I drag a window, it displays snap space where I want it, but the window doesn't resize when I let go.

I use two monitors, and in the zones that Aero work, I can't notice XSnap doing anything - but in the zones where Aero would do nothing ("between" the computers) nothing happens when I release.

_________________
~sumon Appifyer AHK Nova halted Recommended: AHK_L (Why?)


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 18th, 2011, 9:42 pm 
Hi, is there a way to change your script so it works in mulit monitor environment ?


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: November 18th, 2011, 11:06 pm 
Offline

Joined: April 27th, 2008, 5:28 pm
Posts: 489
sumon wrote:
Seems very useful and I wanted to see if I could use it for something (defining more zones, for example), but it doesn't work to me on Win7. When I drag a window, it displays snap space where I want it, but the window doesn't resize when I let go.

I use two monitors, and in the zones that Aero work, I can't notice XSnap doing anything - but in the zones where Aero would do nothing ("between" the computers) nothing happens when I release.
I am using Windows 7 Home Premium and both scripts above work on a single monitor with Windows Aero Snap turned off.

I really don't have time right now to attempt to make it work with multiple monitors.

I have more pressing projects at the moment, after those I will see about making this work with multiple monitors.

_________________
Check out my scripts.
(MyIpChanger) (XPSnap) (SavePictureAs)

All my scripts are tested on Windows 7, AutoHotkey_L 32 bit Ansi unless otherwise stated.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 19th, 2011, 10:37 am 
Offline
User avatar

Joined: May 18th, 2010, 3:10 pm
Posts: 1179
Location: Sweden
DataLife wrote:
sumon wrote:
Seems very useful and I wanted to see if I could use it for something (defining more zones, for example), but it doesn't work to me on Win7. When I drag a window, it displays snap space where I want it, but the window doesn't resize when I let go.

I use two monitors, and in the zones that Aero work, I can't notice XSnap doing anything - but in the zones where Aero would do nothing ("between" the computers) nothing happens when I release.
I am using Windows 7 Home Premium and both scripts above work on a single monitor with Windows Aero Snap turned off.

I really don't have time right now to attempt to make it work with multiple monitors.

I have more pressing projects at the moment, after those I will see about making this work with multiple monitors.


No worries, atleast then I know it should work with one monitor. If I have some time I'll play with it to see if I can make changes to allow for more snapzones, etc.

_________________
~sumon Appifyer AHK Nova halted Recommended: AHK_L (Why?)


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 20th, 2011, 7:20 am 
Offline

Joined: February 19th, 2010, 6:11 pm
Posts: 150
Location: California
Just so you know I wrote a script that does support multiple monitors a little while back. You can see it here:

http://www.autohotkey.com/forum/viewtopic.php?t=72848


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 14 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Rajat, SKAN and 7 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group