Processing Ajax...

Title

Message

Confirm

Confirm

Confirm

Confirm

Are you sure you want to delete this item?

Confirm

Are you sure you want to delete this item?

Confirm

Are you sure?

Swap All Windows Between Monitors 1 and 2 (Opposite Side)

Description
This function will swap all windows between monitors 1 and 2, and place the windows on the opposite side of the destination monitor.
Language
C#.net
Minimum Version
Created By
Keith Lammers (BFS)
Contributors
-
Date Created
Feb 28, 2017
Date Last Modified
Mar 1, 2017

Scripted Function (Macro) Code

using System;
using System.Drawing;
using System.Windows.Forms;

// The 'windowHandle' parameter will contain the window handle for the:
//   - Active window when run by hotkey
//   - Window Location target when run by a Window Location rule
//   - TitleBar Button owner when run by a TitleBar Button
//   - Jump List owner when run from a Taskbar Jump List
//   - Currently focused window if none of these match
public static class DisplayFusionFunction
{
	public static void Run(IntPtr windowHandle)
	{
		//if the passed window handle doesn't exist, exit the function
		if (windowHandle == IntPtr.Zero)
			return;
		
		//get the source windows for each monitor
		IntPtr[] srcWindowsMonitor2 = BFS.Window.GetVisibleWindowHandlesByMonitor(2);
		IntPtr[] srcWindowsMonitor1 = BFS.Window.GetVisibleWindowHandlesByMonitor(1);
			
		//move all of the windows in the destination monitor to the source monitor
		foreach (IntPtr window in srcWindowsMonitor2)
		{
			//move the window to the other monitor
			BFS.Window.MoveToMonitor(1, window);
            
            //get the bounds for the window, and the monitor it's in
			Rectangle windowRect = BFS.Window.GetBounds(window);
			Rectangle monitorRect = BFS.Monitor.GetMonitorBoundsByWindow(window);
		
			//find the space on either side of the window
			int leftSpace = windowRect.X - monitorRect.X;
			int rightSpace = monitorRect.Width - (windowRect.X - monitorRect.X) - windowRect.Width;
			
			//if the leftspace is smaller, it is on the left side of monitor.
			//move it to the right
			if (leftSpace < rightSpace)
			{
				BFS.Window.SetLocation(window, monitorRect.X + monitorRect.Width - leftSpace - windowRect.Width, windowRect.Y);
				continue;
			}
			
			//if we got this far, window is on the right. move it to the left side
			monitorRect = BFS.Monitor.GetMonitorBoundsByWindow(window);
			BFS.Window.SetLocation(window, monitorRect.X + rightSpace, windowRect.Y);
		}
			
		//move all of the windows in the source monitor to the destination monitor
		foreach (IntPtr window in srcWindowsMonitor1)
		{
			//move the window to the other monitor
			BFS.Window.MoveToMonitor(2, window);
			
			//get the bounds for the window, and the monitor it's in
			Rectangle windowRect = BFS.Window.GetBounds(window);
			Rectangle monitorRect = BFS.Monitor.GetMonitorBoundsByWindow(window);
			
			//find the space on either side of the window
			int leftSpace =  windowRect.X - monitorRect.X;
			int rightSpace = monitorRect.Width - (windowRect.X - monitorRect.X) - windowRect.Width;
			
			//if the leftspace is smaller, it is on the left side of monitor.
			//move it to the right
			if (leftSpace < rightSpace)
			{
				BFS.Window.SetLocation(window, monitorRect.X + monitorRect.Width - leftSpace - windowRect.Width, windowRect.Y);
				continue;
			}
			
			//if we got this far, window is on the right. move it to the left side
			monitorRect = BFS.Monitor.GetMonitorBoundsByWindow(window);
			BFS.Window.SetLocation(window, monitorRect.X + rightSpace, windowRect.Y);
		}
	}
}