如题替换后用exescope打开看下替换图片部分根本没有图片信息代码如下不知道哪里错误了ICONDIRstID;ICONDIRENTRYstIDE;GRPICONDIR
如题 替换后用exescope打开看下替换图片部分 根本没有图片信息 代码如下 不知道哪里错误了
ICONDIR stID;
ICONDIRENTRY stIDE;
GRPICONDIR stGID;
HANDLE hFile;
DWORD nSize, nGSize, dwReserved;
HANDLE hUpdate;
PBYTE pIcon, pGrpIcon;
BOOL ret;
hFile = CreateFile("D:\\ZN\\ico\\Debug\\zn.ico", GENERIC_READ, NULL, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE)
{
MessageBox("123");
}
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);
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("D:\\ZN\\ico\\Debug\\32323.exe", false);
ret = UpdateResource(hUpdate, RT_GROUP_ICON, MAKEINTRESOURCE(1), NULL, (LPVOID)pGrpIcon, nGSize);
ret = UpdateResource(hUpdate, RT_ICON, MAKEINTRESOURCE(1),NULL, (LPVOID)pIcon, nSize);
EndUpdateResource(hUpdate, false);
CloseHandle(hFile);
14 个解决方案
网上这类的代码挺多 基本都是这个样子 不知道错误在哪里 上面用到的三个结构体我就不列出来了 关于图标的 求大神啊!
自己给自己up 看在帖子在相关内容的百度第一条了 求大神帮忙啊!
代码没看~~ 你是不是要更改工程可执行程序exe的图标? 直接到工程目录下,的res资源文件夹里面把你工程名的ico(例:mytest.ico)换成你想要的图标就好。
如果是要代码执行,是不是程序运行是图标在使用其它什么的。。。。。
自己UPUP 好好学习天天向上 有解决过这个问题的嘛 帮个忙啊 可以加分的!
就是啊,根本不需要这么麻烦,还写代码。
直接把你要的图片导入资源,然后复制像素进你的IDR_MAINFRAME。这个连原来的资源都不用删除。
再编译出来就是你要的图标了。
先查:
hUpdate = BeginUpdateResource("D:\\ZN\\ico\\Debug\\32323.exe", false);
if(hUpdate ==0)
{
DWORD dwErr=GetLastError();
}
“三个结构体”代码上传看看,注意是BYTE对齐的。
给你一个好用的:
http://blog.csdn.net/visualeleven/article/details/5754831
// 第一个参数是将要替换图标的exe文件路径,第二个参数是替换的ico文件路径
void ReplaceExeIco(LPCTSTR lpszApp, LPCTSTR lpszIco)
{
CFile file;
file.Open(lpszIco, CFile::modeRead);
LONG lOffset = 0;
DWORD dwSize = 0;
DWORD dwBytes = 0;
CONST BYTE bGroupIcoSize = 0x18;
BYTE* lpGroupIco = new BYTE[bGroupIcoSize];
memset(lpGroupIco, 0, bGroupIcoSize);
file.Read(lpGroupIco, 0x06);
file.Read(lpGroupIco+0x08, 0x0C);
memcpy(&dwSize, lpGroupIco+0x10, sizeof(DWORD));
file.Read(&lOffset, sizeof(lOffset));
BYTE* lpRes = new BYTE[dwSize];
memset(lpRes, 0, dwSize);
file.Seek(lOffset, CFile::begin);
file.Read(lpRes, dwSize);
HANDLE hApp = BeginUpdateResource(lpszApp, NULL);
UpdateResource(hApp, RT_GROUP_ICON, MAKEINTRESOURCE(1), 0, lpGroupIco, bGroupIcoSize);
UpdateResource(hApp, RT_ICON, MAKEINTRESOURCE(1), 0, lpRes, dwSize);
EndUpdateResource(hApp, FALSE);
delete[] lpRes;
delete[] lpGroupIco;
file.Close();
}
#include
#include
typedef struct tagHEADER
{
WORD idReserved;
WORD idType;
WORD idCount;
}HEADER, *LPHEADER;
typedef struct tagICONDIRENTRY
{
BYTE bWidth;
BYTE bHeight;
BYTE bColorCount;
BYTE bReserved;
WORD wPlanes;
WORD wBitCount;
DWORD dwBytesInRes;
DWORD dwImageOffset;
}ICONDIRENTRY, *LPICONDIRENTRY;
#pragma pack( push )
#pragma pack( 2 )
typedef struct tagGRPICONDIRENTRY
{
BYTE bWidth;
BYTE bHeight;
BYTE bColorCount;
BYTE bReserved;
WORD wPlanes;
WORD wBitCount;
DWORD dwBytesInRes;
WORD nID;
}GRPICONDIRENTRY, *LPGRPICONDIRENTRY;;
typedef struct tagGRPICONDIR
{
WORD idReserved;
WORD idType;
WORD idCount;
GRPICONDIRENTRY idEntries[1];
}GRPICONDIR, *LPGRPICONDIR;
#pragma pack( pop )
//.CPP文件中的主要代码:
//view plaincopy to clipboardprint?
void ChangeExeIcon(LPCTSTR lpIconFile, LPCTSTR lpExeName)
{
LPICONDIRENTRY pIconDirEntry(NULL);
LPGRPICONDIR pGrpIconDir(NULL);
HEADER header;
LPBYTE pIconBytes(NULL);
HANDLE hIconFile(NULL);
DWORD dwRet(0), nSize(0), nGSize(0), dwReserved(0);
HANDLE hUpdate(NULL);
BOOL ret(FALSE);
WORD i(0);
//打开图标文件
hIconFile = CreateFile(lpIconFile, GENERIC_READ, NULL, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hIconFile == INVALID_HANDLE_VALUE)
{
return;
}
//读取文件头部信息
ret=ReadFile(hIconFile, &header, sizeof(HEADER), &dwReserved, NULL);
if (!ret)
{
CloseHandle(hIconFile);
return;
}
//建立每一个图标的目录信息存放区域
pIconDirEntry = (LPICONDIRENTRY)new BYTE[header.idCount*sizeof(ICONDIRENTRY)];
if (pIcOnDirEntry==NULL)
{
CloseHandle(hIconFile);
return;
}
//从Icon文件中读取每一个图标的目录信息
ret = ReadFile(hIconFile, pIconDirEntry, header.idCount*sizeof(ICONDIRENTRY), &dwReserved, NULL);
if (!ret)
{
delete[] pIconDirEntry;
CloseHandle(hIconFile);
return;
}
//建立EXE文件中RT_GROUP_ICON所需的数据结构存放区域
nGSize=sizeof(GRPICONDIR)+header.idCount*sizeof(ICONDIRENTRY);
pGrpIcOnDir=(LPGRPICONDIR)new BYTE[nGSize];
ZeroMemory(pGrpIconDir, nSize);
//填充信息,这里相当于一个转换的过程
pGrpIconDir->idReserved=header.idReserved;
pGrpIconDir->idType=header.idType;
pGrpIconDir->idCount=header.idCount;
//复制信息并设置每一个图标对应的ID。ID为位置索引号
for(i=0;i {
pGrpIconDir->idEntries[i].bWidth=pIconDirEntry[i].bWidth;
pGrpIconDir->idEntries[i].bHeight=pIconDirEntry[i].bHeight;
pGrpIconDir->idEntries[i].bColorCount=pIconDirEntry[i].bColorCount;
pGrpIconDir->idEntries[i].bReserved=pIconDirEntry[i].bReserved;
pGrpIconDir->idEntries[i].wPlanes=pIconDirEntry[i].wPlanes;
pGrpIconDir->idEntries[i].wBitCount=pIconDirEntry[i].wBitCount;
pGrpIconDir->idEntries[i].dwBytesInRes=pIconDirEntry[i].dwBytesInRes;
pGrpIconDir->idEntries[i].nID=i+1; //id == 0 是 RT_GROUP_ICON 的id,我这里替换的时候出现问题,所以就 + 1 了。
}
//开始更新EXE中的图标资源,ID定为最小0,如果原来存在0ID的图标信息则被替换为新的。
hUpdate = BeginUpdateResource(lpExeName, false);
if (hUpdate!=NULL)
{
HMODULE hMe= 0; // means load from this module
if (FindResource( hMe, lpExeName, RT_GROUP_ICON))
{
//首先更新RT_GROUP_ICON信息
ret = UpdateResource(hUpdate, RT_GROUP_ICON, MAKEINTRESOURCE(0), MAKELANGID(LANG_CHINESE, SUBLANG_SYS_DEFAULT), (LPVOID)pGrpIconDir, nGSize);
if (!ret)
{
delete[] pIconDirEntry;
delete[] pGrpIconDir;
CloseHandle(hIconFile);
return;
}
}
//接着的是每一个Icon的信息存放
for(i=0;i {
//Icon的字节数
nSize = pIconDirEntry[i].dwBytesInRes;
//偏移文件的指针到当前图标的开始处
dwRet=SetFilePointer(hIconFile, pIconDirEntry[i].dwImageOffset, NULL, FILE_BEGIN);
if (dwRet==INVALID_SET_FILE_POINTER)
{
break;
}
//准备pIconBytes来存放文件里的Byte信息用于更新到EXE中。
delete[] pIconBytes;
pIconBytes = new BYTE[nSize];
ZeroMemory(pIconBytes, nSize);
ret = ReadFile(hIconFile, (LPVOID)pIconBytes, nSize, &dwReserved, NULL);
if(!ret)
{
break;
}
//更新每一个ID对应的RT_ICON信息
ret = UpdateResource(hUpdate, RT_ICON, MAKEINTRESOURCE(pGrpIconDir->idEntries[i].nID), MAKELANGID(LANG_CHINESE, SUBLANG_SYS_DEFAULT), (LPVOID)pIconBytes, nSize);
if(!ret)
{
break;
}
}
//结束EXE资源的更新操作
if (pIconBytes!=NULL)
{
delete[] pIconBytes;
}
EndUpdateResource(hUpdate, false);
}
//清理资源并关闭Icon文件,到此更新操作结束!
SAFE_ARRAY_DELETE(pGrpIconDir);
SAFE_ARRAY_DELETE(pIconDirEntry);
CloseHandle(hIconFile);
}