Processing Ajax...

Title
Close Dialog

Message

Confirm
Close Dialog

Confirm
Close Dialog

Confirm
Close Dialog

Change Desktop Icon Size

Description
This script lets you choose the desktop icon size from a drop-down list.
Language
C#.net
Minimum Version
Created By
Thomas Malloch (BFS)
Contributors
-
Date Created
May 2, 2016
Date Last Modified
May 2, 2016

Scripted Function (Macro) Code

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Runtime.InteropServices;

// 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
{
	[DllImport("user32.dll", PreserveSig = true, SetLastError = false, CharSet = CharSet.Unicode)]
	private static extern IntPtr FindWindow(string lpClassName, IntPtr lpWindowName);

	[DllImport("user32.dll", PreserveSig = true, SetLastError = false, CharSet = CharSet.Unicode)]
	private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, IntPtr ZeroOnly);

	[DllImport("user32.dll", PreserveSig = true, SetLastError = false, CharSet = CharSet.Unicode)]
	private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, uint wParam, IntPtr lParam);

	[DllImport("user32.dll", PreserveSig = true, SetLastError = false)]
	[return: MarshalAs(UnmanagedType.Bool)]
	private static extern bool EnumWindows(EnumWindowCallback callPtr, IntPtr lParam);
	private delegate bool EnumWindowCallback(IntPtr hwnd, IntPtr lParam);
	
	private const uint WM_COMMAND = 0x0111;	
	
	public static void Run(IntPtr windowHandle)
	{
		//create a dictionary to store the icon size messages and the names of the sizes
		Dictionary<string, uint> iconSizes = new Dictionary<string, uint>
		{
			{"Small", 28752},
			{"Medium", 28750},
			{"Large", 28751}
		};
	
		//try to get the shell window
		IntPtr shell = FindShellWindow();
		
		//if we couldn't find the shell window, exit the script
		if(shell == IntPtr.Zero)
			return;
		
		//ask the user what size they would like the icons
		string selection = BFS.Dialog.GetUserInputList("What size would you like the icons?", new List<string>(iconSizes.Keys).ToArray());
		
		//if the user cancelled, exit the script
		if(string.IsNullOrEmpty(selection))
			return;
		
		//send a message to the shell window with the user's selection
		SendMessage(shell, WM_COMMAND, iconSizes[selection], IntPtr.Zero);
	}
	
	private static IntPtr FindShellWindow()
	{
		//try to find the shell window. it's either a child of Progman, or tucked away in some WorkerW
		IntPtr progman = FindWindow("Progman", IntPtr.Zero);
		IntPtr shell = FindWindowEx(progman, IntPtr.Zero, "SHELLDLL_DefView", IntPtr.Zero);
		if(shell != IntPtr.Zero)
			return shell;
		
		//enumerate through the windows until we find a WorkerW with SHELLDLL_DefView as it's child
		EnumWindows(delegate(IntPtr hwnd, IntPtr lParam)
		{
			//not a WorkerW
			if(!BFS.Window.GetClass(hwnd).Equals("WorkerW", StringComparison.OrdinalIgnoreCase))
				return true;
			
			//see if this WorkerW contains the shell window
			shell = FindWindowEx(hwnd, IntPtr.Zero, "SHELLDLL_DefView", IntPtr.Zero);
			
			//if it doesn't continue enemerating
			if(shell == IntPtr.Zero)
				return true;
				
			//if we got this far, we found the shell window. stop enumerating.
			return false;
		}, IntPtr.Zero);
			
		return shell;
	}
}