{ "name": "Swap All Topmost Windows to Next Monitor", "language": 0, "code": "using System;\r\nusing System.Collections.Generic;\r\nusing System.Drawing;\r\nusing System.Runtime.InteropServices;\r\nusing System.Linq;\r\n\r\n// The 'windowHandle' parameter will contain the window handle for the:\r\n// - Active window when run by hotkey\r\n// - Window Location target when run by a Window Location rule\r\n// - TitleBar Button owner when run by a TitleBar Button\r\n// - Jump List owner when run from a Taskbar Jump List\r\n// - Currently focused window if none of these match\r\npublic static class DisplayFusionFunction\r\n{\r\n\t[DllImport(\"user32.dll\", SetLastError = true)]\r\n\tprivate static extern IntPtr GetWindow(IntPtr hWnd, uint uCmd);\r\n\t\r\n\t//some constants\r\n\tconst uint GW_HWNDNEXT = 2;\r\n\t\r\n\tpublic static void Run(IntPtr windowHandle)\r\n\t{\r\n\t\t// A list to hold the window handles\r\n List windows = new List();\r\n\t\t\r\n\t\t// A list to store the monitor ids from left to right\r\n\t\tList ids = new List( BFS.Monitor.GetMonitorIDs() );\r\n\t\t\r\n\t\t// Enumerate through the monitors from left to right\r\n\t\t// and get the topmost window for each monitor\r\n\t\tforeach(uint id in ids)\t\t\r\n\t\t\twindows.Add(GetTopmostWindowOnMonitor(id));\r\n\t\t\r\n\t\t// Realign the monitor list by moving the last item to the front\r\n\t\tids.Insert(0, ids.LastOrDefault());\r\n\t\tids.RemoveAt(ids.Count - 1);\r\n\t\t\r\n\t\t// Move the windows\r\n\t\tfor(int i = 0; (i < ids.Count) && (i < windows.Count); i++)\t\t\r\n\t\t\tBFS.Window.MoveToMonitor(ids[i], windows[i]);\t\t\r\n\t}\r\n\t\r\n\tprivate static IntPtr GetTopmostWindowOnMonitor(uint id)\r\n\t{\r\n //get the visible window handles for that monitor\r\n HashSet visibleWindows = new HashSet( BFS.Window.GetVisibleWindowHandlesByMonitor(id) );\r\n\t\r\n //enumerate through the monitors, starting with the focused window, and moving down\r\n\t\t//only enumerate if we havn't found the windows yet\r\n\t\tfor(IntPtr window = BFS.Window.GetFocusedWindow(); ; window = GetWindow(window, GW_HWNDNEXT))\r\n\t\t{\r\n\t\t\t//check to see if there are no windows left\r\n\t\t\tif(window == IntPtr.Zero)\r\n\t\t\t\tbreak;\r\n\t\t\t\t\r\n //check to see if the window is visible. if it's not, ignore it\r\n\t\t\tif(!visibleWindows.Contains(window))\r\n continue;\r\n\t\t\t\t\r\n\t\t\t//if it is a window we should ignore, ignore it\r\n\t\t\tif(IsDisplayFusionWindowOrHiddenExplorerWindow(window))\r\n\t\t\t\tcontinue;\r\n\t\t\t\r\n\t\t\t//get the monitor this window is in\r\n\t\t\tuint monitor = BFS.Monitor.GetMonitorIDByWindow(window);\r\n\t\t\t\r\n\t\t\t//if the monitor isn't in our collection, get the next window\r\n\t\t\tif(id != monitor)\r\n\t\t\t\tcontinue;\r\n\t\t\t\t\r\n\t\t\treturn window;\r\n\t\t}\r\n\t\r\n //return IntPtr.Zero if we didn't find anything\r\n return IntPtr.Zero;\r\n\t}\r\n\t\r\n\tprivate static bool IsDisplayFusionWindowOrHiddenExplorerWindow(IntPtr window)\r\n\t{\r\n //ignore any DisplayFusion windows (title bar buttons, etc.)\r\n //ignore pesky hidden explorer.exe windows\r\n if((BFS.Window.GetClass(window).StartsWith(\"DF\", StringComparison.OrdinalIgnoreCase)) ||\r\n (BFS.Window.GetClass(window).Equals(\"EdgeUiInputTopWndClass\", StringComparison.OrdinalIgnoreCase)) ||\r\n (BFS.Window.GetClass(window).Equals(\"EdgeUiInputWndClass\", StringComparison.OrdinalIgnoreCase)) ||\r\n (BFS.Window.GetClass(window).Equals(\"NativeHWNDHost\", StringComparison.OrdinalIgnoreCase)) ||\r\n (BFS.Window.GetClass(window).Equals(\"ModeInputWnd\", StringComparison.OrdinalIgnoreCase)) ||\r\n (BFS.Window.GetClass(window).Equals(\"MetroGhostWindow\", StringComparison.OrdinalIgnoreCase)) ||\r\n (BFS.Window.GetClass(window).Equals(\"ImmersiveLauncher\", StringComparison.OrdinalIgnoreCase)) ||\r\n (BFS.Window.GetClass(window).Equals(\"ApplicationManager_ImmersiveShellWindow\", StringComparison.OrdinalIgnoreCase)) ||\r\n (BFS.Window.GetClass(window).Equals(\"Shell_TrayWnd\", StringComparison.OrdinalIgnoreCase)) ||\r\n (BFS.Window.GetClass(window).Equals(\"WorkerW\", StringComparison.OrdinalIgnoreCase)) ||\r\n (BFS.Window.GetClass(window).Equals(\"Progman\", StringComparison.OrdinalIgnoreCase)) ||\r\n (BFS.Window.GetClass(window).Equals(\"SearchPane\", StringComparison.OrdinalIgnoreCase)))\r\n {\r\n return true;\r\n }\r\n \r\n return false;\r\n\t}\r\n}", "description": "", "references": "System.Core.dll|System.Data.dll|System.dll|System.Drawing.dll|System.Management.dll|System.Web.dll|System.Windows.Forms.dll|System.Xml.dll" }