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?

Send Window Back Under Cursor

Description
Similar to Thomas's bring window to the front from under the cursor. This works the opposite. Working with vscode and chrome dev tools all day this made it easier for me to swap between windows without having to alt+tab.
Language
C#.net
Minimum Version
Created By
MattNorton
Contributors
-
Date Created
May 6, 2024
Date Last Modified
May 6, 2024

Scripted Function (Macro) Code

using System;
using System.Drawing;
using System.Runtime.InteropServices;

public static class DisplayFusionFunction
{
    public static void Run(IntPtr windowHandle)
    {
        // Current mouse position
        Point mousePosition = new Point(BFS.Input.GetMousePositionX(), BFS.Input.GetMousePositionY());

        // Loop through visible windows
        foreach (IntPtr window in BFS.Window.GetVisibleWindowHandles())
        {
            // Skip any DisplayFusion window (title bar buttons, etc.) or the search pane
            string windowClass = BFS.Window.GetClass(window);
            if (windowClass.StartsWith("DF", StringComparison.Ordinal) || 
                windowClass.Equals("SearchPane", StringComparison.Ordinal) ||
                windowClass.Equals("ImmersiveLauncher", StringComparison.Ordinal) ||
                windowClass.Equals("Windows.UI.Core.CoreWindow", StringComparison.Ordinal) ||
                windowClass.Equals("WorkerW", StringComparison.Ordinal))
            {
                continue;
            }

            // Get the bounds of the window and see if the mouse is over it
            Rectangle bounds = BFS.Window.GetBounds(window);
            if (!bounds.Contains(mousePosition))
                continue;

            // Found the window under the mouse, send it to back
            SendWindowToBack(window);
            return; // Stop processing once the window is sent to back
        }
    }

    private static void SendWindowToBack(IntPtr hWnd)
    {
        // Using Windows API to push window to the back of the Z-order
        SetWindowPos(hWnd, HWND_BOTTOM, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
    }

    // Constants for SetWindowPos
    private static readonly IntPtr HWND_BOTTOM = new IntPtr(1);
    private const UInt32 SWP_NOMOVE = 0x0002;
    private const UInt32 SWP_NOSIZE = 0x0001;
    private const UInt32 SWP_NOACTIVATE = 0x0010;

    [DllImport("User32.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
    private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, UInt32 uFlags);
}