Processing Ajax...
Save 25% on our desktop apps during our Black Friday/Cyber Monday sale!Save 25% on our desktop apps during our Black Friday/Cyber Monday sale, including DisplayFusion, ClipboardFusion, FileSeek, LogFusion, TrayStatus, and VoiceBot!Save 25% on our desktop apps during our Black Friday/Cyber Monday sale!

Title
Close Dialog

Message

Confirm
Close Dialog

Confirm
Close Dialog

Toggle Desktop Icons

Description
This scripted function will toggle the desktop icons on and off.
Language
C#.net
Minimum Version
Created By
NetMage
Contributors
-
Date Created
Mar 4, 2016
Date Last Modified
Mar 4, 2016

Scripted Function (Macro) Code

using System;
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, UInt32 Msg, IntPtr 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 int WM_COMMAND = 0x111;
	
	private static void ToggleDesktopIcons() 
	{
		IntPtr toggleDesktopCommand = new IntPtr(0x7402);
		IntPtr shell = FindShellWindow();
		//if we failed getting the shell window, do nothing
		if(shell == IntPtr.Zero)
			return;
			
		SendMessage(shell, WM_COMMAND, toggleDesktopCommand, 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;
	}
	
	public static void Run(IntPtr windowHandle)
	{
		ToggleDesktopIcons();
	}
}