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?

Auto-Load Desktop Icon Profile

Description
The function checks regularly, if the monitor setup has changed. In case of change it checks, if a corresponding Desktop Icon Profile exists and applies it. The profile name is built using the resolution and Y-position info of each monitor except the primary monitor where the Y-Position is 0.
I use this to automatically have the desktop icons arranged in my laptop, when I put it in a docking station or take it out.
Use the function 'Desktop Icon Profile - Auto-Save' to store your profiles with corresponding names. To make it work, place the following in your startup folder: \DisplayFusionCommand.exe -functionrun "Desktop Icon Profile - Auto-Load"
Language
C#.net
Minimum Version
Created By
Christian Treffler
Contributors
-
Date Created
Mar 1, 2016
Date Last Modified
Mar 1, 2016

Scripted Function (Macro) Code

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;

// V1.0
// by: Christian Treffler
// Thanks to kaweksl513742 whose code pointed me in the right direction to build this
// 
// The function checks regularly, if the monitor setup has changed.
// In case of change it checks, if a corresponding Desktop Icon Profile exists and applies it
// The profile name is built using the resolution and Y-position info of each monitor
// except the primary monitor where the Y-Position is 0
// 
// I use this to automatically have the desktop icons arranged in my laptop, 
// when I put it in a docking station or take it out
// 
// Use the function 'Desktop Icon Profile - Auto-Save' to store your profiles with corresponding names
// To make it work, place the following in your Autostart folder:
//	<PathToDisplayFusion>\DisplayFusionCommand.exe -functionrun "Desktop Icon Profile - Auto-Load"
//
public static class DisplayFusionFunction
{
	public static void Run(IntPtr windowHandle)
	{
		// Refresh every n mseconds
		const int sleeptime = 3000;

		// Get Resolution of all Monitors	
		Rectangle[] Monitors = BFS.Monitor.GetMonitorBounds(); 

		// strings to compare current monitor setup with the last one
		string lastsetup = buildProfileName(Monitors);
		string currentsetup = lastsetup;

		// Apply corresponding profile immediately
		CheckAndLoad(currentsetup);

		while (true)
		{
			BFS.General.Sleep(sleeptime);

			// what's the current setup
			Monitors = BFS.Monitor.GetMonitorBounds();
			currentsetup = buildProfileName(Monitors);

			if(currentsetup != lastsetup) // if changed
			{
				CheckAndLoad(currentsetup);
				lastsetup = currentsetup;
			}
		}
	}

	public static void CheckAndLoad(string setup) 
	// Checks, if a Desktop Icon Profile exists dor the current setup
	// and if yes, applies it
	{
	// Get existing profiles
		string[] profiles = BFS.DisplayFusion.GetDesktopIconsProfiles();

		for (int i = 0; i < profiles.Length; i++)
		{
			if(profiles[i] == setup) // If profiles exist
			{
				BFS.DisplayFusion.LoadDesktopIconsProfile(setup);
				break;
			}
		}
	}

	public static string buildProfileName(Rectangle[] screens)
	{
		// needs System.Text to have StringBuilder class

		StringBuilder pname = new StringBuilder();

		for (int i = 0; i < screens.Length; i++) // For each monitor
		{
			// Add monitors resolution in the format "[<Width>x<Height>, <Y>]"
			pname.Append("[");
			pname.Append(screens[i].Width);
			pname.Append("x");
			pname.Append(screens[i].Height);
			if(screens[i].X!=0 || screens[i].Y!=0) // Primary screen has coordinates [0,0]
			{
				// Add the y-coordinate, if not primary screen
				pname.Append(",");
				pname.Append(screens[i].Y);
			}
			pname.Append("]");
		}

		return pname.ToString();
	}
}