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?

List Open Windows and Select to Focus

Description
This script will show a dialog with all of the open windows with their monitor ID appended. Selecting one will move it to the mouse cursor monitor and give it focus.
Language
C#.net
Minimum Version
Created By
Keith Lammers (BFS)
Contributors
-
Date Created
Feb 23, 2022
Date Last Modified
Feb 23, 2022

Scripted Function (Macro) Code

using System;
using System.Drawing;
using System.Text.RegularExpressions;

public static class DisplayFusionFunction
{
	public static void Run(IntPtr windowHandle)
	{
		// Get the monitor ID of the monitor that the mouse is currently on
		uint currentMonitorID = BFS.Monitor.GetMonitorIDByXY(BFS.Input.GetMousePositionX(), BFS.Input.GetMousePositionY());
		
		// Get the visible and minimized windows
		IntPtr[] windowHandles = BFS.Window.GetVisibleAndMinimizedWindowHandles();
		
		// Loop through the window handles to get their window titles
		string[] windowTitles = new string[windowHandles.Length];
		for (int i = 0; i < windowHandles.Length; i++)
		{
			windowTitles[i] = BFS.Window.GetText(windowHandles[i]);
		}
		
		// Remove the empty window titles and add the monitor ID for each one
		string[] windowTitlesNonEmpty = new string[windowTitles.Length];
		for (int i = 0; i < windowHandles.Length; i++)
		{
			if (windowTitles[i].Length > 0)
			{
				windowTitlesNonEmpty[i] = windowTitles[i] + " :" + BFS.Monitor.GetMonitorIDByWindow(windowHandles[i]).ToString();	
			}
		}
		
		// Prompt the user to select one
		string selectedWindowTitle = BFS.Dialog.GetUserInputListViewWithFilter("Select the window to focus...", windowTitlesNonEmpty);
		
		// Strip the monitor ID from the selected window title
		selectedWindowTitle = Regex.Replace(selectedWindowTitle, @"\s:\d+$", string.Empty);
		
		// Move the window to the current monitor and give it focus
		int selectedWindowIndex = Array.IndexOf(windowTitles, selectedWindowTitle);
		if (selectedWindowIndex >= 0)
		{
			BFS.Window.MoveToMonitor(currentMonitorID, windowHandles[selectedWindowIndex]);
			BFS.Window.Focus(windowHandles[selectedWindowIndex]);
		}
		else
		{
			BFS.Dialog.ShowMessageError("Something went wrong, can't find the selected window's index.");	
		}
	}
}