using System;
using System.Drawing;
// The 'windowHandle' parameter will contain the window handle for the:
// - Active window when run by hotkey
// - Window Location target when run by a Window Location 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 static string WindowSizeSettingsKey = "ToggleWindowSizeSettings";
public static void Run(IntPtr windowHandle)
{
//get the bounds of the window and the monitor it is in
Rectangle monitorBounds = BFS.Monitor.GetMonitorWorkAreaByWindow(windowHandle);
Rectangle windowBounds = BFS.Window.GetBounds(windowHandle);
if((windowBounds.Width == monitorBounds.Width) && (windowBounds.Height == monitorBounds.Height))
{
//if the window is the size of the monitor, load settings and restore
//load the settings
IntPtr oldWindow;
Rectangle oldBounds;
LoadSettings(out oldWindow, out oldBounds);
//check to see if the settings were loaded properly
if(oldWindow == IntPtr.Zero)
return;
//if it isn't the same window, exit
if(windowHandle.ToInt64() != oldWindow.ToInt64())
return;
//restore the window position
BFS.Window.SetSizeAndLocation(windowHandle, oldBounds.X, oldBounds.Y, oldBounds.Width, oldBounds.Height);
}
else
{
//if the window is smaller than the monitor, save settings
SaveSettings(windowHandle, windowBounds);
//make the window the size of the monitor it is in
BFS.Window.SetSizeAndLocation(windowHandle, monitorBounds.X, monitorBounds.Y, monitorBounds.Width, monitorBounds.Height);
}
}
//this function loads settings that we have previously stored
private static void LoadSettings(out IntPtr oldWindow, out Rectangle oldBounds)
{
//try and load settings
try
{
//split up the settings string into its seperate values
string[] settings = BFS.ScriptSettings.ReadValue(WindowSizeSettingsKey).Split(new char[] {'|'}, StringSplitOptions.RemoveEmptyEntries);
//set the parameters to their default values
oldWindow = IntPtr.Zero;
oldBounds = new Rectangle();
//check to see if we have enough parameters saved to the setting
if(settings.Length != 5)
throw new Exception("Incorrect settings format");
//set the values
oldWindow = new IntPtr(Convert.ToInt64(settings[0]));
oldBounds = new Rectangle(
Convert.ToInt32(settings[1]),
Convert.ToInt32(settings[2]),
Convert.ToInt32(settings[3]),
Convert.ToInt32(settings[4]));
}
catch(Exception ex)
{
//if we encountered an error, make sure the parameters are set to the default values
oldWindow = IntPtr.Zero;
oldBounds = new Rectangle();
}
}
//this function saves settings
private static void SaveSettings(IntPtr window, Rectangle bounds)
{
//make the setting string
string settings = string.Format("{0}|{1}|{2}|{3}|{4}", window.ToInt64(), bounds.X, bounds.Y, bounds.Width, bounds.Height);
//save the setting
BFS.ScriptSettings.WriteValue(WindowSizeSettingsKey, settings);
}
}