14

How can I show/hide the desktop icons programmatically, using C#?

I'm trying to create an alternative desktop, which uses widgets, and I need to hide the old icons.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Tibi
  • 4,015
  • 8
  • 40
  • 64
  • I've posted a [C++ answer](https://stackoverflow.com/a/53347282/7571258) to a similar question, maybe someone can translate it to C#. – zett42 Nov 17 '18 at 01:11

7 Answers7

26

You can do this using the Windows API. Here is sample code in C# that will toggle desktop icons.

    [DllImport("user32.dll", SetLastError = true)] static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
    [DllImport("user32.dll", SetLastError = true)] static extern IntPtr GetWindow(IntPtr hWnd, GetWindow_Cmd uCmd);
    enum GetWindow_Cmd : uint
    {
        GW_HWNDFIRST = 0,
        GW_HWNDLAST = 1,
        GW_HWNDNEXT = 2,
        GW_HWNDPREV = 3,
        GW_OWNER = 4,
        GW_CHILD = 5,
        GW_ENABLEDPOPUP = 6
    }
    [DllImport("user32.dll", CharSet = CharSet.Auto)] static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);

    private const int WM_COMMAND = 0x111;

    static void ToggleDesktopIcons()
    {
        var toggleDesktopCommand = new IntPtr(0x7402);
        IntPtr hWnd = GetWindow(FindWindow("Progman", "Program Manager"), GetWindow_Cmd.GW_CHILD);
        SendMessage(hWnd, WM_COMMAND, toggleDesktopCommand, IntPtr.Zero);
    }

This sends a message to the SHELLDLL_DefView child window of Progman, which tells it to toggle visibility (by adding or removing the WS_VISIBLE style) of it's only child, "FolderView". "FolderView" is the actual window that contains the icons.

To test to see if icons are visible or not, you can query for the WS_VISIBLE style by using the GetWindowInfo function, shown below:

    [return: MarshalAs(UnmanagedType.Bool)]
    [DllImport("user32.dll", SetLastError = true)]
    private static extern bool GetWindowInfo(IntPtr hwnd, ref WINDOWINFO pwi);

    [StructLayout(LayoutKind.Sequential)]
    public struct RECT
    {
        private int _Left;
        private int _Top;
        private int _Right;
        private int _Bottom;
    }

    [StructLayout(LayoutKind.Sequential)]
    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(Boolean? filler)
            : this()   // Allows automatic initialization of "cbSize" with "new WINDOWINFO(null/true/false)".
        {
            cbSize = (UInt32)(Marshal.SizeOf(typeof(WINDOWINFO)));
        }

    }

Here is a function that calls the above code and returns true if the window is visible, false if not.

    static bool IsVisible()
    {
        IntPtr hWnd = GetWindow(GetWindow(FindWindow("Progman", "Program Manager"), GetWindow_Cmd.GW_CHILD), GetWindow_Cmd.GW_CHILD);
        WINDOWINFO info = new WINDOWINFO();
        info.cbSize = (uint)Marshal.SizeOf(info);
        GetWindowInfo(hWnd, ref info);
        return (info.dwStyle & 0x10000000) == 0x10000000;
    }

The windows API code along with more information about the window styles can be found here: http://www.pinvoke.net/default.aspx/user32/GetWindowInfo.html

Ondrej Balas
  • 514
  • 4
  • 6
  • 3
    Awesome, im going to put that into all my apps from now on and toggle() it randomly. :) – Gleno Jun 19 '11 at 15:13
  • It doesn't seem to work on my computer... I'm using windows 7. Is this OS dependent? Should it work on all versions of windows? If it is, I will be looking for another solution that works on multiple versions of windows... – Tibi Jun 19 '11 at 18:40
  • Update: It does work, apparently I had to restart explorer.exe, but now it works. Thank you very much. Another question... how can I know if it is on or off? – Tibi Jun 19 '11 at 19:15
  • It was a lot of code to query the icon visibility, so I added it to my answer. – Ondrej Balas Jun 19 '11 at 19:52
  • 7
    This isn't "using the Windows API", it's more like "abusing the Windows API". None of this is officially documented. – David Heffernan Jun 19 '11 at 19:59
  • Thank you very much for all your help. Still, it is causing problems: it only works like 3-4 times to toggle the visibility of desktop icons, and only after restarting explorer.exe. – Tibi Jun 20 '11 at 04:57
5

Even though this is quite old when I tried Ondrej Balas's answer, one problem I found with this solution is that it does not work if the ToggleDesktop command is used to show the desktop ( also if wallpaper rotation is enabled ).

In both of these cases the SHELLDLL_DefView window, which is the recipient of the toggleDesktopCommand in the ToggleDesktopIcons function, is not a child of the "Program manager" window but of a 'WorkerW" window. (see WinApi - How to obtain SHELLDLL_DefView and Windows Desktop ListView Handle.

