Processing Ajax...

Title
Close Dialog

Message

Confirm
Close Dialog

Confirm
Close Dialog

Focus Last Active Window on Other Monitor

Description
This script will focus the window that was last active on the other monitor.
Language
C#.net
Minimum Version
Created By
Joe Burke Minor Jr.57100, Thomas Malloch (BFS)
Contributors
-
Date Created
Sep 14, 2016
Date Last Modified
Aug 1, 2017

Scripted Function (Macro) Code

// This script is a modified version of Thomas Malloch's "Focus Last Active Window on Other Monitor", last modified Sep 14, 2016.
// The original script can be found here:
//     https://www.displayfusion.com/ScriptedFunctions/View/?ID=feb3f25b-de43-40d3-bddc-23a13bdde7bb
//
// The following items have been changed
//     - IsDisplayFusionWindowOrHiddenExplorerWindow now stores "BFS.Window.GetClass(window)" into a string, whereas it was executed in each IF check
//     - The typo "visable" was corrected to "visible"
//     - "using System.Drawing;" has been removed because it is not used.
//     
// Modification by wolfreak99

using System;
using System.Collections.Generic;
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 visible windows
	//use a dictionary for quick access. wish .NET 2 had a hashset
	Dictionary<IntPtr, int> visibleWindows = new Dictionary<IntPtr, int>();
	foreach(IntPtr window in BFS.Window.GetVisibleWindowHandles())
		visibleWindows.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 visible window, continue
            if (!visibleWindows.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
        string windowClass = BFS.Window.GetClass(window);
        if ((windowClass.StartsWith("DF", StringComparison.OrdinalIgnoreCase)) ||
            (windowClass.Equals("EdgeUiInputTopWndClass", StringComparison.OrdinalIgnoreCase)) ||
            (windowClass.Equals("EdgeUiInputWndClass", StringComparison.OrdinalIgnoreCase)) ||
            (windowClass.Equals("NativeHWNDHost", StringComparison.OrdinalIgnoreCase)) ||
            (windowClass.Equals("ModeInputWnd", StringComparison.OrdinalIgnoreCase)) ||
            (windowClass.Equals("MetroGhostWindow", StringComparison.OrdinalIgnoreCase)) ||
            (windowClass.Equals("ImmersiveLauncher", StringComparison.OrdinalIgnoreCase)) ||
            (windowClass.Equals("ApplicationManager_ImmersiveShellWindow", StringComparison.OrdinalIgnoreCase)) ||
            (windowClass.Equals("Shell_TrayWnd", StringComparison.OrdinalIgnoreCase)) ||
            (windowClass.Equals("WorkerW", StringComparison.OrdinalIgnoreCase)) ||
            (windowClass.Equals("Progman", StringComparison.OrdinalIgnoreCase)) ||
            (windowClass.Equals("SearchPane", StringComparison.OrdinalIgnoreCase))) {
            return true;
        }

        return false;
    }
}