Take the 2-minute tour ×

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

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

share|improve this question
add comment

3 Answers

up vote 11 down vote accepted

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,
    [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)]
    private static extern bool GetWindowInfo(IntPtr hwnd, ref WINDOWINFO pwi);

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

    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

share|improve this answer
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
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
ToggleDesktopIcons doesn't work in Windows 8 –  Chuck Savage Dec 4 '12 at 3:46
show 3 more comments

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.

share|improve this answer
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
add comment

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.

share|improve this answer
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 4:59
add comment

Your Answer


