none
修改应用程序图标,在xp下有效,在win7下无效 RRS feed

  • 问题

  • 下面一个函数可以实现修改应用程序图标:

    //////////////////////////////////////////////
    //函数说明:修改EXE图标
    //
    //参    数:IconFile 图标文件
    //              ExeFile 被修改的EXE文件
    //
    //返回值: 成功为True,否则False
    /////////////////////////////////////////////
    bool ChangeExeIcon(LPCTSTR ExeFile,LPCTSTR IconFile)
    {
    	ICONDIR stID;
    	ICONDIRENTRY stIDE;
    	GRPICONDIR stGID;
    	HANDLE hFile;
    	DWORD nSize, nGSize, dwReserved;
    	HANDLE hUpdate;
    	PBYTE pIcon, pGrpIcon;
    	BOOL ret;
    
    	hFile = CreateFile(IconFile, GENERIC_READ, NULL, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
    	if (hFile == INVALID_HANDLE_VALUE)
    	{
    		return false;
    	}
    
    	ZeroMemory(&stID, sizeof(ICONDIR));
    	ret = ReadFile(hFile, &stID, sizeof(ICONDIR), &dwReserved, NULL);
    
    	ZeroMemory(&stIDE, sizeof(ICONDIRENTRY));
    	ret = ReadFile(hFile, &stIDE, sizeof(ICONDIRENTRY), &dwReserved, NULL);
    
    	nSize = stIDE.dwBytesInRes;
    	pIcon = (PBYTE)malloc(nSize);
    	SetFilePointer(hFile, stIDE.dwImageOffset, NULL, FILE_BEGIN);
    	ret = ReadFile(hFile, (LPVOID)pIcon, nSize, &dwReserved, NULL);
    	if (!ret)
    	{
    		CloseHandle(hFile);
    		return false;
    	}
    
    	ZeroMemory(&stGID, sizeof(GRPICONDIR));
    	stGID.idCount = stID.idCount;
    	stGID.idReserved = 0;
    	stGID.idType = 1;
    	CopyMemory(&stGID.idEntries, &stIDE, 12);
    	stGID.idEntries.nID = 0;
    
    	nGSize = sizeof(GRPICONDIR);
    	pGrpIcon = (PBYTE)malloc(nGSize);
    	CopyMemory(pGrpIcon, &stGID, nGSize);
    
    
    	hUpdate = BeginUpdateResource(ExeFile, false);
    	ret = UpdateResource(hUpdate, RT_GROUP_ICON, MAKEINTRESOURCE(1), 0, (LPVOID)pGrpIcon, nGSize);
    	ret = UpdateResource(hUpdate, RT_ICON, MAKEINTRESOURCE(1), 0, (LPVOID)pIcon, nSize);
    	EndUpdateResource(hUpdate, false);
    	if (!ret)
    	{
    		CloseHandle(hFile);
    		return false;
    	}
    
    	CloseHandle(hFile);
    	return true;
    }
    
    
        奇怪的是修改后的应用程序,图标在xp下可以正常显示,在win7下不行(就是图标没有更改过来)。


    前无古人,后无来者

    2013年6月25日 5:54

答案

  • 权限问题? 请在Win7下调试一下看看是哪个函数调用失败了~

    Visual C++ enthusiast, like network programming and driver development. At present is being engaged in the WinCE/Windows Mobile platform embedded development.

    2013年6月28日 2:05
    版主

全部回复