We help IT Professionals succeed at work.

Check out this week's podcast, "Dairy Farms to Databases: Community's Hand in Technology"Listen Now


Refreshing and Redrawing the Desktop Window

mueller asked
Last Modified: 2013-11-20
Currently my application needs to draw directly over the entire desktop.  In doing so, I obtain its device context by calling the CWindowDC constructor.  After doing so, I am able to successfully paint directly to the desktop like I need to.  However, when I finish, I need to restore the desktop to its original state (i.e. refresh it - exactly equivalent to what happens if you click on the desktop and press the F5 key).  I have tried many ways of doing this, all without success.  Any insight would be great.  Thanks.
Watch Question

HWND hwndDT = GetDesktopWindow();
InvalidareRect(hwndDT, NULL);
Thats all


Unfortunately, I went down that path prior to posting the question and came up drawing blanks.  Take the following code for example, which gets the desktop window's device context, draws a text string to it, and attempts to clean up the mess afterwards (all in the same manner as above, but as applied to MFC).

// get desktop window
CWnd *pWnd = GetDesktopWindow();

// get desktop window's device context
CWindowDC desktopDC( pWnd );

// write some string out to the dc
desktopDC.TextOut( 50, 50, "Hello" );

// attempt to clean up the desktop
pWnd->InvalidateRect( NULL, TRUE );

// update the window

Again, the code above does not work, and follows your example  in a similar manner (the difference being you call the Win32 API, but no big deal).  Also, when passing through the online Visual C++ 4 documentation the other day looking for info on CWnd::RedrawWindow(), I found an interesting note at the bottom of the "Remarks" section:

When the RedrawWindow member function is used to invalidate part of the desktop window, that window does not receive a WM_PAINT message. To repaint the desktop, an application should use CWnd::ValidateRgn, CWnd::InvalidateRgn, CWnd::UpdateWindow, or ::RedrawWindow.

However, when I followed the advice, I still came up drawing blanks.  If anyone has any insight, I'd be very appreciative. Thanks.

Try this:


Maybe the following code is better. I hope it will update the desktop.

// Get the PIDL for the Desktop.
LPITEMIDLIST pidlDesktop;  // PIDL for the Desktop
if (SUCCEEDED(::SHGetSpecialFolderLocation(pWnd->GetSafeHwnd(), CSIDL_DESKTOP, &pidlDesktop)))
    // Refresh the Desktop
    ::SHChangeNotify(SHCNE_UPDATEDIR, SHCNF_IDLIST, (DWORD)pidlDesktop, 0);

    // pointer to the shell's IMalloc interface.
    LPMALLOC pMalloc;

    // Get the shell's allocator.
    if (SUCCEEDED(::SHGetMalloc(&pMalloc)))
        // Free the PIDL returned by SHGetSpecialFolderLocation

        // Release the shell's allocator.


Unfortunately the code used to update and refresh the desktop by using the Shell API was not successful.  If anyone has any ideas, it would be great.  Thanks again.


Adjusted points to 150
This one is on us!
(Get your first solution completely free - no credit card required)



          Thanks for all of the help.  I did use Spy++ earlier to grab the desktop window's class name, but mine came up with
"SysListView32".  I tried the FindWindow method with that class name but always came up blank.  Again, thanks for the help.


The "SysListView32" window that you got is the child of the "Program Manager" window that is the top level window. The class name "SysListView32" belongs to the List View control.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.