替换应用程序exe图标,主要使用的API函数是BeginUpdateResource(),UpdateResource(),EndUpdateResource()来使用自定义的ico文件类替换exe程序原来的图标。
[cpp] view plain copy
- // 第一个参数是将要替换图标的exe文件路径,第二个参数是替换的ico文件路径
- void ReplaceExeIco(LPCTSTR lpszApp, LPCTSTR lpszIco)
- {
- try
- {
- CFile file(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();
- }
- catch (CException* e)
- {
- e->ReportError();
- e->Delete();
- }
- }
- //调用方法很简单,例如下面这样就可以了
- ReplaceExeIco(_T("F://11.exe"), _T("F://11.ico"));
http://blog.csdn.net/visualeleven/article/details/5754831
时间: 2024-10-10 04:05:28