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

16 Dec 2015   #50
MourningStar

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

Quote   Quote: Originally Posted by Brink View Post
Hello Marc,

That's normal when using the .bat files. They need to restart explorer to apply.
oh well, it was a nice idea - thx Shawn

p.s. if anyone comes up with a clean toggle for hide/show let me/us know -laters


My System SpecsSystem Spec
.
16 Dec 2015   #51
MourningStar

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

Quote   Quote: Originally Posted by MourningStar View Post
p.s. if anyone comes up with a clean toggle for hide/show let me/us know -laters
This is it : Desktop Icons - Hide or Show Toggle Key
My System SpecsSystem Spec
17 Dec 2015   #52
Pyprohly

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

​Because writing to the registry is not the proper way to toggle Show Desktop Icons, restarting Explorer is the only way for a batch file to get it to read newly assigned registry values. This unfortunately has the caveat of closing all Explorer windows.

Utilising C# code, PowerShell may interact with the Windows API. This allows for a "clean" way of toggling that Show Desktop Icons using a builtin script.


ToggleShowDesktopIcons.ps1
Code:
$signatures = @'
[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("user32.dll")]
public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
'@
$API = Add-Type -MemberDefinition $signatures -Name 'Win32' -Namespace 'Win32Functions' -PassThru
 
$progman_handle = $API::FindWindow('Progman', 'Program Manager')
$shellviewwin_handle = $API::FindWindowEx($progman_handle, 0, 'SHELLDLL_DefView', '')
[void]$API::SendMessage($shellviewwin_handle, 0x0111, 0x7402, 0)
My System SpecsSystem Spec
17 Dec 2015   #53
Brink

64-bit Windows 10 Pro
 
 

Great job Pyprohly.
My System SpecsSystem Spec
.

17 Dec 2015   #54
MourningStar

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

Quote   Quote: Originally Posted by Brink View Post
Great job Pyprohly.
I agree (tho I have no understanding of the posted code whatsoever ). Shawn, will this be incorporated into this tutorial, or how would I do it here at home?
My System SpecsSystem Spec
17 Dec 2015   #55
Brink

64-bit Windows 10 Pro
 
 

Yeah, I'll get it added later tonight.
My System SpecsSystem Spec
17 Dec 2015   #56
MourningStar

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

Quote   Quote: Originally Posted by Brink View Post
Yeah, I'll get it added later tonight.
My System SpecsSystem Spec
18 Dec 2015   #57
Brink

64-bit Windows 10 Pro
 
 

Marc,

I've been playing with the .ps1 file, but I'm just not sure how it's any easier than just using OPTION ONE when you still need to right click on the .ps1 file and click on "Run with PowerShell".

You could create a .bat file to run the .ps1 file with PowerShell.exe though.
My System SpecsSystem Spec
18 Dec 2015   #58
MourningStar

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

Quote   Quote: Originally Posted by Brink View Post
Marc,

I've been playing with the .ps1 file, but I'm just not sure how it's any easier than just using OPTION ONE ...
Thx for the effort. I've no clue what all is happening/involved, thought maybe the code would be incorporated into your OPTION TWO, I do not like having all my open explorers closed.

Option One and/or the app referenced in post#51 will suffice.

-thx
My System SpecsSystem Spec
20 Dec 2015   #59
Pyprohly

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

Quote   Quote: Originally Posted by Brink View Post
I've been playing with the .ps1 file, but I'm just not sure how it's any easier than just using OPTION ONE when you still need to right click on the .ps1 file and click on "Run with PowerShell".
A good idea would be to shortcut it: assign the script a hotkey. I can't imagine why anyone would want to recreate the Show Desktop Icons functionality if their intentions aren't to bind it to a hotkey.

