elva

“刷新” 功能代码

// Refresh.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#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 cnt1 = 0; cnt1 < strlen(szi); cnt1++)
   {   switch(szi[cnt1])
       {   case 48: ii += 0 * myexp(cnt1);  break; 
    case 49: ii += 1 * myexp(cnt1);  break; 
    case 50: ii += 2 * myexp(cnt1);  break; 
    case 51: ii += 3 * myexp(cnt1);  break; 
    case 52: ii += 4 * myexp(cnt1);  break; 
    case 53: ii += 5 * myexp(cnt1);  break; 
    case 54: ii += 6 * myexp(cnt1);  break; 
    case 55: ii += 7 * myexp(cnt1);  break; 
    case 56: ii += 8 * myexp(cnt1);  break; 
    case 57: ii += 9 * myexp(cnt1);  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; 
}

 

 

posted on 2007-06-19 16:27 叶子 阅读(409) 评论(0)  编辑 收藏 引用 所属分类: C\C++


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理