Solved

Refreshing and Redrawing the Desktop Window

Posted on 1997-07-17
9
474 Views
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.
0
Comment
Question by:mueller
  • 4
  • 4
9 Comments
 
LVL 3

Expert Comment

by:jaba
Comment Utility
HWND hwndDT = GetDesktopWindow();
InvalidareRect(hwndDT, NULL);
UpdateWindow(hwndDT);
Thats all
0
 

Author Comment

by:mueller
Comment Utility
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
pWnd->UpdateWindow();
-------------------------------------------------------------------------

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.
0
 
LVL 23

Expert Comment

by:chensu
Comment Utility
Try this:

::SHChangeNotify(SHCNE_UPDATEDIR, SHCNF_FLUSH, 0, 0);
0
 
LVL 23

Expert Comment

by:chensu
Comment Utility
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
      pMalloc->Free(pidlDesktop);

        // Release the shell's allocator.
        pMalloc->Release();
    }
}

0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:mueller
Comment Utility
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.
0
 

Author Comment

by:mueller
Comment Utility
Adjusted points to 150
0
 
LVL 23

Accepted Solution

by:
chensu earned 150 total points
Comment Utility
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)
{
    pWnd->ShowWindow(SW_HIDE);
    pWnd->ShowWindow(SW_SHOWNA);
}

0
 

Author Comment

by:mueller
Comment Utility
Chensu,

          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.

                                                 Steve
0
 
LVL 23

Expert Comment

by:chensu
Comment Utility
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.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Add content to output file 4 57
not able to insert into temp table 68 150
sumDigits challenge 9 96
firstChar challenge 13 83
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now