Click here to Skip to main content
15,894,720 members
Articles / Programming Languages / C++
Article

Code to refresh desktop programmatically

Rate me:
Please Sign up or sign in to vote.
1.18/5 (11 votes)
17 Feb 2005 55.6K   541   14   5
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


Written By
Web Developer
Indonesia Indonesia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 2 Pin
Ra$cal11-May-09 7:46
Ra$cal11-May-09 7:46 
Rantmakes the desktop icons dissappear and other things on windows 2000 Pin
Rami Abughazaleh18-Apr-08 18:52
Rami Abughazaleh18-Apr-08 18:52 
GeneralREFRESH Pin
Twardy1-Dec-05 9:50
Twardy1-Dec-05 9:50 
GeneralRe: REFRESH Pin
paulgafa16-Feb-07 3:03
paulgafa16-Feb-07 3:03 
Generalre-arranges the desktop Pin
adrian_info8-Mar-05 3:42
adrian_info8-Mar-05 3:42 

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

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