如何使用C ++获取文件图标

时间:2022-10-17 21:46:47

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是树的controlView,我没有得到什么问题,如果有人有想法请建议。

Thanks, Praveen Mamdge

谢谢,Praveen Mamdge

3 个解决方案

#1


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的系统图标索引。

#2


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.

然后添加到imagelist并使用索引来设置图标。

m_nIndex = m_ilLargeIcons.Add( stFileInfo.hIcon );

#3


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);

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来销毁你存储的那个图标。

#1


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的系统图标索引。

#2


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.

然后添加到imagelist并使用索引来设置图标。

m_nIndex = m_ilLargeIcons.Add( stFileInfo.hIcon );

#3


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);

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来销毁你存储的那个图标。