Processing Ajax...

Title

Message

Confirm

Confirm

Confirm

Confirm

Are you sure you want to delete this item?

Confirm

Are you sure you want to delete this item?

Confirm

Are you sure?

Close Topmost Window on Specific Monitor

Description
This script will close the topmost window on the monitor specified on line 17.
Language
C#.net
Minimum Version
Created By
Keith Lammers (BFS)
Contributors
Thomas Malloch (BFS)
Date Created
Feb 9, 2022
Date Last Modified
Feb 9, 2022

Scripted Function (Macro) Code

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

public static class DisplayFusionFunction
{
	[DllImport("user32.dll", SetLastError = true)]
	private static extern IntPtr GetWindow(IntPtr hWnd, uint uCmd);
	
	// Some constants
	const uint GW_HWNDNEXT = 2;
	
	public static void Run(IntPtr windowHandle)
	{
        // Set the monitor ID here
        uint monitorID = 2;
        
        // Find the topmost window on the monitor
        IntPtr window = GetTopmostWindowOnMonitor(monitorID);
        
        // Close the window
        BFS.Window.Close(window);
    }
	
	private static IntPtr GetTopmostWindowOnMonitor(uint id)
	{
        // Get the visible window handles for that monitor
        HashSet<IntPtr> visibleWindows = new HashSet<IntPtr>( BFS.Window.GetVisibleWindowHandlesByMonitor(id) );
	
        // Enumerate through the monitors, starting with the focused window, and moving down
		// Only enumerate if we havn't found the windows yet
		for(IntPtr window = BFS.Window.GetFocusedWindow(); ; window = GetWindow(window, GW_HWNDNEXT))
		{
			// Check to see if there are no windows left
			if(window == IntPtr.Zero)
				break;
				
            // Check to see if the window is visible. if it's not, ignore it
			if(!visibleWindows.Contains(window))
                continue;
				
			// If it is a window we should ignore, ignore it
			if(IsDisplayFusionWindowOrHiddenExplorerWindow(window))
				continue;
			
			// Get the monitor this window is in
			uint monitor = BFS.Monitor.GetMonitorIDByWindow(window);
			
			// If the monitor isn't in our collection, get the next window
			if(id != monitor)
				continue;
				
			return window;
		}
	
        // Return IntPtr.Zero if we didn't find anything
        return IntPtr.Zero;
	}
	
	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;
	}
}