Windows Forms: Hide and Show desktop icons in C#

This post shows you How to hide and show desktop icons in C# Windows Forms Application.

How to hide desktop icons programmatically?

You can use the Win32 API function to hide and show desktop icons in c#.

First of all, you should create a simple GUI that allows you to hide and display all the icons on the screen as shown below.

hide desktop icons in c#

After you finish designing your form, you need open your form code behind, then add ShowWindow and FindWindowEx methods as shown below.

[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

The DLLImport ("user32.dll") must have the name of C++ and Parentheses indicate that we are using an attribute.

Adding a click event handler to the Show button allows you to show desktop icons.

private void btnShow_Click(object sender, EventArgs e)
{
    IntPtr hWnd = FindWindowEx(IntPtr.Zero, IntPtr.Zero, "Progman", null);
    ShowWindow(hWnd, 5);
}

Finally, Add the click event hanlder to the Hide button allows you to hide desktop icons.

private void btnHide_Click(object sender, EventArgs e)
{
    IntPtr hWnd = FindWindowEx(IntPtr.Zero, IntPtr.Zero, "Progman", null);
    ShowWindow(hWnd, 0);
}

and don't forget to include the namespaces below to your form.

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

Through this c# example, i hope so you can easily hide or show desktop icons programmatically in c# windows forms application.