AutoHotkey Community

It is currently May 27th, 2012, 5:55 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 15 posts ] 
Author Message
PostPosted: June 7th, 2009, 4:53 am 
Offline

Joined: October 5th, 2007, 2:21 am
Posts: 138
Location: Bundaberg (Bundy), Qld, Australia
this script "throws" a window from one monitor to the other.

i know lexicos has written the excellent windowpad however my offering is a simple code snippet just for switching windows.

windows that are not minimized and will fit on both screens stay the same size. the window will stay in roughly the same relative position.
windows that are partially off screen are thrown wholly onto the other screen
windows that are to big for the screen they are being moved to are adjusted to correctly fit. they are adjusted on the x-axis, the y-axis or both
windows that fill the entire x-axis or y-axis (or both) are thrown to the other screen keeping their full state
maximized windows are thrown to the other screen in a maximized state

windows in this script are thrown using shift-m which throws the window that is under the mouse, or shift-a which throws the active window. it is much easier using a gesture program

Latest Version
Code:
SysGet, Mon1, Monitor, 1
SysGet, Mon2, Monitor, 2

+m::   ;move window under mouse
mousegetpos,,,windowtomove
gosub windowmove
return

+a::
winget,windowtomove,id,A    ;move active window
gosub windowmove
return

windowmove:
if not mon2left
return
wingetpos,x1,y1,w1,h1,ahk_id %windowtomove%
winget,winstate,minmax,ahk_id %windowtomove%
m1:=(x1+w1/2>mon1left) and (x1+w1/2<mon1right) and (y1+h1/2>mon1top) and (y1+h1/2<mon1bottom) ? 1:2   ;works out if centre of window is on monitor 1 (m1=1) or monitor 2 (m1=2)
m2:=m1=1 ? 2:1  ;m2 is the monitor the window will be moved to
ratiox:=abs(mon%m1%right-mon%m1%left)-w1<5 ? 0:abs((x1-mon%m1%left)/(abs(mon%m1%right-mon%m1%left)-w1))  ;where the window fits on x axis
ratioy:=abs(mon%m1%bottom-mon%m1%top)-h1<5 ? 0:abs((y1-mon%m1%top)/(abs(mon%m1%bottom-mon%m1%top)-h1))   ;where the window fits on y axis
x2:=mon%m2%left+ratiox*(abs(mon%m2%right-mon%m2%left)-w1)   ;where the window will fit on x axis in normal situation
y2:=mon%m2%top+ratioy*(abs(mon%m2%bottom-mon%m2%top)-h1)
w2:=w1   
h2:=h1   ;width and height will stay the same when moving unless reason not to lower in script

if abs(mon%m1%right-mon%m1%left)-w1<5 or abs(mon%m2%right-mon%m2%left-w1)<5   ;if x axis takes up whole axis OR won't fit on new screen
   {
   x2:=mon%m2%left   
   w2:=abs(mon%m2%right-mon%m2%left)
   }
if abs(mon%m1%bottom-mon%m1%top)-h1<5 or abs(mon%m2%bottom-mon%m2%top)-h1<5
   {
   y2:=mon%m2%top
   h2:=abs(mon%m2%bottom-mon%m2%top)
   }
if winstate   ;move maximized window
   {
   winrestore,ahk_id %windowtomove%
   winmove,ahk_id %windowtomove%,,mon%m2%left,mon%m2%top
   winmaximize,ahk_id %windowtomove%
   }
else
   {
   if (x1<mon%m1%left)
      x2:=mon%m2%left   ;adjustments for windows that are not fully on the initial monitor (m1)
   if (x1+w1>mon%m1%right)
      x2:=mon%m2%right-w2
   if (y1<mon%m1%top)
      y2:=mon%m2%top
   if (y1+h1>mon%m1%bottom)
      y2:=mon%m2%bottom-h2
   winmove,ahk_id %windowtomove%,,x2,y2,w2,h2   ;move non-maximized window
   }
return


Original post
Code:

SysGet, Mon1, Monitor, 1
SysGet, Mon2, Monitor, 2
return
+z::
mousegetpos,,,windowundermouse
wingetpos,x1,y1,w1,h1,ahk_id %windowundermouse%
winget,winstate,minmax,ahk_id %windowundermouse%
m1:=(x1+w1/2>mon1left) and (x1+w1/2<mon1right) ? 1:2   ;works out if centre of window is on monitor 1 (m1=1) or monitor 2 (m1=2)
m2:=m1=1 ? 2:1  ;m2 is the monitor the window will be moved to
ratiox:=mon%m1%right-mon%m1%left-w1<5 ? 0:(x1-mon%m1%left)/(mon%m1%right-mon%m1%left-w1)  ;where the window fits on x axis
ratioy:=mon%m1%bottom-mon%m1%top-h1<5 ? 0:(y1-mon%m1%top)/(mon%m1%bottom-mon%m1%top-h1)   ;where the window fits on y axis
x2:=mon%m2%left+ratiox*(mon%m2%right-mon%m2%left-w1)   ;where the window will fit on x axis in normal situation
y2:=mon%m2%top+ratioy*(mon%m2%bottom-mon%m2%top-h1)
w2:=w1   
h2:=h1   ;width and height will stay the same when moving unless reason not to lower in script
if mon%m1%right-mon%m1%left-w1<5 or mon%m2%right-mon%m2%left-w1<5   ;if x axis takes up whole axis OR won't fit on new screen
   {
   x2:=mon%m2%left   
   w2:=mon%m2%right-mon%m2%left
   }
