using System; using System.Drawing; // 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) { //get all of the monitor work areas Rectangle[] monitorBounds = BFS.Monitor.GetMonitorWorkAreas(); //find the size and location of the current window Rectangle windowBounds = BFS.Window.GetBounds(windowHandle); //enumerate through all of the monitor work areas until we find //which monitor the window is in Rectangle currentMonitor = Rectangle.Empty; int currentMonitorIndex = 0; foreach(Rectangle monitor in monitorBounds) { //if the X coordinate of the window isn't in this monitor, continue if(!monitor.Contains(new Point(windowBounds.X, 0))) { currentMonitorIndex++; continue; } //if we found the monitor, save the variable and exit currentMonitor = monitor; break; } //if we couldnt find the monitor, put the monitor on the leftmost monitor and exit the script if(currentMonitor.Equals(Rectangle.Empty)) { BFS.Window.SetSizeAndLocation(windowHandle, monitorBounds[0].X, monitorBounds[0].Y, monitorBounds[0].Width, monitorBounds[0].Height); return; } //if the window is positioned in the window, span the window across the two monitors if(currentMonitor.Equals(windowBounds)) { //if we are on the rightmost monitor, move it to the leftmost and exit the script if(currentMonitorIndex == monitorBounds.Length - 1) { BFS.Window.SetSizeAndLocation(windowHandle, monitorBounds[0].X, monitorBounds[0].Y, monitorBounds[0].Width, monitorBounds[0].Height); return; } //span the window across the two monitors and exit the script BFS.Window.SetSize(windowHandle, currentMonitor.Width + monitorBounds[currentMonitorIndex].Width, monitorBounds[currentMonitorIndex].Height); return; } else if((windowBounds.Width < currentMonitor.Width) || (windowBounds.Height < currentMonitor.Height)) { //if the window is smaller than the current monitor, maximize it BFS.Window.SetSizeAndLocation(windowHandle, currentMonitor.X, currentMonitor.Y, currentMonitor.Width, currentMonitor.Height); } else { //if we got this far, we need to move the window to the next monitor int nextMonitor = ++currentMonitorIndex % monitorBounds.Length; BFS.Window.SetSizeAndLocation(windowHandle, monitorBounds[nextMonitor].X, monitorBounds[nextMonitor].Y, monitorBounds[nextMonitor].Width, monitorBounds[nextMonitor].Height); } } }