Based on those and building upon Ondrej Balas's earlier answer change the ToggleDesktopIcons function to be :

static void ToggleDesktopIcons()
{
    var toggleDesktopCommand = new IntPtr(0x7402);
    SendMessage(GetDesktopSHELLDLL_DefView(), WM_COMMAND, toggleDesktopCommand, IntPtr.Zero);
}

And add a GetDesktopSHELLDLL_DefView function:

    [DllImport("user32.dll", SetLastError = true)]
    public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, string windowTitle);
    [DllImport("user32.dll", SetLastError = false)]
    static extern IntPtr GetDesktopWindow();

    static IntPtr GetDesktopSHELLDLL_DefView()
    {
        var hShellViewWin = IntPtr.Zero;
        var hWorkerW = IntPtr.Zero;

        var hProgman = FindWindow("Progman", "Program Manager");
        var hDesktopWnd = GetDesktopWindow();

        // If the main Program Manager window is found
        if (hProgman != IntPtr.Zero)
        {
            // Get and load the main List view window containing the icons.
            hShellViewWin = FindWindowEx(hProgman, IntPtr.Zero, "SHELLDLL_DefView", null);
            if (hShellViewWin == IntPtr.Zero)
            {
                // When this fails (picture rotation is turned ON, toggledesktop shell cmd used ), then look for the WorkerW windows list to get the
                // correct desktop list handle.
                // As there can be multiple WorkerW windows, iterate through all to get the correct one
                do
                {
                    hWorkerW = FindWindowEx(hDesktopWnd, hWorkerW, "WorkerW", null);
                    hShellViewWin = FindWindowEx(hWorkerW, IntPtr.Zero, "SHELLDLL_DefView", null);
                } while (hShellViewWin == IntPtr.Zero && hWorkerW != IntPtr.Zero);
            }
        }
        return hShellViewWin;
    }

Now regardless of the desktop toggle or wallpaper rotation the ToggleDesktopIcons should always work.

For reference this is my toggle desktop function which caused the issue with the original ToggleDesktopIcons function

static public void ToggleDesktop(object sender, EventArgs e)
        {
            var shellObject = new Shell32.Shell();
            shellObject.ToggleDesktop();
        }

In response to James M, this function returns the current state:

bool IconsVisible()
{
    var hWnd = GetDesktopListView();
    var info = new User32.WINDOWINFO(null);
    User32.GetWindowInfo(hWnd, ref info);
    return (info.dwStyle & User32.WindowStyle.WS_VISIBLE) == User32.WindowStyle.WS_VISIBLE;
}
Domoninic
  • 101
  • 1
  • 4
  • 1
    How can you extend this to see if the icons are currently visible? The accepted answer doesn’t work with this solution. –  Jun 30 '20 at 00:03
2

A different approach is to create a separate desktop and show it instead. It will not have icons.

Application running itself on a separate desktop

Community
  • 1
  • 1
Robert
  • 2,357
  • 4
  • 25
  • 46
2

You can do this in RegEdit HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Advanced change HideIcons to 1

    static void HideIcons()
    {
        RegistryKey myKey = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Advanced", true);
        if (myKey != null)
        {
            myKey.SetValue("HideIcons", 1);
            myKey.Close();
        }
    }

Use the Registry class as described here.

http://msdn.microsoft.com/en-us/library/microsoft.win32.registry.aspx

Pepernoot
  • 3,409
  • 3
  • 21
  • 46
1

You can create a full screen view application and make it the top most window.

Then make your application to be start up with windows.

  • 3
    If I make it top most, it will be on top of all other applications... it needs to be exactly the opposite, the bottom most window, except for the taskbar. – Tibi Jun 19 '11 at 14:20
0

You are going about this the wrong way. What you are really trying to do is to replace the shell. Windows provides for this so you should just take advantage of it. Write your own shell to replace explorer.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • 1
    I'm not trying to replace the shell, just the desktop. Instead of having boring icons, I will have some nice widgets. – Tibi Jun 20 '11 at 04:59
0

Nice topic. Without actually creating a different desktop it would be visually pleasant to have the running applications minimized in the same swoop.

khalfan
  • 23
  • 6
  • This does not really answer the question. If you have a different question, you can ask it by clicking [Ask Question](https://stackoverflow.com/questions/ask). To get notified when this question gets new answers, you can [follow this question](https://meta.stackexchange.com/q/345661). Once you have enough [reputation](https://stackoverflow.com/help/whats-reputation), you can also [add a bounty](https://stackoverflow.com/help/privileges/set-bounties) to draw more attention to this question. - [From Review](/review/late-answers/32069746) – Dan Friedman Jun 26 '22 at 14:18