我有一个在MFC中创建的应用程序,需要为exe设置几个图标中的其中一个。我不知道要使用哪个图标,直到该应用程序安装在用户的计算机上。我已经按照几个网站(包括https://learn.microsoft.com/en-us/windows/win32/menurc/using-resources)上的说明进行了编程。
在应用程序的.rc文件中,exe使用的图标是IDR_MAINFRAME(ID 128),只是一个标准的.ico文件。运行更新代码后,当我在Visual Studio中打开exe时,我看到IDR_MAINFRAME的图标已更新为正确的图标,但在资源管理器中使用的exe图像仍显示旧(原始)图标。我没有看到更新后的exe中任何关于原始图标的引用。
我错过了什么?
在应用程序的.rc文件中,exe使用的图标是IDR_MAINFRAME(ID 128),只是一个标准的.ico文件。运行更新代码后,当我在Visual Studio中打开exe时,我看到IDR_MAINFRAME的图标已更新为正确的图标,但在资源管理器中使用的exe图像仍显示旧(原始)图标。我没有看到更新后的exe中任何关于原始图标的引用。
我错过了什么?
HGLOBAL hResLoad; // handle to loaded resource
HMODULE hExe; // handle to existing .EXE file
HRSRC hRes; // handle/ptr. to res. info. in hExe
HANDLE hUpdateRes; // update resource handle
LPVOID lpResLock; // pointer to resource data
BOOL result;
hExe = LoadLibrary(TEXT("app.exe"));
if (hExe == NULL)
{
return;
}
// Locate the icon resource in the .EXE file.
hRes = FindResource(hExe, MAKEINTRESOURCE(IDI_TB_SAVE), RT_GROUP_ICON);
if (hRes == NULL)
{
return;
}
// Load the resource into global memory.
hResLoad = LoadResource(hExe, hRes);
if (hResLoad == NULL)
{
return;
}
// Lock the resourcex into global memory.
lpResLock = LockResource(hResLoad);
if (lpResLock == NULL)
{
return;
}
// Open the file to which you want to add the icon resource.
hUpdateRes = BeginUpdateResource(TEXT("app_updated.exe"), FALSE);
if (hUpdateRes == NULL)
{
return;
}
result = UpdateResource(hUpdateRes, // update resource handle
RT_GROUP_ICON, // change icon resource
MAKEINTRESOURCE(IDR_MAINFRAME), // resource id
MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), // english
lpResLock, // ptr to resource info
SizeofResource(hExe, hRes)); // size of resource info
if (result == FALSE)
{
return;
}
// Write changes to app_updated.exe and then close it.
if (!EndUpdateResource(hUpdateRes, FALSE))
{
return;
}
// Clean up.
if (!FreeLibrary(hExe))
{
return;
}