How to programmatically toggle the visibilty of the desktop icons

The following code will toggle the visibility of the desktop icons, and will update the checkmark in the menu when you right click the desktop.

HWND GetDesktopHWND()
{
  HWND hProgman = FindWindow(_T("Progman"), 0);
  if (hProgman)
    return FindWindowEx(hProgman, 0, _T("SHELLDLL_DefView"), 0);

  return NULL;
}

void ToggleDesktopIcons()
{
  HWND hWndDesktop = GetDesktopHWND();
  if (hWndDesktop)
    SendMessage(hWndDesktop, WM_COMMAND, 0x7402, 0); 
}
Share

2 Comments so far »

  1. Vannes said,

    Wrote on February 12, 2015 @ 2:16 am

    There is a bug.

    If I do “sendmessage” once then click “Show Desktop” on the right side of taskbar, do “sendmessage” again will fail unless I click “Show Desktop” again.

    Any ideas? Thank you 🙂

  2. Marc Gregoire said,

    Wrote on February 12, 2015 @ 8:55 am

    You can use Spy++ to see what messages are being sent when you click “Show Desktop” on the right side of the taskbar. See if that’s the same WM_COMMAND and 0x7402 as in my code or not.

Comment RSS · TrackBack URI

Leave a Comment

Name: (Required)

E-mail: (Required)

Website:

Comment: