{ "name": "Toggle Window to Center of Monitor", "language": 0, "code": "using System;\r\nusing System.Drawing;\r\nusing System.Runtime.InteropServices;\r\n\r\n// The 'windowHandle' parameter will contain the window handle for the:\r\n// - Active window when run by hotkey\r\n// - Trigger target when run by a Trigger rule\r\n// - TitleBar Button owner when run by a TitleBar Button\r\n// - Jump List owner when run from a Taskbar Jump List\r\n// - Currently focused window if none of these match\r\npublic static class DisplayFusionFunction\r\n{\r\n\tprivate const string PropertyNameOldLocation = \"CenterWindowScriptOldLocation\";\r\n\t\r\n\tpublic static void Run(IntPtr windowHandle)\r\n\t{\r\n\t\t// Get the custom window properties\r\n\t\tnint raw = BFS.Window.GetWindowProperty(windowHandle, PropertyNameOldLocation);\r\n\t\t\t\t\r\n\t\t// We didnt have a window property stored. Center the window and save the last location\r\n\t\tif(raw == nint.Zero)\r\n\t\t{\t\t\r\n\t\t\t// Get the size of the monitor and the window\r\n\t\t\tRectangle currentMonitorBounds = BFS.Monitor.GetMonitorBoundsByWindow(windowHandle);\r\n\t\t\tRectangle windowBounds = BFS.Window.GetBounds(windowHandle);\r\n\t\t\t\r\n\t\t\t// Figure out where the center of the monitor is\r\n\t\t\tint x = currentMonitorBounds.X - windowBounds.Width / 2 + currentMonitorBounds.Width / 2;\r\n\t\t\tint y = currentMonitorBounds.Y - windowBounds.Height / 2 + currentMonitorBounds.Height / 2;\r\n\t\t\t\r\n\t\t\t// Set the windows location to the center of the monitor\r\n\t\t\tBFS.Window.SetLocation(windowHandle, x, y);\r\n\t\t\t\r\n\t\t\t// Carve out some memory to save the location to\r\n\t\t\traw = Marshal.AllocHGlobal(Marshal.SizeOf());\t\r\n\t\t\t\r\n\t\t\t// Write the location to the memory\r\n\t\t\tMarshal.StructureToPtr(windowBounds.Location, raw, false);\r\n\t\t\t\r\n\t\t\t// Set the window property to the memory we carved out\r\n\t\t\tBFS.Window.SetWindowProperty(windowHandle, PropertyNameOldLocation, raw);\r\n\t\t\t\r\n\t\t\t// Exit the script\r\n\t\t\treturn;\r\n\t\t}\r\n\t\t\r\n\t\t// We have an old location. Set the window location to the old one and remove the properties\r\n\t\t// Convert the raw memory to a Point\r\n\t\tPoint oldLocation = Marshal.PtrToStructure(raw);\r\n\t\t\r\n\t\t// Set the location\r\n\t\tBFS.Window.SetLocation(windowHandle, oldLocation.X, oldLocation.Y);\t\t\r\n\t\t\r\n\t\t// Remove the property\r\n\t\tBFS.Window.RemoveWindowProperty(windowHandle, PropertyNameOldLocation);\r\n\t\t\r\n\t\t// Free the memory\r\n\t\tMarshal.FreeHGlobal(raw);\r\n\t}\r\n}", "description": "", "references": "Microsoft.VisualBasic.Core.dll|Microsoft.Win32.Primitives.dll|Microsoft.Win32.Registry.dll|netstandard.dll|Newtonsoft.Json.dll|System.Collections.Concurrent.dll|System.Collections.dll|System.Collections.Immutable.dll|System.Collections.NonGeneric.dll|System.Collections.Specialized.dll|System.ComponentModel.Primitives.dll|System.ComponentModel.TypeConverter.dll|System.Console.dll|System.Core.dll|System.Data.dll|System.Diagnostics.Process.dll|System.dll|System.Drawing.Common.dll|System.Drawing.dll|System.Drawing.Primitives.dll|System.IO.Compression.dll|System.IO.dll|System.IO.FileSystem.Watcher.dll|System.Linq.dll|System.Linq.Expressions.dll|System.Linq.Parallel.dll|System.Linq.Queryable.dll|System.Management.dll|System.Net.dll|System.Net.Primitives.dll|System.Net.Requests.dll|System.Net.WebClient.dll|System.Net.WebHeaderCollection.dll|System.Private.CoreLib.dll|System.Private.Uri.dll|System.Private.Xml.dll|System.Runtime.dll|System.Runtime.InteropServices.dll|System.Runtime.Serialization.Formatters.dll|System.Security.Cryptography.Algorithms.dll|System.Security.Cryptography.Csp.dll|System.Security.Cryptography.dll|System.Security.Cryptography.Primitives.dll|System.Text.Json.dll|System.Text.RegularExpressions.dll|System.Threading.dll|System.Threading.Tasks.dll|System.Threading.Tasks.Parallel.dll|System.Web.dll|System.Web.HttpUtility.dll|System.Windows.Extensions.dll|System.Windows.Forms.dll|System.Windows.Forms.Primitives.dll|System.Xml.dll" }