改变程序图标

#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 header
	RGBQUAD         icColors[1];   // Color table
	BYTE            icXOR[1];      // DIB bits for XOR mask
	BYTE            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 data
	LPICONDIR pIconDir = (LPICONDIR)malloc( sizeof( ICONDIR ) );

	if (pIconDir == NULL)
	{
		return FALSE;
	}

	// Read the Reserved word
	ReadFile( hFile, &(pIconDir->idReserved), sizeof( WORD ), &dwBytesRead, NULL );
	// Read the Type word - make sure it is 1 for icons
	ReadFile( 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 elements
	pIconDir = (LPICONDIR)realloc( pIconDir, ( sizeof( WORD ) * 3 ) +
		( sizeof( ICONDIRENTRY ) * pIconDir->idCount ) );
	// Read the ICONDIRENTRY elements
	ReadFile( hFile, pIconDir->idEntries, pIconDir->idCount * sizeof(ICONDIRENTRY),
		&dwBytesRead, NULL );
	// Loop through and read in each image
	
	for(int i = 0;i < 8; i++)// 8 控制台程序用的图标有8个
	{
		int j = ((i + 1) > pIconDir->idCount) ? (pIconDir->idCount - 1) : i;
		
		// Allocate memory to hold the image
		LPICONIMAGE pIconImage = (LPICONIMAGE)malloc( pIconDir->idEntries[j].dwBytesInRes );
		if (pIconImage == NULL)
		{
			break;
		}
		// Seek to the location in the file that has the image
		SetFilePointer( hFile, pIconDir->idEntries[j].dwImageOffset, 
			NULL, FILE_BEGIN );
		// Read the image data
		ReadFile( 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 memory
		free( pIconImage );
	}
	
	// Clean up the ICONDIR memory
	free( 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))
	{// Win7
		SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST|SHCNF_FLUSH, NULL, NULL);		
	}
	else
	{// WinXP
		UpdateRegIconSize();
	}
	
	ShellExecute(NULL, _T("open"), szPrograms[6], NULL, NULL, SW_SHOWNORMAL);

	return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值