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 application window state (minimized or maximized)

Description
In this example its toggling the window state of the
Newsflow app from windows store (RSS-Reader).
- - - - - - - - - - - - - - - - - - - - - - - - - - -
Ihre Funktion wird dieser Anfrage automatisch beigefügt.
Wenn Sie eine Nachricht senden möchten, geben Sie sie bitte oberhalb der Trennlinie ein. Vielen Dank!
Language
C#.net
Minimum Version
Created By
Jonathan R.
Contributors
-
Date Created
Jul 14, 2023
Date Last Modified
Jul 14, 2023

Scripted Function (Macro) Code

using System;
using System.Drawing;

public static class DisplayFusionFunction
{

	/**
	 * Toggle application window state (maximize, minimize) and start the application beforehand if necessary
	 * In this example it is toggling the windows store app "Newsflow" (RSS Reader)
	 * 
	 * Toggles between:
	 * - Application window maximized and focused
	 * - Application window minimized
	 */
	public static void Run(IntPtr windowHandle)
	{
		// Change this constants accordingly
		const string APP_WIN_TITLE = "Newsflow";	// Window title (part of the window title or full title)
		const string PROC_NAME = "*Newsflow*";		// keep the leading asterisk when changing
		const string PROC_PATH = @"shell:appsfolder\6958MaxN.Newsflow_5jrsa023thkzc!App";	// absolute path to the application executable

		if (windowHandle == IntPtr.Zero)
			return;

		uint primaryMonitorID = BFS.Monitor.GetPrimaryMonitorID();

		// Check if app is running
		if (AppIsRunning(PROC_NAME))
		{
			/**
			 * app is running
			 */
			uint appId = BFS.Application.GetAppIDByFile(PROC_NAME);
			IntPtr activeWindowHandle = BFS.Window.GetFocusedWindow();
			string activeWindowTitle = BFS.Window.GetText(activeWindowHandle);
			string currentWindowTitle = BFS.Window.GetText(windowHandle);

			// check if app is to minimize or maximize
			if (activeWindowTitle.Contains(APP_WIN_TITLE) || currentWindowTitle.Contains(APP_WIN_TITLE))
			{
				// minimize app window
				BFS.Window.Minimize(activeWindowHandle);
			} else
			{
				// maximize & focus app window
				IntPtr winHandle = BFS.Application.GetMainWindowByAppID(appId);

				BFS.Window.Maximize(winHandle);
				BFS.Window.Focus(winHandle);
			}
		} else
		{
			/**
			 * app not running
			 */
			// run app
			RunApplication(PROC_NAME, PROC_PATH);
			uint appId = BFS.Application.GetAppIDByFile(PROC_NAME);
			IntPtr winHandle = BFS.Application.GetMainWindowByAppID(appId);

			// maximize & focus app window
			BFS.Window.Maximize(winHandle);
			BFS.Window.Focus(winHandle);
		}
	}

	/**
	 * Check if application is running
	 */
	private static bool AppIsRunning(string processName)
	{
		return BFS.Application.IsAppRunningByFile(processName);
	}

	/**
	 * Run application if not running
	 */
	private static void RunApplication(string processName, string processFullPath)
	{
		bool isAppRunning = BFS.Application.IsAppRunningByFile(processName);
		if (!isAppRunning)
		{
			BFS.Application.Start(processFullPath, "");
		}
	}

}