Processing Ajax...

Title
Close Dialog

Message

Confirm
Close Dialog

Confirm
Close Dialog

Move all windows on the current monitor to the selected monitor

Description
This function will prompt for a destination monitor, and move all windows from the current monitor (including minimized windows) to the selected destination monitor.
Language
C#.net
Minimum Version
Created By
Venryx
Contributors
-
Date Created
Oct 2, 2018
Date Last Modified
Oct 2, 2018

Scripted Function (Macro) Code

using System;
using System.Drawing;
using System.Collections.Generic;
using System.Runtime.InteropServices;

public static class DisplayFusionFunction
{
	public static void Run(IntPtr windowHandle)
	{
        var currentMonitor = BFS.Monitor.GetMonitorIDByXY(BFS.Input.GetMousePositionX(), BFS.Input.GetMousePositionY());
        var newMonitor = BFS.Monitor.ShowMonitorSelector();
        if (newMonitor == 0) return;
        
        // Loop through all the windows (including minimized ones) on the current monitor and move them to the monitor selected in the popup.
        foreach (IntPtr window in GetWindowsOnMonitor(currentMonitor))
        {
            var wasMinimized = BFS.Window.IsMinimized(window);
            BFS.Window.MoveToMonitor(newMonitor, window);
            if (wasMinimized)
            {
                BFS.Window.Minimize(window);
            }
        }   
	}
	
	static List<IntPtr> GetWindowsOnMonitor(uint monitorID)
	{
        var monitorRect = BFS.Monitor.GetMonitorBoundsByID(monitorID);
	
        var result = new List<IntPtr>();
        foreach (IntPtr window in BFS.Window.GetAllWindowHandles())
        {
            if (!BFS.Window.IsVisible(window)) continue;
            
            Rectangle windowRect;
            
            // If the window is minimized, we have to get the bounds using a different function. (BFS.Window.GetBounds gives an incorrect result)
            //if (placement.showCmd == (int)ShowWindowEnum.ShowMinimized)
            if (BFS.Window.IsMinimized(window))
            {
                WindowPlacement placement = new WindowPlacement();
                GetWindowPlacement(window, ref placement);
                windowRect = new Rectangle(placement.rcNormalPosition.X, placement.rcNormalPosition.Y,
                    placement.rcNormalPosition.Width - placement.rcNormalPosition.X, placement.rcNormalPosition.Height - placement.rcNormalPosition.Y);
            }
            else
            {
                windowRect = BFS.Window.GetBounds(window);
            }
            
            if (windowRect.IntersectsWith(monitorRect))
            {
                result.Add(window);
            }
        }
        return result;
	}
	
	[DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    static extern bool GetWindowPlacement(IntPtr hWnd, ref WindowPlacement lpwndpl);

    /*enum ShowWindowEnum
    {
        Hide = 0,
        ShowNormal = 1, ShowMinimized = 2, ShowMaximized = 3,
        Maximize = 3, ShowNormalNoActivate = 4, Show = 5,
        Minimize = 6, ShowMinNoActivate = 7, ShowNoActivate = 8,
        Restore = 9, ShowDefault = 10, ForceMinimized = 11
    }*/
    struct WindowPlacement
    {
        public int length;
        public int flags;
        public int showCmd;
        public System.Drawing.Point ptMinPosition;
        public System.Drawing.Point ptMaxPosition;
        public System.Drawing.Rectangle rcNormalPosition;
    }
}