Refreshing and Redrawing the Desktop Window

Posted on 1997-07-17
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.
Question by:mueller
  • 4
  • 4

Expert Comment

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

Author Comment

ID: 1302624
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.
LVL 23

Expert Comment

ID: 1302625
Try this:

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

LVL 23

Expert Comment

ID: 1302626
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.


Author Comment

ID: 1302627
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.

Author Comment

ID: 1302628
Adjusted points to 150
LVL 23

Accepted Solution

chensu earned 150 total points
ID: 1302629
The following code does it. (You can use Spy++ to find that the top desktop window is Program Manager window.)

CWnd *pWnd = CWnd::FindWindow("Progman", "Program Manager");
if (pWnd != NULL)


Author Comment

ID: 1302630

          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.

LVL 23

Expert Comment

ID: 1302631
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.

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ffmpeg - "rtsp://...... Operation not permitted" 4 86
has77  challenge 9 88
Thin secure Windows 10 5 102
"Black Box" Testing of Control System Software 2 70
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question