【MoreWindows工作笔记4】 获取文件图标,类型名称,属性 SHGetFileInfo

工作后比较忙,没大块的时间来酝酿博客的写作,所以随时记点笔记,一来方便自己查阅,二来也督促自己学习和总结。如果能对大家有所帮助,就更加开心了大笑

 

 SHGetFileInfo是一个相当实用的Windows API函数。在Windows资源管理器中就要使用它。如图中的第3列显示的就是文件的类型名称

下面来看看这个函数的详细用法:

// 【MoreWindows工作笔记4】 获取文件图标,类型名称,属性 SHGetFileInfo
#include <Windows.h>
#include <iostream>
using namespace std;
int main()
{
  printf("   【MoreWindows工作笔记4】 获取文件图标,类型名称,属性 SHGetFileInfo\n");    
  printf(" - http://blog.csdn.net/morewindows/article/details/16358681 -\n");    
  printf(" -- By MoreWindows( http://blog.csdn.net/MoreWindows ) --\n\n");  

  CoInitialize(NULL);
  char file_name[] = "C:\\MoreWindows.mp3";
  SHFILEINFO sfi = {0};
  
  cout<<"file name = "<<file_name<<endl;

  // type name
  SHGetFileInfo(file_name, 0, &sfi, sizeof(sfi), SHGFI_USEFILEATTRIBUTES | SHGFI_TYPENAME);
  cout<<"type name = "<<sfi.szTypeName<<endl;

  // display name
  SHGetFileInfo(file_name, 0, &sfi, sizeof(sfi), SHGFI_USEFILEATTRIBUTES | SHGFI_DISPLAYNAME);
  cout<<"display name = "<<sfi.szDisplayName<<endl;

  // attribute
  SHGetFileInfo(file_name, 0, &sfi, sizeof(sfi), SHGFI_USEFILEATTRIBUTES | SHGFI_ATTRIBUTES);
  cout<<"attribute = "<<hex<<sfi.dwAttributes<<endl;  // 使用IShellFolder::GetAttributesOf函数解析
  
  // HICON
  // 除了SHGFI_ICON之外还有SHGFI_LARGEICON(大图标), SHGFI_SMALLICON(小图标)
  SHGetFileInfo(file_name, 0, &sfi, sizeof(sfi), SHGFI_USEFILEATTRIBUTES | SHGFI_ICON);
  cout<<"HICON = 0x"<<hex<<sfi.hIcon<<endl;  // 使用IShellFolder::GetAttributesOf函数解析
  DestroyIcon(sfi.hIcon);

  // HICON system index
  SHGetFileInfo(file_name, 0, &sfi, sizeof(sfi), SHGFI_USEFILEATTRIBUTES | SHGFI_SYSICONINDEX);
  cout<<"HICON system index = "<<sfi.iIcon<<endl;  // 使用IShellFolder::GetAttributesOf函数解析

  CoUninitialize();

  // SHGFI_USEFILEATTRIBUTES 的说明
  // Indicates that the function should not attempt to access the file specified by pszPath. 
  // Rather, it should act as if the file specified by pszPath exists with the file attributes passed in dwFileAttributes.
  // This flag cannot be combined with the SHGFI_ATTRIBUTES, SHGFI_EXETYPE, or SHGFI_PIDL flags.
  return 0;
}

运行结果如图所示:

.mp3文件:

.txt文件

.docx文件

 

 

地址:http://blog.csdn.net/morewindows/article/details/16358681 转载请标明出处,谢谢。

欢迎关注微博:http://weibo.com/MoreWindows  

 


 

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值