Click here to Skip to main content
Licence 
First Posted 17 Feb 2005
Views 37,234
Bookmarked 13 times

Code to refresh desktop programmatically

By | 17 Feb 2005 | Article
C++ code to refresh desktop programmatically using only Win32 API.

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

Web Developer

Indonesia Indonesia

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 2 PinmemberRa$cal7:46 11 May '09  
terrible code..... author, read "refactoring" fowler... and "code complete" Mcconnell.
Rantmakes the desktop icons dissappear and other things on windows 2000 PinmemberRami Abughazaleh18:52 18 Apr '08  
GeneralREFRESH PinmemberTwardy9:50 1 Dec '05  
GeneralRe: REFRESH Pinmemberpaulgafa3:03 16 Feb '07  
Generalre-arranges the desktop Pinmemberadrian_info3:42 8 Mar '05  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120604.1 | Last Updated 17 Feb 2005
Article Copyright 2005 by habasoft
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid