{ "name": "Get the Count of Windows on the Same Desktop", "language": 0, "code": "using System;\r\nusing System.Drawing;\r\nusing System.Runtime.InteropServices;\r\nusing System.Collections.Generic;\r\n\r\n// The 'windowHandle' parameter will contain the window handle for the:\r\n// - Active window when run by hotkey\r\n// - Trigger target when run by a Trigger 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 // This script makes a list of all window handles on the same virtual desktop as the windowHandle variable\r\n\tpublic static void Run(IntPtr windowHandle)\r\n\t{\r\n // Get the IVirtualDesktopManager interface\r\n CVirtualDesktopManager cmanager = new CVirtualDesktopManager();\r\n IVirtualDesktopManager manager = cmanager as IVirtualDesktopManager;\r\n if(manager == null)\r\n {\r\n BFS.Dialog.ShowMessageError(\"Unable to get IVirtualDesktopManager interface\");\r\n cmanager = null;\r\n return;\r\n } \r\n\t\r\n\t\t// Get the desktop id of the focused window\r\n\t\tGuid windowHandleDesktopID = GetWindowDesktopId(manager, windowHandle);\r\n\t\t\r\n\t\t// Make a list to store the windows when we find them\r\n\t\tList visibleWindowsInSameVirtualDesktop = new List();\r\n\t\t\r\n\t\t// Loop through all visible windows\r\n\t\tforeach(IntPtr window in BFS.Window.GetVisibleWindowHandles())\r\n\t\t{\r\n // Get the desktop id of the visible window.\r\n // If the ID is not equal to our main window, skip ahead\r\n Guid id = GetWindowDesktopId(manager, window);\r\n if(!id.Equals(windowHandleDesktopID))\r\n continue;\r\n \r\n // If we got this far, that means that the window is on the same desktop. Add it to the list\r\n visibleWindowsInSameVirtualDesktop.Add(window);\r\n\t\t}\r\n\r\n\t\t// Show a dialog with how many windows we found\r\n\t\tBFS.Dialog.ShowMessageInfo(\"There are \" + visibleWindowsInSameVirtualDesktop.Count + \" windows in the same virtual desktop as this one\");\r\n\t\tcmanager = null;\r\n\t}\t\r\n\t\r\n\t// This function returns the desktop id of the given window handle\r\n\t// You can pass in a reference to IVirtualDesktopManager, but if it doesn't exist, it will just make it itself\r\n\t// https://stackoverflow.com/questions/27977924/in-windows-10-how-can-we-determine-which-virtual-desktop-a-window-belongs-to\r\n\tpublic static Guid GetWindowDesktopId(IVirtualDesktopManager manager, IntPtr window)\r\n { \r\n if(manager == null)\r\n {\r\n CVirtualDesktopManager cmanager = new CVirtualDesktopManager();\r\n manager = cmanager as IVirtualDesktopManager;\r\n if(manager == null)\r\n return Guid.Empty;\r\n }\r\n \r\n Guid result;\r\n int hr = manager.GetWindowDesktopId(window, out result); \r\n return (hr == 0) ? result : Guid.Empty;\r\n }\r\n\t\r\n\t// COM stuff from https://stackoverflow.com/questions/27977924/in-windows-10-how-can-we-determine-which-virtual-desktop-a-window-belongs-to\r\n\t[ComImport]\r\n\t[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]\r\n\t[Guid(\"a5cd92ff-29be-454c-8d04-d82879fb3f1b\")]\r\n public interface IVirtualDesktopManager\r\n {\r\n [PreserveSig]\r\n int IsWindowOnCurrentVirtualDesktop(\r\n [In] IntPtr TopLevelWindow,\r\n [Out] out int OnCurrentDesktop\r\n );\r\n [PreserveSig]\r\n int GetWindowDesktopId(\r\n [In] IntPtr TopLevelWindow,\r\n [Out] out Guid CurrentDesktop\r\n );\r\n\r\n [PreserveSig]\r\n int MoveWindowToDesktop(\r\n [In] IntPtr TopLevelWindow,\r\n [MarshalAs(UnmanagedType.LPStruct)]\r\n [In]Guid CurrentDesktop\r\n );\r\n }\r\n\r\n [ComImport]\r\n [Guid(\"aa509086-5ca9-4c25-8f95-589d3c07b48a\")]\r\n public class CVirtualDesktopManager\r\n {\r\n\r\n }\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" }