AutoHotkey Community

It is currently May 26th, 2012, 10:10 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 13 posts ] 
Author Message
 Post subject: Dual monitor swap
PostPosted: May 22nd, 2007, 10:21 pm 
Offline

Joined: May 22nd, 2007, 9:48 pm
Posts: 1
Here's a script that swaps all application windows from one monitor to another in a dual-monitor setup. It currently only works with two monitors, and they must be running at the same resolution.

I created this to work with SharpE desktop's virtual desktop switcher, which shifts virtual desktops left and right on dual monitors. I will probably try to create my own virtual desktop switcher with AutoHotkey if there is enough interest.

Here's the script:

Code:
; MonSwap - Swaps all the application windows from one monitor to another.
; v1.0
; Author: Alan Henager

SetWinDelay, 0 ; This switching should be instant

; Set this key combination to whatever.
+#s::
SwapAll:
{
  DetectHiddenWindows, Off ; I think this is default, but just for safety's sake...
  WinGet, WinArray, List ; , , , Sharp
  ; Enable the above commented out portion if you are running SharpE

  i := WinArray
  Loop, %i% {
     WinID := WinArray%A_Index%
     WinGetTitle, CurWin, ahk_id %WinID%
     If (CurWin = ) ; For some reason, CurWin <> didn't seem to work.
     {}
     else
     {
        WinGet, IsMin, MinMax, ahk_id %WinID% ; The window will re-locate even if it's minimized
        If (IsMin = -1) {
           WinRestore, ahk_id %WinID%
           SwapMon(WinID)
           WinMinimize, ahk_id %WinID%
        } else {
           SwapMon(WinID)
        }
     }
  }
  return
}

SwapMon(WinID) ; Swaps window with and ID of WinID onto the other monitor
{
  SysGet, Mon1, Monitor, 1
  SysGet, Mon2, Monitor, 2
  WinGetPos, WinX, WinY, WinWidth, , ahk_id %WinID%

  WinCenter := WinX + (WinWidth / 2) ; Determines which monitor this is on by the position of the center pixel.
  if (WinCenter > Mon1Left and WinCenter < Mon1Right) {
    WinX := Mon2Left + (WinX - Mon1Left)
  } else if (WinCenter > Mon2Left and WinCenter < Mon2Right) {
    WinX := Mon1Left + (WinX - Mon2Left)
  }

  WinMove, ahk_id %WinID%, , %WinX%, %WinY%
  return
}


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 23rd, 2007, 7:43 am 
Offline

Joined: July 12th, 2005, 1:21 pm
Posts: 633
A very similar script I wrote for 1 window (I hope I can post it in your thread):
(I assume that the smaller monitor (if any) is at the left side of the bigger one, otherwise you will have to do little codechanges)
Code:
ScreenWidth1 = 1280  ;Screen-width of smallest monitor
ScreenHeight1 = 1024   ;Screen-heigth of smallest monitor
return

MButton::
MouseGetPos, , , ActiveWin
MoveWindowToMonitor(ActiveWin, ScreenWidth1, ScreenHeight1)
return

MoveWindowToMonitor(ActiveWin, ScreenWidth1, ScreenHeight1)
{
   WinGet, SizeState, MinMax, ahk_id %ActiveWin%
   if SizeState = 1   ;Maximized
      WinRestore, ahk_id %ActiveWin%
      
   WinGetPos, WinXPos, WinYPos, WinWidth, WinHeight, ahk_id %ActiveWin%
   if WinXPos < 0      ;Move window from left (small) to right monitor (bigger)
   {
      WinMove, ahk_id %ActiveWin%, , % WinXPos + ScreenWidth1
   }
   else      ;Move window from right monitor (bigger) to small (left)
   {
      ;Calculation of new size, so window doesn't reach outside the screen of the smaller monitor
      if WinWidth > %ScreenWidth1%
         WinWidth = %ScreenWidth1%
      if WinHeight > %ScreenHeight1%
         WinHeight = %ScreenHeight1%
      WinXPosWidth := WinXPos + WinWidth
      if WinXPosWidth > %ScreenWidth1%
         WinXOffset := WinXPosWidth
      else
         WinXOffset = %ScreenWidth1%
      WinYPosHeight := WinYPos + WinHeight
      if WinYPosHeight > %ScreenHeight1%
         WinYOffset := WinYPosHeight - ScreenHeight1
      else
         WinYOffset = 0
         
      ;Resize and move window:
      WinMove, ahk_id %ActiveWin%, , % WinXPos - WinXOffset, % WinYPos - WinYOffset, %WinWidth%, %WinHeight%
   }
   if SizeState = 1   ;Restores Maximize-State
      WinMaximize, ahk_id %ActiveWin%
   WinActivate ahk_id %ActiveWin%      ;Necessary, because another window may overlap it otherwise...
}

