VC++替换exe可执行文件的图标

3162阅读 3评论2007-03-10 huanat
分类:C/C++

替换exe可执行文件的图标:
1、首先取得源资源的指针;
2、利用UpdateResource函数进行替换;
 
void CDlgTest2Dlg::OnBTNUpdateResource()
{
 // TODO: Add your control notification handler code here
 HMODULE hExe;
 HANDLE hUpdateRes;
 HRSRC hRes;
 HANDLE hResLoad;
 char *lpResLock;
 BOOL result;
 hExe=LoadLibrary("a.exe");
 if (!hExe)
 {
  MessageBox("载入可执行文件失败!");
 }
 hRes=FindResource(hExe,MAKEINTRESOURCE(IDI_ICON1),RT_GROUP_ICON);
 if (!hRes)
 {
  MessageBox("FindResource失败!");
 }
 hResLoad=LoadResource(hExe,hRes);
 if (!hResLoad)
 {
  MessageBox("LoadResource失败!");
 }
 lpResLock=(char*)LockResource(hResLoad);
 if (!lpResLock)
 {
  MessageBox("LockResource失败!");
 }
 hUpdateRes=BeginUpdateResource("switch.exe",FALSE);
 if (!hUpdateRes)
 {
  MessageBox("BeginUpdateResource失败!");
 }
 result=UpdateResource(hUpdateRes,RT_GROUP_ICON,MAKEINTRESOURCE(IDR_MAINFRAME),
  MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL),lpResLock,SizeofResource(hExe,hRes));
 if (!result)
 {
  MessageBox("UpdateResource失败!");
 }
 if (!EndUpdateResource(hUpdateRes, FALSE))
 {
  MessageBox("Could not write changes to file.");
 }
 
 // Clean up.
 if (!FreeLibrary(hExe))
 {
  MessageBox("Could not free executable.");
 }
}
 
上一篇:ESP定律详解
下一篇:利用Visual C++设置桌面墙纸