Click here to Skip to main content
6,596,602 members and growing! (22,352 online)
Email Password   helpLost your password?
General Reading » Hardware & System » General     Intermediate

Code to refresh desktop programmatically

By habasoft

C++ code to refresh desktop programmatically using only Win32 API.
C++Win2K, WinXP, Visual Studio, Dev
Posted:17 Feb 2005
Views:27,852
Bookmarked:13 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
9 votes for this article.
Popularity: 1.17 Rating: 1.23 out of 5
7 votes, 77.8%
1
2 votes, 22.2%
2

3

4

5

Introduction

Refresh desktop programmatically

Following code that uses only Win32 API and written in C++ will mimic F5 key action on desktop. This code already tested with BCC55, worked as intended. Please do some error checking yourself in 'F5Desk()' function.

#include <windows.h>


typedef char str64[64];
typedef char str256[256];
typedef char str1024[1024];

#define HKCU   HKEY_CURRENT_USER

BOOL SetString(HKEY hkeymom, str256 pkeyname, 
      str64 pvalname, str1024 pvaldata)
{  HKEY hkey = 0;
   if(RegCreateKeyEx(hkeymom, pkeyname, 0, 
      NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, 
      &hkey, NULL) != ERROR_SUCCESS)
      return FALSE;
   LONG rtn = RegSetValueEx(hkey, pvalname, 0, 
      REG_SZ, (BYTE*)pvaldata, lstrlen(pvaldata));
   RegCloseKey(hkey);
   if(rtn == ERROR_SUCCESS)    
      return TRUE;
   return FALSE;
}

BOOL ReadString(HKEY hkeymom, str256 pkeyname, 
      str64 pvalname, str1024 pvaldata)
{  HKEY hkey = 0;
   DWORD dw;
   if(RegOpenKeyEx(hkeymom, pkeyname, 0, KEY_READ, &hkey) != ERROR_SUCCESS)
   {  strcpy(pvaldata, "");
      return FALSE;
   }
   LONG rtn = RegQueryValueEx(hkey, pvalname, NULL, &dw, NULL, NULL);
   if((rtn != ERROR_SUCCESS) || (dw != REG_SZ))
   {  RegCloseKey(hkey);        
      strcpy(pvaldata, "");
      return FALSE;    
   }
   dw = 1024;
   RegQueryValueEx(hkey, pvalname, NULL, NULL, (BYTE*)pvaldata, &dw);
   RegCloseKey(hkey);
   return TRUE;
}

int myexp (int y)
{  int ii = 1;
   for (int cnt = 0; cnt < y; cnt++)
      ii *= 10;
   return ii;     
}

BOOL StringToInt(str1024 sz, int* ires)
{  //temp string

   str1024 szi = "";
   //copy character that represent number or negative sign only to temp string

   for(unsigned int cnt = 0; cnt < strlen(sz); cnt++)
   {  //if not 0 to 9 or negative sign then break

      if((sz[cnt] != 45) && (sz[cnt] != 48) && 
         (sz[cnt] != 49) & (sz[cnt] != 50) && 
         (sz[cnt] != 51) && (sz[cnt] != 52) && 
         (sz[cnt] != 53) && (sz[cnt] != 54) && 
         (sz[cnt] != 55) && (sz[cnt] != 56) && (sz[cnt] != 57))
         break;          
      //if negative sign but not in first letter then break

      if((sz[cnt] == 45) && (cnt != 0))
         break;
      //add to temp string

      switch(sz[cnt])
      {  case 45:    strcat(szi, "-");    break;
         case 48:    strcat(szi, "0");    break;
         case 49:    strcat(szi, "1");    break;
         case 50:    strcat(szi, "2");    break;
         case 51:    strcat(szi, "3");    break;
         case 52:    strcat(szi, "4");    break;
         case 53:    strcat(szi, "5");    break;
         case 54:    strcat(szi, "6");    break;
         case 55:    strcat(szi, "7");    break;
         case 56:    strcat(szi, "8");    break;
         case 57:    strcat(szi, "9");    break;
      }
   }
   //if length of temp string = 0 then return FALSE     

   if(!strlen(szi))    
   {  *ires = 0;    
      return FALSE;
   }
   //if length of temp string = 1 but only negative sign in it, return FALSE

   else if((strlen(szi) == 1) && !strcmp(szi, "-"))    
   {  *ires = 0;    
      return FALSE;
   }
   //flag for negative or positive number

   BOOL flag = FALSE;
   //if there is negative sign in temp string then flag = TRUE

   if(strchr(szi, '-'))    flag = TRUE;    
   int ii = 0;
   strrev(szi);
   //convert temp string to number 

   for(unsigned int cnt = 0; cnt < strlen(szi); cnt++)
   {   switch(szi[cnt])
       {   case 48:    ii += 0 * myexp(cnt);        break;    
           case 49:    ii += 1 * myexp(cnt);        break;    
           case 50:    ii += 2 * myexp(cnt);        break;    
           case 51:    ii += 3 * myexp(cnt);        break;    
           case 52:    ii += 4 * myexp(cnt);        break;    
           case 53:    ii += 5 * myexp(cnt);        break;    
           case 54:    ii += 6 * myexp(cnt);        break;    
           case 55:    ii += 7 * myexp(cnt);        break;    
           case 56:    ii += 8 * myexp(cnt);        break;    
           case 57:    ii += 9 * myexp(cnt);        break;    
       }
   }
   //if flag and number > 0 then negative the number

   if((ii > 0) && flag)    ii /= -1; 
   *ires = ii;
   return TRUE;
}