_________________
AHK-Icon-Changer
AHK-IRC
deutsches Forum


Report this post
Top
 Profile  
Reply with quote  
 Post subject: Swap all but one?
PostPosted: September 14th, 2007, 10:41 pm 
Hey, your script is awesome Alan, but on my system I am running Ultramon. The task bar that ultramon creates, gets moved between the screens with the windows. I have been trying to figure out how to exclude the "ahk_class UltraMon Taskbar" but I cant figure it out.

I am wondering if someone might know how this could be accomplished?

On a quick side note ANYONE running dual monitors should check out Ultramon, its not free but the functionality it brings to multi displays is worth it.


Report this post
Top
  
Reply with quote  
 Post subject: Lol fixed
PostPosted: September 14th, 2007, 11:01 pm 
Lol just updated Ultramon to the 3.0Beta and now all works fine. Great script


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: September 15th, 2007, 4:07 am 
Offline

Joined: October 17th, 2006, 4:15 pm
Posts: 7502
Location: Australia
Didn't notice these scripts...

You might like to check out WindowPad, which provides similar functionality. (Win+NumpadDot to switch monitors, Win+NumpadDiv|Mult to gather all windows on monitor 2|1, and Win+Numpad* to simultaneously move and resize windows.)

Middle-click to move a window is a good idea... perhaps I'll add it to WindowPad.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 22nd, 2008, 3:50 pm 
Hi There,

I know this thread is ancient, however I was looking for a script that switched windows between monitors and this was the first one I found :).

After trying it out, I found it didn't quite do what I was after -- mainly because my monitors don't run at the same resolutions. Hence I've changed it slightly so that instead of just blindly swapping the windows between the monitors, it now swaps but maintains the relative size of the window based upon the size of the new monitor. This means that when full-screen windows are switched between the different monitors they still appear full screen when switched to a larger monitor, and don't grow outside the side of the monitor when switched to a smaller one.

The modified script is below:

Code:
; MonSwap - Swaps all the application windows from one monitor to another.
; v1.0.1
; Author: Alan Henager
;
; v1.0.1 - xenrik - Updated to use relative screen size when swapping

SetWinDelay, 0 ; This switching should be instant

; Set this key combination to whatever.
+#s::
SwapAll:
{
  DetectHiddenWindows, Off ; I think this is default, but just for safety's sake...
  WinGet, WinArray, List ; , , , Sharp
  ; Enable the above commented out portion if you are running SharpE

  i := WinArray
  Loop, %i% {
     WinID := WinArray%A_Index%
     WinGetTitle, CurWin, ahk_id %WinID%
     If (CurWin = ) ; For some reason, CurWin <> didn't seem to work.
     {}
     else
     {
        WinGet, IsMin, MinMax, ahk_id %WinID% ; The window will re-locate even if it's minimized
        If (IsMin = -1) {
           WinRestore, ahk_id %WinID%
           SwapMon(WinID)
           WinMinimize, ahk_id %WinID%
        } else {
           SwapMon(WinID)
        }
     }
  }
  return
}

