这两天想做一下windows系统下图标的修改,让程序有更新的时候能够更新图标的外观,达到提醒的作用,360,QQ经常采用这种方式进行更新的提示,也有采用弹框的方式来提示,用新版QVOD的同事可能见到过类似的。废话不说,我的思路是:
(1)检测程序更新
(2)检测windows桌面图标中是够含有需要的,如果没有则需要创建,如果有则准备更新图标
(3)根据跟新的内容,程序自己创建新的图像,修改快捷方式的图标
这种方法需要开机之后,存在一个系统进程来检测这种更新,360就是这样干的~~还有一个弊端是程序需要检测桌面快捷方式的信息来判断是不是自己想去修改的,OK ,这个问题其实可以通过LNK文件的属性,应用信息,名字等问题来解决。 下面是实现过程
1 ICON文件的操作
ICON文件的格式和BMP文件的格式的类似,不过增加了层数的东西,一个ICON文件中可能有几份数据,不同的分辨率,ICON文件的格式主要定义如下:
//ICON文件存储格式
typedef struct
{byte bwidth; // width, in pixels, of the imagebyte bheight; // height, in pixels, of the imagebyte bcolorcount; // number of colors in image (0 if >=8bpp)byte breserved; // reserved ( must be 0)WORD wplanes; // color planesWORD wbitcount; // bits per pixelDWORD dwbytesinres; // how many bytes in this resource?DWORD dwimageoffset; // where in the file is this image?
} ICONDIRENTRY, *LPICONDIRENTRY;//ICON文件目录
typedef struct
{WORD idReserved; // ReservedWORD idType; // resource type (1 for icons)WORD idCount; // how many images?ICONDIRENTRY idEntries[1]; // the entries for each image
} ICONDIR, *LPICONDIR;//ICON文件数据存储格式
typedef struct
{BITMAPINFOHEADER icheader; // dib headerRGBQUAD iccolors[1]; // color tablebyte icxor[1]; // dib bits for xor maskbyte icand[1]; // dib bits for and mask
} iconimage, *lpiconimage;
,
//图像数据存储
struct stImageData
{BITMAPINFOHEADER icheader; // ptr to headerbyte* icxor ;byte* icand ;stImageData(){icand = NULL ;icxor = NULL ;}
};//ICON图像整体组成
struct stIconData
{int num ;stImageData data[1];stIconData(){num = 0 ; }
};struct stIcon
{ICONDIR *dir ;stIconData *icon ;stIcon(){dir = NULL ;icon = NULL ;}
};
bool CIconOperate::ReadIcon(const string _str)
{fstream fin ;fin.open(_str.c_str(),ios_base::binary | ios_base::in);if (!fin.is_open()){return false ;}ICONDIR dirTemp ;//reservedfin.read((char*)(&dirTemp.idReserved),sizeof(WORD));//typefin.read((char*)(&dirTemp.idType),sizeof(WORD));//numfin.read((char*)(&dirTemp.idCount),sizeof(WORD));//image data headerbyte *pBuffer = new byte[3 * sizeof(WORD) + dirTemp.idCount * sizeof(ICONDIRENTRY)] ; // 变长数据操作m_IconDir = (ICONDIR *)(pBuffer);m_IconDir->idCount = dirTemp.idCount ;m_IconDir->idReserved = dirTemp.idReserved ;m_IconDir->idType = dirTemp.idType ;for (int i = 0 ; i
}
写数据
bool CIconOperate::SaveIcon(const string _str)
{if (m_IconDir == NULL || m_IconData == NULL)return false ;//int width = 64 ;//int heigth = 64 ;//int ppx = 32 ;fstream fout;fout.open(_str.c_str(),ios_base::out | ios_base::binary); //reserved WORD wData = 0 ;fout.write((char*)(&wData),sizeof(WORD));//typewData = 1 ;fout.write((char*)(&wData),sizeof(WORD));//numwData = m_IconDir->idCount ;fout.write((char*)(&wData),sizeof(WORD));//write ICONDIRENTRY数据for (int i = 0 ; i
}
vector<string>* CFileSearch::SearchLinkByName(const CString& _str)
{m_FindList.clear();m_strFind &#61; _str ;GetPath(m_strDeskTopPath,m_strQuickLanchPath); if(GetDesktopIShellFolder()) { GetIEunmIDList(m_pIShellFolder,FALSE,m_mode); } fprintf(fp,"\n\n\n\n");for (int i &#61; 0 ; i
}//获取桌面文件夹和快速启动文件夹的路径
int CFileSearch::GetPath(char *DeskTop, char* AppData)
{ CRegKey m_reg; if(ERROR_SUCCESS&#61;&#61;m_reg.Open(HKEY_CURRENT_USER,"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders",KEY_READ)) { ULONG m_ulsize1&#61;1000; ULONG m_ulsize2&#61;1000; m_reg.QueryStringValue("Desktop",DeskTop,&m_ulsize1); m_reg.QueryStringValue("AppData",AppData,&m_ulsize2); lstrcat(AppData,"\\Microsoft\\Internet Explorer\\Quick Launch"); } return 0;
} //获取桌面文件夹的IShellFolder接口指针
BOOL CFileSearch::GetDesktopIShellFolder()
{ m_pIShellFolder&#61;GetIShellFolderByPath(m_strDeskTopPath); //m_pAppData&#61;GetIShellFolderByPath(m_strQuickLanchPath); return TRUE;
} //获取桌面文件夹的IEnumIDList接口
//isQuickLanch---是否搜索第二层文件夹
//isRecur---是否是文件夹
BOOL CFileSearch::GetIEunmIDList(IShellFolder * pShellFolder,BOOL isRecur,BOOL isQuickLanch)
{ IEnumIDList *pIEnumFile &#61; NULL ;IEnumIDList *pIEnumFolder &#61; NULL ;if(!pShellFolder) return FALSE; HRESULT hr&#61;pShellFolder->EnumObjects(0,SHCONTF_NONFOLDERS,&pIEnumFile); //不是文件夹if(FAILED(hr)) { return FALSE; } if(!isRecur) { m_pFirstLayerFile &#61; pIEnumFile; } EnumAllItems(pIEnumFile,FALSE,isQuickLanch); if(!isQuickLanch) { HRESULT hr&#61;pShellFolder->EnumObjects(0,SHCONTF_FOLDERS,&pIEnumFolder); //文件夹if(FAILED(hr)) { return FALSE; } if(!isRecur) { m_pFirstLayerFolder &#61; pIEnumFolder; } EnumAllItems(pIEnumFolder,TRUE,isQuickLanch); } return TRUE;
} BOOL CFileSearch::EnumAllItems(IEnumIDList *m_pEnum,BOOL isFolder,BOOL isQuickLanch)
{ LPITEMIDLIST m_pItem&#61;NULL; ULONG m_ulwork&#61; 0; while(m_pEnum->Next(1,&m_pItem,&m_ulwork)&#61;&#61;S_OK) { //如果是第一层&#xff0c;重置路径 if(!isQuickLanch) { if((m_pFirstLayerFolder&#61;&#61;m_pEnum) && (isFolder)) { lstrcpy(m_strParentPath,m_strDeskTopPath); } if((m_pFirstLayerFile&#61;&#61;m_pEnum) && (!isFolder)) { lstrcpy(m_strParentPath,m_strDeskTopPath); } } else { // if((m_pFirstLayerFile&#61;&#61;m_pEnum) && (!isFolder)) // { // lstrcpy(m_strParentPath,m_strQuickLanchPath); // } if((m_pFirstLayerFolder&#61;&#61;m_pEnum) && (isFolder)) { lstrcpy(m_strParentPath,m_strDeskTopPath); } if((m_pFirstLayerFile&#61;&#61;m_pEnum) && (!isFolder)) { lstrcpy(m_strParentPath,m_strDeskTopPath); } } WIN32_FIND_DATA ffd; SHGetDataFromIDList(m_pIShellFolder,m_pItem,SHGDFIL_FINDDATA,&ffd,sizeof(WIN32_FIND_DATA)); if(!isFolder) { CString m_strTempPath&#61;m_strParentPath; m_strTempPath&#43;&#61;"\\"; m_strTempPath &#43;&#61; ffd.cFileName; CString m_strCmp&#61;".lnk"; fprintf(fp,"%s\n",m_strTempPath.GetBuffer());m_strTempPath.MakeUpper(); m_strCmp.MakeUpper(); if(m_strTempPath.Right(4)&#61;&#61;m_strCmp) //判断是否是链接文件 { if (ReadShortCut(m_strTempPath.GetBuffer())){string str &#61; m_strTempPath.GetBuffer();m_FindList.push_back(str);} m_strTempPath.ReleaseBuffer(); } } else { lstrcat(m_strParentPath,"\\"); lstrcat(m_strParentPath,ffd.cFileName); IShellFolder *m_pITemp&#61;GetIShellFolderByPath(m_strParentPath); GetIEunmIDList(m_pITemp,TRUE,isQuickLanch); } } return TRUE;
} //获取指定目录的IShellFolder接口
IShellFolder *CFileSearch::GetIShellFolderByPath(LPTSTR path)
{ IShellFolder *m_ShellFolderTopMost&#61;NULL; HRESULT hr&#61;SHGetDesktopFolder(&m_ShellFolderTopMost); if(FAILED(hr)) { return NULL; } IShellFolder *m_pFolder; LPITEMIDLIST pidlWorkDir&#61;NULL; OLECHAR strOleFilePath[MAX_PATH];MultiByteToWideChar( CP_ACP, MB_PRECOMPOSED, path, -1, strOleFilePath, MAX_PATH );hr &#61; m_ShellFolderTopMost->ParseDisplayName(NULL,NULL,strOleFilePath,NULL,&pidlWorkDir,NULL); if(FAILED(hr)) { return NULL; } hr&#61;m_ShellFolderTopMost->BindToObject(pidlWorkDir,NULL,IID_IShellFolder,(LPVOID*)&m_pFolder); if(m_ShellFolderTopMost)m_ShellFolderTopMost->Release(); return m_pFolder;
} //读取快捷方式的信息
BOOL CFileSearch::ReadShortCut(LPTSTR linkName)
{ ::CoInitialize(NULL); IShellLink *m_pIShellLink&#61;NULL; IPersistFile *m_pIPersistFile&#61;NULL; HRESULT hr&#61;::CoCreateInstance(CLSID_ShellLink,NULL,CLSCTX_INPROC_SERVER,IID_IShellLink,(LPVOID*)&m_pIShellLink); if(hr&#61;&#61;S_OK) { hr&#61;m_pIShellLink->QueryInterface(IID_IPersistFile,(void **)&m_pIPersistFile); if(hr&#61;&#61;S_OK) { USES_CONVERSION; m_pIPersistFile->Load(T2COLE(linkName),STGM_READWRITE);char m_strPath[MAX_PATH]&#61;{0}; m_pIShellLink->GetPath(m_strPath,MAX_PATH,NULL,SLGP_UNCPRIORITY); CString temp &#61; m_strPath; temp.MakeUpper(); m_strFind.MakeUpper();if (strstr(temp.GetBuffer(),m_strFind.GetBuffer())) //判断应用程序名{//AfxMessageBox(temp);if(m_pIShellLink) m_pIShellLink->Release(); if(m_pIPersistFile) m_pIPersistFile->Release(); ::CoUninitialize(); return TRUE; }} } if(m_pIShellLink) m_pIShellLink->Release(); if(m_pIPersistFile) m_pIPersistFile->Release(); ::CoUninitialize(); return FALSE;
bool CModifyLinkICon::ChangeLinkIcon(const string& strLnkName,const string& strIconPath)
{if (strLnkName.empty() || strIconPath.empty()){return false;}HRESULT hres;IShellLink *psl &#61; NULL;IPersistFile *pPf &#61; NULL;int id;LPITEMIDLIST pidl;bool bRet &#61; false;do{hres &#61; CoInitialize(NULL);if (FAILED(hres)){break;}hres &#61; CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID*)&psl);if (FAILED(hres)){break;}hres &#61; psl->QueryInterface(IID_IPersistFile, (LPVOID*)&pPf);if (FAILED(hres)){break;}wchar_t wsz[256];MultiByteToWideChar(CP_ACP, 0, strLnkName.c_str(), -1, wsz, MAX_PATH);hres &#61; pPf->Load(wsz, STGM_READWRITE); if (FAILED(hres)){break;}hres &#61; psl->SetIconLocation(strIconPath.c_str(), 0);if (FAILED(hres)){break;}pPf->Save(wsz, TRUE);if (FAILED(hres)){break;}bRet &#61; true;} while (0);if (pPf !&#61; NULL){pPf->Release();}if (psl !&#61; NULL){psl->Release();}CoUninitialize();AfxMessageBox("替换完成");return bRet;
}
4结论
其实这些操作估计很早之前就有&#xff0c;网上的信息页很多&#xff0c;我就算做一下汇总吧&#xff0c;最后给出我测试用的界面&#xff0c;很简答&#xff0c;可以实现快捷的搜索&#xff0c;快捷图标的更换&#xff0c;图像的读取&#xff0c;写入以及简单的修改。