void F5Desk()
{  str1024 szres = "";
   //read current icon size, usually 32 (in Windows� XP)

   ReadString(HKCU, "Control Panel\\Desktop\\WindowMetrics", 
         "Shell Icon Size", szres);
   int ii = 0;
   //translate to number 

   StringToInt(szres, &ii);
   //increase it by 1

   ii++;
   //convert back to string

   wsprintf(szres, "%i", ii);
   //now, change the icon size 

   SetString(HKCU, "Control Panel\\Desktop\\WindowMetrics", 
      "Shell Icon Size", szres);
   //find taskbar and make it can't redraw

   HWND hwndtask = FindWindow("Shell_traywnd", "");
   SendMessage(hwndtask, WM_SETREDRAW, FALSE, 0);
   //broadcast the icon size change, so the desktop will be refreshed

   SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 
      SPI_SETNONCLIENTMETRICS, 0, 
      SMTO_ABORTIFHUNG, 100000, NULL);
   //make the icon size go back to original number

   ii--;
   wsprintf(szres, "%i", ii);
   SetString(HKCU, "Control Panel\\Desktop\\WindowMetrics", 
         "Shell Icon Size", szres);
   SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 
      SPI_SETNONCLIENTMETRICS, 0, 
      SMTO_ABORTIFHUNG, 100000, NULL);
   //make taskbar can redraw again

   SendMessage(hwndtask, WM_SETREDRAW, TRUE, 0);
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, 
   LPSTR lpCmdLine, int nCmdShow)
{  F5Desk();
   return 0;    
}

Copyright notice

  • This code is based on work by Rocky Mountain Computer Consulting, Inc.
  • This code is provided free. Use it as you wish, but all responsibilities are yours.
  • You should include this Copyright notice in your Copyright notice, or, Readme, or similar thing if you use this code or get know-how from this code and you use it in your program.
  • This code is supplied from an anonymous man in Semarang, Indonesia.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

habasoft


Member

Occupation: Web Developer
Location: Indonesia Indonesia

Other popular Hardware & System articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 5 of 5 (Total in Forum: 5) (Refresh)FirstPrevNext
GeneralMy vote of 2 PinmemberRa$cal8:46 11 May '09  
Rantmakes the desktop icons dissappear and other things on windows 2000 PinmemberRami Abughazaleh19:52 18 Apr '08  
GeneralREFRESH PinmemberTwardy10:50 1 Dec '05  
GeneralRe: REFRESH Pinmemberpaulgafa4:03 16 Feb '07  
Generalre-arranges the desktop Pinmemberadrian_info4:42 8 Mar '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 17 Feb 2005
Editor: Sumalatha K.R.
Copyright 2005 by habasoft
Everything else Copyright © CodeProject, 1999-2009
Web17 | Advertise on the Code Project