SwapMon(WinID) ; Swaps window with and ID of WinID onto the other monitor
{
  SysGet, Mon1, Monitor, 1
  Mon1Width := Mon1Right - Mon1Left
  Mon1Height := Mon1Bottom - Mon1Top

  SysGet, Mon2, Monitor, 2
  Mon2Width := Mon2Right - Mon2Left
  Mon2Height := Mon2Bottom - Mon2Top

  WinGetPos, WinX, WinY, WinWidth, WinHeight, ahk_id %WinID%
  WinCenter := WinX + (WinWidth / 2)
  if (WinCenter >= Mon1Left and WinCenter <= Mon1Right) {
    NewX := (WinX - Mon1Left) / Mon1Width
    NewX := Mon2Left + (Mon2Width * NewX)

    NewWidth := WinWidth / Mon1Width
    NewWidth := Mon2Width * NewWidth

    NewY := (WinY - Mon1Top) / Mon1Height
    NewY := Mon2Top + (Mon2Height * NewY)

    NewHeight := WinHeight / Mon1Height
    NewHeight := Mon2Height * NewHeight
  } else {
    NewX := (WinX - Mon2Left) / Mon2Width
    NewX := Mon1Left + (Mon1Width * NewX)

    NewWidth := WinWidth / Mon2Width
    NewWidth := Mon1Width * NewWidth

    NewY := (WinY - Mon2Top) / Mon2Height
    NewY := Mon1Top + (Mon1Height * NewY)

    NewHeight := WinHeight / Mon2Height
    NewHeight := Mon1Height * NewHeight
  }

  WinMove, ahk_id %WinID%, , %NewX%, %NewY%, %NewWidth%, %NewHeight%
  return
}


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: December 20th, 2008, 10:03 pm 
Great script Alan. Works fine. Thank you!!


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: December 21st, 2008, 12:52 am 
Offline

Joined: October 17th, 2006, 4:15 pm
Posts: 7502
Location: Australia
xenrik wrote:
it now swaps but maintains the relative size of the window based upon the size of the new monitor.
WindowPad does this and much more.


Report this post
Top
 Profile  
Reply with quote  
 Post subject: Swap one window
PostPosted: September 11th, 2010, 12:47 pm 
Offline

Joined: September 11th, 2010, 12:36 pm
Posts: 1
Location: Pretoria, South Africa
Thanks so much for your script! :)

I hope you don't mind, but I modified it to make it only swap the window under the mouse:

Code:
SetWinDelay, 0

#RButton::
{
    MouseGetPos, , , WinID, control

    SysGet, Mon1, Monitor, 1
    SysGet, Mon2, Monitor, 2
    WinGetPos, WinX, WinY, WinWidth, , ahk_id %WinID%

    WinCenter := WinX + (WinWidth / 2) ; Determines which monitor this is on by the position of the center pixel.
    if (WinCenter > Mon1Left and WinCenter < Mon1Right) {
        WinX := Mon2Left + (WinX - Mon1Left)
    } else if (WinCenter > Mon2Left and WinCenter < Mon2Right) {
        WinX := Mon1Left + (WinX - Mon2Left)
    }

    WinMove, ahk_id %WinID%, , %WinX%, %WinY%
    return
}


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 9th, 2011, 1:05 pm 
Offline

Joined: May 27th, 2011, 8:36 pm
Posts: 7
Hi guys,

I am looking for a simple solution but none of the above code snippets are useful for me.

I have two monitors, both with the same resolution. I just want to be able to quickly move the active window from one monitor to the other.
What do I need to do?

Thanks.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 9th, 2011, 2:16 pm 
Offline

Joined: March 21st, 2007, 7:50 pm
Posts: 76
http://www.autohotkey.com/forum/viewtopic.php?t=45036


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 9th, 2011, 3:13 pm 
Offline

Joined: May 27th, 2011, 8:36 pm
Posts: 7
Thank you kiropes.

I am not a savvy AHK programmer, but did I see the code has two key combinations, +m and +a. For me, +a is working fine. I am wondering what the use of +m is.

Thanks.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 11th, 2011, 10:40 am 
Offline

Joined: October 5th, 2007, 2:21 am
Posts: 138
Location: Bundaberg (Bundy), Qld, Australia
musicgold wrote:
I am not a savvy AHK programmer, but did I see the code has two key combinations, +m and +a. For me, +a is working fine. I am wondering what the use of +m is.
It is used to move the window under the mouse to the next screen. It's to allow easy tie in with gesture programs, so that a defined gesture will make the window under the gesture move to the next screen.


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

All times are UTC [ DST ]


Who is online

Users browsing this forum: Bing [Bot], xXDarknessXx and 9 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