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?

Center Window Ignoring Splits (run again to restore position)

Description
Moves active window to center of monitor, overriding splits. When run again restores original position.
Position is stored per window, so any number of windows may be centered and then moved back to original position.
Set targetWidth to change window's width when centered (width is also restored when restoring position).
TIP: set targetWidth to 1920 to emulate the width of a borderless Full-HD window (very useful for ultra wides)
Language
C#.net
Minimum Version
Created By
euricog
Contributors
-
Date Created
Oct 11, 2017
Date Last Modified
Oct 11, 2017

Scripted Function (Macro) Code

using System;
using System.Drawing;

// Moves active window to center of monitor, ignoring splits
// when run again (for same window) restores original position
// position is stored per window, so any number of windows may be centered and then moved back to original position
// set targetWidth to change window's width when centered (width is also restored when restoring position)
// ***
// TIP - set targetWidth to 1920 to emulate the width of a borderless FULL-HD window (very useful for ultra wides)
// ***
public static class DisplayFusionFunction
{
	public static void Run(IntPtr windowHandle)
	{
        // general config
        string scriptID = "eg_move2center";
        int targetMonitor = 0;  // ignores splits: 0 - first monitor, 1 - second monitor, (...)
        int targetWidth = 0;    // client size (i.e.: window's inner width); zero uses same width
        
        // we need an active window
        if (windowHandle == IntPtr.Zero) {
            BFS.Dialog.ShowMessageError("No active window found.");
            return;
        }
        // get an ID for window
        string windowID = BFS.Window.BuildWindowDebug(windowHandle);
        if (string.IsNullOrEmpty(windowID)) {
            BFS.Dialog.ShowMessageError("Failed to fetch a window ID.");
            return;
        }
        
        // no windowBounds stored? store them and move to center, otherwise restore previous bounds
        string storedBoundsKey = scriptID + "_stored_window_" + windowID;
        string storedBounds = BFS.ScriptSettings.ReadValue(storedBoundsKey);
        RectangleConverter rectConverter = new RectangleConverter();
        
        // move to center and store original bounds
        if (string.IsNullOrEmpty(storedBounds)) {
            Rectangle[] monitorBounds = BFS.Monitor.GetMonitorBoundsNoSplits();
            Rectangle windowBounds = BFS.Window.GetBounds(windowHandle);
            int primaryMonitorCenter = monitorBounds[targetMonitor].Width / 2;
            if (targetWidth > 0) {
                // let's get cient (inner) width, so that borders and/or paddings are added
                Rectangle clientBounds = BFS.Window.GetClientRect(windowHandle);
                targetWidth += (windowBounds.Width - clientBounds.Width);
            }
            int windowWidth = (targetWidth > 0) ? targetWidth : windowBounds.Width;
            bool windowMoved = BFS.Window.SetSizeAndLocation(windowHandle, primaryMonitorCenter - (windowWidth / 2), windowBounds.Top, windowWidth, windowBounds.Height);
            if (windowMoved) {
                // save original bounds to allow restoring original size and position
                BFS.ScriptSettings.WriteValue(storedBoundsKey, rectConverter.ConvertToString(windowBounds));
            }
        // restore original bounds
        } else {
            Rectangle windowBounds = (Rectangle)rectConverter.ConvertFromString(storedBounds);
            bool windowMoved = BFS.Window.SetSizeAndLocation(windowHandle, windowBounds.Left, windowBounds.Top, windowBounds.Width, windowBounds.Height);
            if (windowMoved) {
                // reset storedBounds
                BFS.ScriptSettings.WriteValue(storedBoundsKey, "");
            }
        }
	}
}