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?

Apply Snap

Description
Windows 10 has an irritating behaviour of making non maximized windows stand-off from monitor edges. This script works around that behaviour by snapping the window back to the edges if it's close to them. Assign to a "Window Created" trigger.
Language
C#.net
Minimum Version
Created By
Alex Vallat75938
Contributors
-
Date Created
Nov 18, 2019
Date Last Modified
Jul 19, 2021

Scripted Function (Macro) Code

using System;
using System.Drawing;

// The 'windowHandle' parameter will contain the window handle for the:
//   - Active window when run by hotkey
//   - Trigger target when run by a Trigger 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
{
    private const int SnapDistance = 10;
	public static void Run(IntPtr windowHandle)
	{
		var windowBounds = BFS.Window.GetBounds(windowHandle);
		var monitorBounds = BFS.Monitor.GetMonitorWorkAreaByWindow(windowHandle);
		
		var left = SnapTo(windowBounds.Left, monitorBounds.Left);
		var right = SnapTo(windowBounds.Right, monitorBounds.Right);
		var top = SnapTo(windowBounds.Top, monitorBounds.Top);
		var bottom = SnapTo(windowBounds.Bottom, monitorBounds.Bottom);
		
		BFS.Window.SetSizeAndLocation(windowHandle, left, top, right - left, bottom - top);
	}
	
    private static int SnapTo(int value, int snapTo)
    {
        return (Math.Abs(snapTo - value) < SnapDistance) ? snapTo : value;
    }	
}