if mon%m1%bottom-mon%m1%top-h1<5 or mon%m2%bottom-mon%m2%top-h1<5
   {
   y2:=mon%m2%top
   h2:=mon%m2%bottom-mon%m2%top
   }
if winstate   ;move maximized window
   {
   winrestore,ahk_id %windowundermouse%
   winmove,ahk_id %windowundermouse%,,mon%m2%left+ratiox*(mon%m2%right-mon%m2%left-w1),mon%m2%top+ratioy*(mon%m2%bottom-mon%m2%top-h1)
   winmaximize,ahk_id %windowundermouse%
   }
else
   {
   if (x1<mon%m1%left)
      x2:=mon%m2%left   ;adjustments for windows that are not fully on the initial monitor (m1)
   if (x1+w1>mon%m1%right)
      x2:=mon%m2%right-w2
   if (y1<mon%m1%top)
      y2:=mon%m2%top
   if (y1+h1>mon%m1%bottom)
      y2:=mon%m2%bottom-h2
   winmove,ahk_id %windowundermouse%,,x2,y2,w2,h2   ;move non-maximized window
   }
return


Last edited by rodfell on June 12th, 2011, 12:27 am, edited 5 times in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 29th, 2009, 10:13 am 
Offline

Joined: January 25th, 2008, 8:37 pm
Posts: 50
Location: Sweden
Thanks for sharing! Works great in Windows 7 32-bit.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 5th, 2009, 1:46 am 
It works perfectly in XP Pro SP3.
Thank you rodfell!


Report this post
Top
  
Reply with quote  
 Post subject: Thanks :-)
PostPosted: December 20th, 2009, 12:41 am 
Offline

Joined: October 5th, 2009, 6:02 pm
Posts: 66
Location: southampton UK
WOW!!
This works great , I can now move my multimedia apps to my tv in the front room and back without seeing the app as long as your mouse pointer is in the other screen and clicked.
Thanks , Works in XP Pro and Win 7
:D

