改变程序图标

来源:互联网 发布:法甲数据 编辑:程序博客网 时间:2024/04/28 14:21
#include "stdafx.h"#include <windows.h>#include <malloc.h>#include <tchar.h>#include <shellapi.h>#include <Shlobj.h>#define CONFIGFILE _T(".\\Custom\\Config.ini")typedef struct{    BYTE        bWidth;          // Width, in pixels, of the image    BYTE        bHeight;         // Height, in pixels, of the image    BYTE        bColorCount;     // Number of colors in image (0 if >=8bpp)    BYTE        bReserved;       // Reserved ( must be 0)    WORD        wPlanes;         // Color Planes    WORD        wBitCount;       // Bits per pixel    DWORD       dwBytesInRes;    // How many bytes in this resource?    DWORD       dwImageOffset;   // Where in the file is this image?} ICONDIRENTRY, *LPICONDIRENTRY;typedef struct{ WORD           idReserved;   // Reserved (must be 0)    WORD           idType;       // Resource Type (1 for icons)    WORD           idCount;      // How many images?    ICONDIRENTRY   idEntries[1]; // An entry for each image (idCount of 'em)} ICONDIR, *LPICONDIR;typedef struct{BITMAPINFOHEADER   icHeader;   // DIB headerRGBQUAD         icColors[1];   // Color tableBYTE            icXOR[1];      // DIB bits for XOR maskBYTE            icAND[1];      // DIB bits for AND mask} ICONIMAGE, *LPICONIMAGE;/**  * 函数:ChangeExeIcon * 描述:更改程序图标 * 参数:lpszIcon .ico文件路径 * lpszExeFile 被更改的程序路径 * 返回:TRUE 成功 * FALSE 失败 */BOOL ChangeExeIcon(LPCTSTR lpszIcon, LPCTSTR lspzExeFile){HANDLE hFile;DWORD dwBytesRead;hFile = CreateFile(lpszIcon, GENERIC_READ, NULL, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);if (hFile == INVALID_HANDLE_VALUE){return FALSE;}// We need an ICONDIR to hold the dataLPICONDIR pIconDir = (LPICONDIR)malloc( sizeof( ICONDIR ) );if (pIconDir == NULL){return FALSE;}// Read the Reserved wordReadFile( hFile, &(pIconDir->idReserved), sizeof( WORD ), &dwBytesRead, NULL );// Read the Type word - make sure it is 1 for iconsReadFile( hFile, &(pIconDir->idType), sizeof( WORD ), &dwBytesRead, NULL );// Read the count - how many images in this file?ReadFile( hFile, &(pIconDir->idCount), sizeof( WORD ), &dwBytesRead, NULL );// Reallocate IconDir so that idEntries has enough room for idCount elementspIconDir = (LPICONDIR)realloc( pIconDir, ( sizeof( WORD ) * 3 ) +( sizeof( ICONDIRENTRY ) * pIconDir->idCount ) );// Read the ICONDIRENTRY elementsReadFile( hFile, pIconDir->idEntries, pIconDir->idCount * sizeof(ICONDIRENTRY),&dwBytesRead, NULL );// Loop through and read in each imagefor(int i = 0;i < 8; i++)// 8 控制台程序用的图标有8个{int j = ((i + 1) > pIconDir->idCount) ? (pIconDir->idCount - 1) : i;// Allocate memory to hold the imageLPICONIMAGE pIconImage = (LPICONIMAGE)malloc( pIconDir->idEntries[j].dwBytesInRes );if (pIconImage == NULL){break;}// Seek to the location in the file that has the imageSetFilePointer( hFile, pIconDir->idEntries[j].dwImageOffset, NULL, FILE_BEGIN );// Read the image dataReadFile( hFile, pIconImage, pIconDir->idEntries[j].dwBytesInRes,&dwBytesRead, NULL );// Here, pIconImage is an ICONIMAGE structure. Party on it :)HANDLE hUpdate = ::BeginUpdateResource(lspzExeFile, FALSE);BOOL bRet = ::UpdateResource(hUpdate, RT_ICON, MAKEINTRESOURCE(i+1),MAKELANGID(LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED),pIconImage, dwBytesRead);::EndUpdateResource(hUpdate, FALSE);// Then, free the associated memoryfree( pIconImage );}// Clean up the ICONDIR memoryfree( pIconDir );CloseHandle(hFile);return TRUE;}/**  * 函数:UpdateRegIconSize * 描述:改变桌面图标的大小,再复原大小,目的是为变换图标后立即生效 */void UpdateRegIconSize(){HKEY   hKey;  struct   HKEY__*RootKey;  TCHAR   *SubKey;  DWORD   dwType=REG_SZ;  DWORD   dwLength=256;  TCHAR   *ValueName;  LPBYTE   SetContent_S;  RootKey=HKEY_CURRENT_USER;                                                                                  SubKey=_T("Control Panel\\Desktop\\WindowMetrics");    ValueName=_T("Shell Icon Size");  TCHAR content[256];  RegOpenKeyEx(RootKey,SubKey,0,KEY_READ,&hKey);  RegQueryValueEx(hKey,ValueName,NULL,&dwType,(unsigned char*)content,&dwLength);  RegCloseKey(hKey);  SetContent_S=LPBYTE(_T("24"));  RegOpenKeyEx(RootKey,SubKey,0,KEY_WRITE,&hKey);  RegSetValueEx(hKey,ValueName,NULL,REG_SZ,SetContent_S,_tcslen(LPCTSTR(SetContent_S))*sizeof(TCHAR));  RegCloseKey(hKey);      SendMessageTimeout(HWND_BROADCAST,WM_SETTINGCHANGE, SPI_SETNONCLIENTMETRICS,0,SMTO_ABORTIFHUNG,5000,NULL);  SetContent_S=LPBYTE(content);  RegOpenKeyEx(RootKey,SubKey,0,KEY_WRITE,&hKey);  RegSetValueEx(hKey,ValueName,NULL,REG_SZ,SetContent_S,_tcslen(LPCTSTR(SetContent_S))*sizeof(TCHAR));  RegCloseKey(hKey);      SendMessageTimeout(HWND_BROADCAST,WM_SETTINGCHANGE, SPI_SETNONCLIENTMETRICS,0,SMTO_ABORTIFHUNG,5000,NULL); }/**  * 函数:GetWinVersion * 描述:获取系统版本号 */LONG GetWinVersion(){OSVERSIONINFO osvi;::ZeroMemory(&osvi, sizeof(OSVERSIONINFO));osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);::GetVersionEx(&osvi);return MAKELONG(osvi.dwMajorVersion,osvi.dwMinorVersion);}int APIENTRY _tWinMain(HINSTANCE hInstance,                     HINSTANCE hPrevInstance,                     LPTSTR     lpCmdLine,                     int       nCmdShow){ // TODO: Place code here.TCHAR szPath[MAX_PATH];memset(szPath, 0x00, sizeof(szPath));GetPrivateProfileString(_T("Path"), _T("LogoPath"), _T(""), szPath, MAX_PATH, CONFIGFILE);TCHAR szCurDir[MAX_PATH];memset(szCurDir, 0x00, MAX_PATH);GetCurrentDirectory(MAX_PATH, szCurDir);TCHAR szPrograms[10][MAX_PATH];int i = 0;for (i = 0; i < 10; i++){_tcsncpy(szPrograms[i], szCurDir, MAX_PATH);}_tcscat(szPrograms[0], _T("\\outline.exe"));_tcscat(szPrograms[1], _T("\\MainFrame.exe"));_tcscat(szPrograms[2], _T("\\PatchHotfixDown.exe"));_tcscat(szPrograms[3], _T("\\RemoteFileModule.exe"));_tcscat(szPrograms[4], _T("\\ClientSetup.exe"));_tcscat(szPrograms[5], _T("\\CtrlOnline.exe"));_tcscat(szPrograms[6], _T("\\HrCtrl.exe"));_tcscat(szPrograms[7], _T("\\SeeFlux.exe"));Sleep(1000);for (i = 0; i < 8; i++){if (!ChangeExeIcon(szPath, szPrograms[i]))break;}if (i == 8){WritePrivateProfileString(_T("Flag"), _T("UsedNew"), _T("1"), CONFIGFILE);}if (GetWinVersion() >= MAKELONG(6,1)){// Win7SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST|SHCNF_FLUSH, NULL, NULL);}else{// WinXPUpdateRegIconSize();}ShellExecute(NULL, _T("open"), szPrograms[6], NULL, NULL, SW_SHOWNORMAL);return 0;}

原创粉丝点击