using System; using System.Collections.Generic; using System.Drawing; using System.Runtime.InteropServices; // 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 { [DllImport("user32.dll", SetLastError = true)] static extern IntPtr GetWindow(IntPtr hWnd, uint uCmd); const uint GW_HWNDNEXT = 2; public static void Run(IntPtr windowHandle) { //all visable windows //use a dictionary for quick access. wish .NET 2 had a hashset Dictionary visableWindows = new Dictionary(); foreach(IntPtr window in BFS.Window.GetVisibleWindowHandles()) visableWindows.Add(window, 0); //get the monitor id that the current window is in uint currentMonitorId = BFS.Monitor.GetMonitorIDByWindow(windowHandle); //enumerate through all windows, starting with the focused window, and moving down for(IntPtr window = windowHandle; window != IntPtr.Zero; window = GetWindow(window, GW_HWNDNEXT)) { //if it isn't a visable window, continue if(!visableWindows.ContainsKey(window)) continue; //check if it's a displayfusion or hidden explorer window. if so, skip it. //this will avoid adding titlebar button and other things if(IsDisplayFusionWindowOrHiddenExplorerWindow(window)) continue; //get the monitor this window is in. if the window are in the same monitor, ignore it if(BFS.Monitor.GetMonitorIDByWindow(window) == currentMonitorId) continue; //if we got this far, that means this window is the topmost window on the other window. switch to it BFS.Window.Focus(window); break; } } private static bool IsDisplayFusionWindowOrHiddenExplorerWindow(IntPtr window) { //ignore any DisplayFusion windows (title bar buttons, etc.) //ignore pesky hidden explorer.exe windows if((BFS.Window.GetClass(window).StartsWith("DF", StringComparison.OrdinalIgnoreCase)) || (BFS.Window.GetClass(window).Equals("EdgeUiInputTopWndClass", StringComparison.OrdinalIgnoreCase)) || (BFS.Window.GetClass(window).Equals("EdgeUiInputWndClass", StringComparison.OrdinalIgnoreCase)) || (BFS.Window.GetClass(window).Equals("NativeHWNDHost", StringComparison.OrdinalIgnoreCase)) || (BFS.Window.GetClass(window).Equals("ModeInputWnd", StringComparison.OrdinalIgnoreCase)) || (BFS.Window.GetClass(window).Equals("MetroGhostWindow", StringComparison.OrdinalIgnoreCase)) || (BFS.Window.GetClass(window).Equals("ImmersiveLauncher", StringComparison.OrdinalIgnoreCase)) || (BFS.Window.GetClass(window).Equals("ApplicationManager_ImmersiveShellWindow", StringComparison.OrdinalIgnoreCase)) || (BFS.Window.GetClass(window).Equals("Shell_TrayWnd", StringComparison.OrdinalIgnoreCase)) || (BFS.Window.GetClass(window).Equals("WorkerW", StringComparison.OrdinalIgnoreCase)) || (BFS.Window.GetClass(window).Equals("Progman", StringComparison.OrdinalIgnoreCase)) || (BFS.Window.GetClass(window).Equals("SearchPane", StringComparison.OrdinalIgnoreCase))) { return true; } return false; } }