windows 如何使用C++获取文件图标
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1061871/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get file icon using C++
提问by
I want to add Icon to treeview node, using C++. I want to get the icons from system, I tried
我想使用 C++ 将 Icon 添加到 treeview 节点。我想从系统中获取图标,我试过了
I tried with,
我试过,
PMString ucPath("C:\path\to\file.extension");
SHFILEINFO info;
::SHGetFileInfo(ucPath.GrabTString(), FILE_ATTRIBUTE_NORMAL, &info, sizeof(info),
SHGFI_ICON | SHGFI_USEFILEATTRIBUTES | SHGFI_SMALLICON);
iconView->SetRsrcID((RsrcID) info.hIcon);
::DestroyIcon(info.hIcon);
where, SetResrcID ,PMString are the InDesing API and iconView is the controlView of the Tree, I am not getting what's going wrong, if anyone has idea please suggest.
其中, SetResrcID ,PMString 是 InDesing API,iconView 是 Tree 的 controlView,我不明白出了什么问题,如果有人有想法请提出建议。
Thanks, Praveen Mamdge
谢谢,Praveen Mamdge
回答by Yigang Wu
Here is the codes what I'm using in my application, you should change the icon to a bitmap.
这是我在应用程序中使用的代码,您应该将图标更改为位图。
PMString ucPath("C:\path\to\file.extension");
SHFILEINFO info;
::SHGetFileInfo(ucPath.GrabTString(), FILE_ATTRIBUTE_NORMAL, &info, sizeof(info),
SHGFI_ICON | SHGFI_USEFILEATTRIBUTES | SHGFI_SMALLICON);
ICONINFO stIconInfo;
GetIconInfo(s_sfi.hIcon, &stIconInfo);
HBITMAP hBmp = stIconInfo.hbmColor;
DestroyIcon(s_sfi.hIcon) ;
The best way to do it is using the system icon index with SHGFI_SYSICONINDEX.
最好的方法是使用系统图标索引和 SHGFI_SYSICONINDEX。
回答by Sauron
Some thing like this, Extract icon from file first.
像这样的事情,首先从文件中提取图标。
SHFILEINFO stFileInfo;
SHGetFileInfo( file,
FILE_ATTRIBUTE_NORMAL,
&stFileInfo,
sizeof( stFileInfo ),
SHGFI_ICON | SHGFI_LARGEICON );
Then add to imagelist and use the index to set icon.
然后添加到图像列表并使用索引设置图标。
m_nIndex = m_ilLargeIcons.Add( stFileInfo.hIcon );
回答by Abet
This is your code snippet, observe line by line:
这是您的代码片段,逐行观察:
PMString ucPath("C:\path\to\file.extension"); SHFILEINFO info;
::SHGetFileInfo(ucPath.GrabTString(), FILE_ATTRIBUTE_NORMAL, &info, sizeof(info), SHGFI_ICON | SHGFI_USEFILEATTRIBUTES | SHGFI_SMALLICON);iconView->SetRsrcID((RsrcID) info.hIcon);
::DestroyIcon(info.hIcon);
PMString ucPath("C:\path\to\file.extension"); SHFILEINFO信息;
::SHGetFileInfo(ucPath.GrabTString(), FILE_ATTRIBUTE_NORMAL, &info, sizeof(info), SHGFI_ICON | SHGFI_USEFILEATTRIBUTES | SHGFI_SMALLICON);iconView->SetRsrcID((RsrcID) info.hIcon);
::DestroyIcon(info.hIcon);
After this line: iconView->SetRsrcID((RsrcID) info.hIcon);
, you called ::DestroyIcon that destroyed that icon you stored.
在此行之后:iconView->SetRsrcID((RsrcID) info.hIcon);
,您调用了 ::DestroyIcon 来销毁您存储的那个图标。