Windows 7 Forums
Welcome to Windows 7 Forums. Our forum is dedicated to helping you find support and solutions for any problems regarding your Windows 7 PC be it Dell, HP, Acer, Asus or a custom build. We also provide an extensive Windows 7 tutorial section that covers a wide range of tips and tricks.

Windows 7: Desktop Icons - Hide or Show

26 Dec 2015   #60

Windows 10, Windows 8.1 Pro, Windows 7 Professional, OS X El Capitan

For those who want to get practical and are interested in incorporating the ToggleDesktopIcons() function into their PowerShell scripts, this post includes an extended version of the "DesktopUtility" C# snippet I've been using, and a bit of information on how to use it. Importing the "DesktopUtility" code in a PowerShell session will add the ToggleDesktopIcons() function, including five additional functions, as static methods of the [DesktopUtility.Desktop] class:
  • GetHandle() - gets the handle to the "SHELLDLL_DefView" window, whether it resides under Progman or WorkerW.
  • IsActiveWindow() - determines whether the desktop is the active window, i.e., the window the user is currently interacting with.
  • IsDesktopIconsVisible() - determines whether Windows' "Show Desktop Icons" function is enabled or not.
  • ToggleDesktopIcons() - enables "Show Desktop Icons" if it is disabled and vice versa.
  • IsDesktopControlsVisible() - determines if the main desktop window is visible.
  • ToggleDesktopControls() - turns on or off visibility for the main desktop control. Turning visibility off for the desktop control will hide icons and disable the context menu.

And here is the complete source code to import these functions (hint: use Add-Type -TypeDefiniton in PowerShell):

using System;
using System.Runtime.InteropServices;

namespace DesktopUtility
	class Win32Functions
		public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
		public static extern IntPtr GetWindow(IntPtr hWnd, uint uCmd);
		public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
		public static extern IntPtr GetDesktopWindow();
		public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
		public static extern IntPtr GetForegroundWindow();
		public static extern bool GetWindowInfo(IntPtr hwnd, ref WINDOWINFO pwi);
		public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
		public static extern bool IsWindowVisible(IntPtr hWnd);

		public struct RECT
			private int _Left;
			private int _Top;
			private int _Right;
			private int _Bottom;

		public struct WINDOWINFO
			public uint cbSize;
			public RECT rcWindow;
			public RECT rcClient;
			public uint dwStyle;
			public uint dwExStyle;
			public uint dwWindowStatus;
			public uint cxWindowBorders;
			public uint cyWindowBorders;
			public ushort atomWindowType;
			public ushort wCreatorVersion;

			public WINDOWINFO(bool? filler) : this()
				cbSize = (uint)Marshal.SizeOf(typeof(WINDOWINFO));

		public const int SW_HIDE = 0;
		public const int SW_SHOWNORMAL = 1;
		public const int SW_SHOWMINIMIZED = 2;
		public const int SW_SHOWMAXIMIZED = 3;
		public const int SW_SHOWNOACTIVATE = 4;
		public const int SW_RESTORE = 9;
		public const int SW_SHOWDEFAULT = 10;

	public class Desktop
		public static IntPtr GetHandle()
			IntPtr hDesktopWin = Win32Functions.GetDesktopWindow();
			IntPtr hProgman = Win32Functions.FindWindow("Progman", "Program Manager");
			IntPtr hWorkerW = IntPtr.Zero;

			IntPtr hShellViewWin = Win32Functions.FindWindowEx(hProgman, IntPtr.Zero, "SHELLDLL_DefView", "");
			if (hShellViewWin == IntPtr.Zero)
					hWorkerW = Win32Functions.FindWindowEx(hDesktopWin, hWorkerW, "WorkerW", "");
					hShellViewWin = Win32Functions.FindWindowEx(hWorkerW, IntPtr.Zero, "SHELLDLL_DefView", "");
				} while (hShellViewWin == IntPtr.Zero && hWorkerW != null);
			return hShellViewWin;

		public static bool IsActiveWindow()
			return Win32Functions.GetWindow(Win32Functions.GetForegroundWindow(), 5)
				== Win32Functions.GetWindow(Desktop.GetHandle(), 3);

		public static bool IsDesktopIconsVisible()
			IntPtr hWnd = Win32Functions.GetWindow(Desktop.GetHandle(), 5);
			Win32Functions.WINDOWINFO info = new Win32Functions.WINDOWINFO();
			info.cbSize = (uint)Marshal.SizeOf(info);
			Win32Functions.GetWindowInfo(hWnd, ref info);
			return (info.dwStyle & 0x10000000) == 0x10000000;

		public static void ToggleDesktopIcons()
			Win32Functions.SendMessage(Desktop.GetHandle(), 0x0111, (IntPtr)0x7402, (IntPtr)0);

		public static bool IsDesktopControlsVisible()
			return Win32Functions.IsWindowVisible(Desktop.GetHandle());

		public static void ToggleDesktopControls()
			if (Desktop.IsDesktopControlsVisible())
				Win32Functions.ShowWindowAsync(Desktop.GetHandle(), Win32Functions.SW_HIDE);
				Win32Functions.ShowWindowAsync(Desktop.GetHandle(), Win32Functions.SW_SHOWNORMAL);

