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 string LastMoveTimeSetting = "MoveWindowScript_LastMoveTime"; private const string LastMonitorIdSetting = "MoveWindowScript_LastMonitorId"; //this is the threshold to move windows, in milliseconds private const int MoveWindowThreshold = 5000; public static void Run(IntPtr windowHandle) { //these variables will hold the information about the last move DateTime lastMoveTime; uint lastMonitorId; //if we fail to get the settings, save the settings and exit the script if(!GetSettings(out lastMoveTime, out lastMonitorId)) { SaveSettings(BFS.Monitor.GetMonitorIDByWindow(windowHandle)); return; } //if the last time the script ran is longer than the threshold, save the settings and exit the script if(DateTime.UtcNow.Subtract(lastMoveTime) > new TimeSpan(0, 0, 0, 0, MoveWindowThreshold)) { SaveSettings(BFS.Monitor.GetMonitorIDByWindow(windowHandle)); return; } //find the last monitor ID in the list of connected monitors uint[] monitorIds = BFS.Monitor.GetMonitorIDs(); int monitorIdIndex; for(monitorIdIndex = 0; monitorIdIndex < monitorIds.Length; monitorIdIndex++) { if(monitorIds[monitorIdIndex] != lastMonitorId) continue; break; } //get the next monitor ID uint nextMonitorID = monitorIds[++monitorIdIndex % monitorIds.Length]; //move the window to the selected monitor BFS.Window.MoveToMonitor(nextMonitorID, windowHandle); //save the settings SaveSettings(nextMonitorID); } //this function will get the script settings private static bool GetSettings(out DateTime lastMoveTime, out uint lastMonitorId) { lastMoveTime = DateTime.MinValue; lastMonitorId = 0; long lastMoveTicks; if(!long.TryParse(BFS.ScriptSettings.ReadValue(LastMoveTimeSetting), out lastMoveTicks)) return false; if(!uint.TryParse(BFS.ScriptSettings.ReadValue(LastMonitorIdSetting), out lastMonitorId)) return false; lastMoveTime = new DateTime(lastMoveTicks); return true; } //this function will save the script settings private static void SaveSettings(uint lastMonitorId) { BFS.ScriptSettings.WriteValue(LastMoveTimeSetting, "" + DateTime.UtcNow.Ticks); BFS.ScriptSettings.WriteValue(LastMonitorIdSetting, "" + lastMonitorId); } }