_________________
All`s well that ends well :-)
I will not appreciate sarcastic or personal insults in reply s to my posts . Those are the acts of cowards being brave from afar .If you cant be productive don't be destructive .


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 18th, 2010, 10:44 am 
Offline

Joined: October 5th, 2007, 2:21 am
Posts: 138
Location: Bundaberg (Bundy), Qld, Australia
thanks for support.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 7th, 2010, 2:35 pm 
Offline

Joined: October 26th, 2009, 3:51 pm
Posts: 3
Location: North of France
I love you Rodfell !

Works on Win7 32bits


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 13th, 2010, 6:15 pm 
I have WIN 7 - 64bit two screens mounted vertically (Windows settings configured correctly)

This script works to throw from Monitor 1 to 2, but cannot throw back to monitor 1. Is there a fix for this?


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: August 14th, 2010, 4:05 pm 
Offline

Joined: October 5th, 2007, 2:21 am
Posts: 138
Location: Bundaberg (Bundy), Qld, Australia
i'll work on it. the problem/bug seems to arise when screen co-ords are negative numbers


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 15th, 2010, 7:58 am 
Offline

Joined: October 5th, 2007, 2:21 am
Posts: 138
Location: Bundaberg (Bundy), Qld, Australia
this seems to have fixed the bug. let me know if any problems
Code:
SysGet, Mon1, Monitor, 1
SysGet, Mon2, Monitor, 2
return
+z::
mousegetpos,,,windowundermouse
wingetpos,x1,y1,w1,h1,ahk_id %windowundermouse%
winget,winstate,minmax,ahk_id %windowundermouse%
m1:=(x1+w1/2>mon1left) and (x1+w1/2<mon1right) and (y1+h1/2>mon1top) and (y1+h1/2<mon1bottom) ? 1:2   ;works out if centre of window is on monitor 1 (m1=1) or monitor 2 (m1=2)
m2:=m1=1 ? 2:1  ;m2 is the monitor the window will be moved to
ratiox:=abs(mon%m1%right-mon%m1%left)-w1<5 ? 0:abs((x1-mon%m1%left)/(abs(mon%m1%right-mon%m1%left)-w1))  ;where the window fits on x axis
ratioy:=abs(mon%m1%bottom-mon%m1%top)-h1<5 ? 0:abs((y1-mon%m1%top)/(abs(mon%m1%bottom-mon%m1%top)-h1))   ;where the window fits on y axis
x2:=mon%m2%left+ratiox*(abs(mon%m2%right-mon%m2%left)-w1)   ;where the window will fit on x axis in normal situation
y2:=mon%m2%top+ratioy*(abs(mon%m2%bottom-mon%m2%top)-h1)
w2:=w1   
h2:=h1   ;width and height will stay the same when moving unless reason not to lower in script

if abs(mon%m1%right-mon%m1%left)-w1<5 or abs(mon%m2%right-mon%m2%left-w1)<5   ;if x axis takes up whole axis OR won't fit on new screen
   {
   x2:=mon%m2%left   
   w2:=abs(mon%m2%right-mon%m2%left)
   }
if abs(mon%m1%bottom-mon%m1%top)-h1<5 or abs(mon%m2%bottom-mon%m2%top)-h1<5
   {
   y2:=mon%m2%top
   h2:=abs(mon%m2%bottom-mon%m2%top)
   }
if winstate   ;move maximized window
   {
   winrestore,ahk_id %windowundermouse%
   winmove,ahk_id %windowundermouse%,,mon%m2%left,mon%m2%top
   winmaximize,ahk_id %windowundermouse%
   }
else
   {
   if (x1<mon%m1%left)
      x2:=mon%m2%left   ;adjustments for windows that are not fully on the initial monitor (m1)
   if (x1+w1>mon%m1%right)
      x2:=mon%m2%right-w2
   if (y1<mon%m1%top)
      y2:=mon%m2%top
   if (y1+h1>mon%m1%bottom)
      y2:=mon%m2%bottom-h2
   winmove,ahk_id %windowundermouse%,,x2,y2,w2,h2   ;move non-maximized window
   }
return


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 16th, 2010, 1:23 pm 
That did it. Very nicely done.


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: August 17th, 2010, 12:39 am 
Offline

Joined: February 17th, 2008, 8:52 pm
Posts: 314
I sometimes run with only 1 monitor so is there any way to place a check to only move the window if the second monitor is powered up.

I was trying to figure out where to place the IF statement and a message box but my placement of the code must be wrong.

If monitor 2 is enabled
move window
else
do nothing

Right now when I run the code with only a single monitor active it attempts to resize it on the same monitor. It works and window resizes on a single monitor just looks weird.

This is some great code. Thanks for sharing.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 17th, 2010, 4:02 am 
Offline

Joined: October 5th, 2007, 2:21 am
Posts: 138
Location: Bundaberg (Bundy), Qld, Australia
if only 1 monitor ...
Code:
SysGet, Mon1, Monitor, 1
SysGet, Mon2, Monitor, 2
return
+z::
if not mon2left
return
mousegetpos,,,windowundermouse
wingetpos,x1,y1,w1,h1,ahk_id %windowundermouse%
winget,winstate,minmax,ahk_id %windowundermouse%
m1:=(x1+w1/2>mon1left) and (x1+w1/2<mon1right) and (y1+h1/2>mon1top) and (y1+h1/2<mon1bottom) ? 1:2   ;works out if centre of window is on monitor 1 (m1=1) or monitor 2 (m1=2)
m2:=m1=1 ? 2:1  ;m2 is the monitor the window will be moved to
ratiox:=abs(mon%m1%right-mon%m1%left)-w1<5 ? 0:abs((x1-mon%m1%left)/(abs(mon%m1%right-mon%m1%left)-w1))  ;where the window fits on x axis
ratioy:=abs(mon%m1%bottom-mon%m1%top)-h1<5 ? 0:abs((y1-mon%m1%top)/(abs(mon%m1%bottom-mon%m1%top)-h1))   ;where the window fits on y axis
x2:=mon%m2%left+ratiox*(abs(mon%m2%right-mon%m2%left)-w1)   ;where the window will fit on x axis in normal situation
y2:=mon%m2%top+ratioy*(abs(mon%m2%bottom-mon%m2%top)-h1)
w2:=w1   
h2:=h1   ;width and height will stay the same when moving unless reason not to lower in script

if abs(mon%m1%right-mon%m1%left)-w1<5 or abs(mon%m2%right-mon%m2%left-w1)<5   ;if x axis takes up whole axis OR won't fit on new screen
   {
   x2:=mon%m2%left   
   w2:=abs(mon%m2%right-mon%m2%left)
   }
if abs(mon%m1%bottom-mon%m1%top)-h1<5 or abs(mon%m2%bottom-mon%m2%top)-h1<5
   {
   y2:=mon%m2%top
   h2:=abs(mon%m2%bottom-mon%m2%top)
   }
if winstate   ;move maximized window
   {
   winrestore,ahk_id %windowundermouse%
   winmove,ahk_id %windowundermouse%,,mon%m2%left,mon%m2%top
   winmaximize,ahk_id %windowundermouse%
   }
else
   {
   if (x1<mon%m1%left)
      x2:=mon%m2%left   ;adjustments for windows that are not fully on the initial monitor (m1)
   if (x1+w1>mon%m1%right)
      x2:=mon%m2%right-w2
   if (y1<mon%m1%top)
      y2:=mon%m2%top
   if (y1+h1>mon%m1%bottom)
      y2:=mon%m2%bottom-h2
   winmove,ahk_id %windowundermouse%,,x2,y2,w2,h2   ;move non-maximized window
   }
return


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 17th, 2010, 11:41 am 
Offline

Joined: October 5th, 2007, 2:21 am
Posts: 138
Location: Bundaberg (Bundy), Qld, Australia
added ability to throw active window (shift-a) as well as window under the mouse (shift-m).
Code:
SysGet, Mon1, Monitor, 1
SysGet, Mon2, Monitor, 2

+m::
mousegetpos,,,windowtomove
gosub windowmove
return

+a::
winget,windowtomove,id,A
gosub windowmove
return

windowmove:
if not mon2left
return
wingetpos,x1,y1,w1,h1,ahk_id %windowtomove%
winget,winstate,minmax,ahk_id %windowtomove%
m1:=(x1+w1/2>mon1left) and (x1+w1/2<mon1right) and (y1+h1/2>mon1top) and (y1+h1/2<mon1bottom) ? 1:2   ;works out if centre of window is on monitor 1 (m1=1) or monitor 2 (m1=2)
m2:=m1=1 ? 2:1  ;m2 is the monitor the window will be moved to
ratiox:=abs(mon%m1%right-mon%m1%left)-w1<5 ? 0:abs((x1-mon%m1%left)/(abs(mon%m1%right-mon%m1%left)-w1))  ;where the window fits on x axis
ratioy:=abs(mon%m1%bottom-mon%m1%top)-h1<5 ? 0:abs((y1-mon%m1%top)/(abs(mon%m1%bottom-mon%m1%top)-h1))   ;where the window fits on y axis
x2:=mon%m2%left+ratiox*(abs(mon%m2%right-mon%m2%left)-w1)   ;where the window will fit on x axis in normal situation
y2:=mon%m2%top+ratioy*(abs(mon%m2%bottom-mon%m2%top)-h1)
w2:=w1   
h2:=h1   ;width and height will stay the same when moving unless reason not to lower in script

if abs(mon%m1%right-mon%m1%left)-w1<5 or abs(mon%m2%right-mon%m2%left-w1)<5   ;if x axis takes up whole axis OR won't fit on new screen
   {
   x2:=mon%m2%left   
   w2:=abs(mon%m2%right-mon%m2%left)
   }
if abs(mon%m1%bottom-mon%m1%top)-h1<5 or abs(mon%m2%bottom-mon%m2%top)-h1<5
   {
   y2:=mon%m2%top
   h2:=abs(mon%m2%bottom-mon%m2%top)
   }
if winstate   ;move maximized window
   {
   winrestore,ahk_id %windowtomove%
   winmove,ahk_id %windowtomove%,,mon%m2%left,mon%m2%top
   winmaximize,ahk_id %windowtomove%
   }
else
   {
   if (x1<mon%m1%left)
      x2:=mon%m2%left   ;adjustments for windows that are not fully on the initial monitor (m1)
   if (x1+w1>mon%m1%right)
      x2:=mon%m2%right-w2
   if (y1<mon%m1%top)
      y2:=mon%m2%top
   if (y1+h1>mon%m1%bottom)
      y2:=mon%m2%bottom-h2
   winmove,ahk_id %windowtomove%,,x2,y2,w2,h2   ;move non-maximized window
   }
return


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 9th, 2010, 9:32 am 
with using "+a" as a hotkey, I can't write "A" in Editors any more. The change to a hotkey without using shift would be better.


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: September 10th, 2010, 12:16 pm 
Offline

Joined: October 5th, 2007, 2:21 am
Posts: 138
Location: Bundaberg (Bundy), Qld, Australia
you can change it to whatever you want. i use mouse gestures myself.


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

All times are UTC [ DST ]


Who is online

Users browsing this forum: Apollo, Bing [Bot], Exabot [Bot], Google Feedfetcher, JamixZol and 22 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:
cron
Powered by phpBB® Forum Software © phpBB Group