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);
}