Quote   Quote: Originally Posted by Brink View Post
You could create a .bat file to run the .ps1 file with PowerShell.exe though.
If it helps, any PowerShell script can easily be turned into a batch file by placing the below lines at the top of the script and renaming the file accordingly (with a .bat extension). (Caveat: the resulting script will not be able to accept arguments. Though, there exists another version for that.)
Code:
<# :
@powershell -Command Invoke-Expression $([String]::Join([char]10, (Get-Content \"%~f0\")))"
@goto :eof
#>
E.g.
ToggleShowDesktopIcons.ps1.bat
Code:
<# :
@powershell -Command Invoke-Expression $([String]::Join([char]10, (Get-Content \"%~f0\")))"
@goto :eof
#>
 
$signatures = @'
[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("user32.dll")]
public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
'@
$API = Add-Type -MemberDefinition $signatures -Name 'Win32' -Namespace 'Win32Functions' -PassThru
 
$progman_handle = $API::FindWindow('Progman', 'Program Manager')
$shellviewwin_handle = $API::FindWindowEx($progman_handle, 0, 'SHELLDLL_DefView', '')
[void]$API::SendMessage($shellviewwin_handle, 0x0111, 0x7402, 0)

Brink, MourningStar, the PowerShell script I posted was just a demonstration of a native solution for toggling Show Desktop Icons in a clean way, to satisfy MourningStar. I posted the PowerShell script to prove that such native solution does exist. I didn't expect both of you to want to incorporate the script in the tutorial, so I kept the script short and simple, at the price of ignoring a noteworthy bug. But if you guys wish for an proper improvement to Option Two, that can be done...

Especially with Windows 8 or newer, there is an issue with the 'ToggleShowDesktopIcons.ps1' script where it fails to toggle desktop icons on or off after the user changes their desktop wallpaper. The script works again after restarting Windows Explorer.

TL;DR,
This bug occurs because SHELLDLL_DefView (the window where the desktop icons are drawn, and the thing that gets destroyed and recreated when a desktop refresh takes place), the usual child window of the "Program Manager" shell, detaches from "Program Manager" to a new shell which has the class name of "WorkerW" and are a good number of windows that have the same class name, so locating SHELLDLL_DefView when this happens is no easy chore. I believe the reason why SHELLDLL_DefView is moved is to allow for the smooth transition effect. I'm not sure when SHELLDLL_DefView moves back to begin a child window of Program Manager, or if it does at all.

The PowerShell script below is safe from the mentioned bug. It looks full on though. And all it's doing is executing C# code.


ToggleShowDesktopIcons2.ps1
Code:
$source = @'
using System;
using System.Runtime.InteropServices;
 
namespace DesktopUtility
{
    class Win32Functions
    {
        [DllImport("user32.dll")]
        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
        [DllImport("user32.dll")]
        public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
        [DllImport("user32.dll")]
        public static extern IntPtr GetDesktopWindow();
        [DllImport("user32.dll")]
        public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
    }
 
    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)
            {
                do
                {
                    hWorkerW = Win32Functions.FindWindowEx(hDesktopWin, hWorkerW, "WorkerW", "");
                    hShellViewWin = Win32Functions.FindWindowEx(hWorkerW, IntPtr.Zero, "SHELLDLL_DefView", "");
                } while (hShellViewWin == IntPtr.Zero && hWorkerW != null);
            }
            return hShellViewWin;
        }
 
        public static void ToggleDesktopIcons()
        {
            Win32Functions.SendMessage(Desktop.GetHandle(), 0x0111, (IntPtr)0x7402, (IntPtr)0);
        }
    }
}
'@
Add-Type -TypeDefinition $source
 
[DesktopUtility.Desktop]::ToggleDesktopIcons()
Combine this script with four special lines and you've got quite a power-shell batch file.


Edit: I've attached a Batch-wrapped ToggleShowDesktopIcons2.ps1 PowerShell script to this post, and I have also included a binary version of it.

Edit 2: Fixed execution policy error message in batch file.


Attached Files
File Type: bat ToggleShowDesktopIcons.bat (1.6 KB, 69 views)
File Type: zip ToggleShowDesktopIcons.zip (2.9 KB, 22 views)
My System SpecsSystem Spec
Reply

 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
Customization
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...
Tutorials
Desktop icons play hide and go seek...
I like to keep a lot of my programs on my desktop and I group similar function programs together. Occasionally my screen will blink, the icons will change to placeholders and then sequentially replace all of the placeholders with the correct icon. Sometimes -- not very often but even once is...
General Discussion
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...
Tutorials
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 21:34.
Twitter Facebook Google+ Seven Forums iOS App Seven Forums Android App