如果想得到windows中我的电脑或者网络邻居等系统图标可以有如下解决办法。
方案一
用图标提取软件从c:\winnt\system32\shell32.dll或者c:\winnt\explorer.exe中提取出来然后加入到自己的程序中(路径随操作系统和安装目录不同而变化)这种方法的缺点是不同的操作系统的系统图标有一些变化,比如说2003和2000的系统图标就有很大变化,如果你提取的是2000的图标,应用程序如果在2003下运行可能看起来有一些别扭。
方案二
同样是利用c:\winnt\system32\shell32文件来提取,但是这次是利用注册表和windows的函数来完成。代码如下:
hicon ctestdlg::getshellicon(int nindex)
{
hicon hicon=null;
hkey hkeyshellicons;
//打开注册表,读相应的图标项目
if(regopenkeyex(hkey_local_machine,"software\\microsoft\\windows\\currentversion\\explorer\\shell i cons", 0,key_read,&hkeyshellicons)==error_success)
{
char szbuffer[max_path];
dword dwsize=max_path;
char szindex[4];
sprintf(szindex,"%d",nindex);
if(regqueryvalueex(hkeyshellicons,szindex,null,null,(lpbyte)szbuffer,&dwsize)==error_success)
{
cstring strfile,strindex;
afxextractsubstring(strfile,szbuffer,0,',');
afxextractsubstring(strindex,szbuffer,1,',');
extracticonex(strfile,atoi(strindex),null,&hicon,1);
}
regclosekey(hkeyshellicons);
}
if(!hicon)
extracticonex("shell32.dll",nindex,null,&hicon,1);
return hicon;
}
这种方法克服了第一种方法使用不灵活的缺点但是代码量比较大。(index目录在最后列出)
方案三
利用shell函数shgetspecialfolderlocation和shgetfileinfo。代码如下:
lpitemidlist lpitemidlist;
shfileinfo shinfo;
shgetspecialfolderlocation (this->m_hwnd, csidl_network, &lpitemidlist);
shgetfileinfo( (lpctstr)lpitemidlist,
null,
&shinfo,
sizeof(shinfo),
shgfi_sysiconindex | shgfi_displayname | shgfi_icon| shgfi_smallicon|shgfi_pidl);
这是shinfo中的icon和hicon保存了得到的icon的值。
这种方法既有灵活性,代码量也比较少,推荐使用。在shgetspecialfolderlocation中的第二个参数是想得到的图片的csidl值。
【附表1:nindex值说明】
nindex 意义 说明