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?

Toggle Window to Center of Monitor (ignore splits)

Description
This script will move a window to the centre of the monitor, ignoring splits. When run a second time, it will move it back to the split.
Language
C#.net
Minimum Version
Created By
Thomas Malloch (BFS)
Contributors
-
Date Created
Jun 11, 2020
Date Last Modified
Jun 11, 2020

Scripted Function (Macro) Code

using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;

// 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
{
	public static void Run(IntPtr windowHandle)
	{
		/////////////////////////////////////////////////////////////////////////////////////////////
		// Monitor index from left to right. -1 = primary, 0 = leftmost, 1 = one to the right, etc //
		/////////////////////////////////////////////////////////////////////////////////////////////
        int targetMonitor = -1; 
		
		/////////////////////////////////////////////////////////////////////////////////////////////////
		// The target width of the window when it moves to the center of the target. 0 = keep the same //
		/////////////////////////////////////////////////////////////////////////////////////////////////
        int targetWidth = 0;
        		
		// A constant value to easily refer to the property we're saving
        const string PropertyName = "CenterScript_OriginalBounds";		
		
		// Try and get the window property
		IntPtr raw = BFS.Window.GetWindowProperty(windowHandle, PropertyName);
		
		// If it's IntPtr.Zero, there isn't one. Save the window position and move the window to the center of the screen
		if(raw == IntPtr.Zero)
		{
            // Get and save original window location
            raw = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(Rectangle)));
            Rectangle bounds = BFS.Window.GetBounds(windowHandle);
            Marshal.StructureToPtr(bounds, raw, false);
            BFS.Window.SetWindowProperty(windowHandle, PropertyName, raw);
            
            // If we're changing the width, do it now
            if(targetWidth != 0)
            {
                BFS.Window.SetSize(windowHandle, targetWidth, bounds.Height);
                bounds = BFS.Window.GetBounds(windowHandle);
            }
            
            // Get the bounds of our target monitor
            Rectangle monitorBounds = (targetMonitor == -1) ? Screen.PrimaryScreen.Bounds : BFS.Monitor.GetMonitorBoundsNoSplits()[targetMonitor];
			
			// Move the window
            BFS.Window.SetLocation(
                windowHandle,
                monitorBounds.X + monitorBounds.Width / 2 - bounds.Width / 2,
                monitorBounds.Y + monitorBounds.Height / 2 - bounds.Height / 2);
		}
		else // Otherwise, move the window back and clear the property
		{
            // Get the saved rectangle
            Rectangle bounds = Marshal.PtrToStructure<Rectangle>(raw);
            BFS.Window.SetSizeAndLocation(windowHandle, bounds.X, bounds.Y, bounds.Width, bounds.Height);
            
            // Clean up
            BFS.Window.RemoveWindowProperty(windowHandle, PropertyName);
            Marshal.FreeHGlobal(raw);
		}
	}
}