An example of a practical use for the new functions would be a PowerShell script that reveals the desktop icons only when the user is interacting with the desktop. The script will immediately hide the desktop icons when the user clicks away from the desktop and is interacting with another program or application. Such PowerShell script would look something like...

$source = @'
[...DesktopUtility code here...]
Add-Type -TypeDefinition $source

function EnableDesktopControls {
	if (![DesktopUtility.Desktop]::IsDesktopControlsVisible()) {

function DisableDesktopControls {
	if ([DesktopUtility.Desktop]::IsDesktopControlsVisible()) {

while (1) {
	sleep -Milliseconds 125
	if ([DesktopUtility.Desktop]::IsActiveWindow()) {
	} else {
I've attached the above example to this post. If you like the idea of this script you may consider running the script in the background as soon as you log in to Windows.

Attached Files
File Type: ps1 HideDesktopIconsWhenDesktopNotActive.ps1 (4.2 KB, 160 views)
My System SpecsSystem Spec
26 Dec 2015   #61

Microsoft Windows 7 Home Premium 64-bit 7601 Multiprocessor Free Service Pack 1

My System SpecsSystem Spec

 Desktop Icons - Hide or Show

Thread Tools

Similar help and support threads
Thread Forum
Only show icons (and hide notifications)
The notification area customization dialog allows for 3 options per icon: Only show notifications Show icon and notifications Hide icon and notifications What about the fourth option? 4. Show icon and HIDE NOTIFICATIONS
Desktop Icons - Hide or Show Toggle Key
Is there a way I can have this function toggle via a programmed keyboard key or shortcut? -thx
General Discussion
Notification Area Icons - Hide or Show System Icons and Notifications
How to Hide or Show System Icons and Notifications in Windows 7 and Windows 8 The notification area is a part of the taskbar that provides a temporary source for notifications and status. It can also be used to display icons for system and program features that are not on the desktop. The...
Desktop Gadgets - Hide or Show
How to Hide or Show Desktop Gadgets in Windows 7 This will show you how to easily hide or show all gadgets on your desktop in Windows 7. Hide or Show Desktop Gadgets from Desktop Context Menu 1. Right click on a empty area of the desktop, and do either step 2 or 3 below for what you...
How to hide or show icons at desktop?
What kind of software do we use?
General Discussion

Our Sites

Site Links

About Us

Find Us

Windows 7 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 7" and related materials are trademarks of Microsoft Corp.

Designer Media Ltd

All times are GMT -5. The time now is 15:09.
Twitter Facebook Google+