获取文件图标,类型名称,属性 SHGetFileInfo

来源:互联网 发布:淘宝店铺广告图片750 编辑:程序博客网 时间:2024/05/18 21:42

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

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

[cpp] view plaincopy
  1. // 【MoreWindows工作笔记4】 获取文件图标,类型名称,属性 SHGetFileInfo  
  2. #include <Windows.h>  
  3. #include <iostream>  
  4. using namespace std;  
  5. int main()  
  6. {  
  7.   printf("   【MoreWindows工作笔记4】 获取文件图标,类型名称,属性 SHGetFileInfo\n");      
  8.   printf(" - http://blog.csdn.net/morewindows/article/details/16358681 -\n");      
  9.   printf(" -- By MoreWindows( http://blog.csdn.net/MoreWindows ) --\n\n");    
  10.   
  11.   CoInitialize(NULL);  
  12.   char file_name[] = "C:\\MoreWindows.mp3";  
  13.   SHFILEINFO sfi = {0};  
  14.     
  15.   cout<<"file name = "<<file_name<<endl;  
  16.   
  17.   // type name  
  18.   SHGetFileInfo(file_name, 0, &sfi, sizeof(sfi), SHGFI_USEFILEATTRIBUTES | SHGFI_TYPENAME);  
  19.   cout<<"type name = "<<sfi.szTypeName<<endl;  
  20.   
  21.   // display name  
  22.   SHGetFileInfo(file_name, 0, &sfi, sizeof(sfi), SHGFI_USEFILEATTRIBUTES | SHGFI_DISPLAYNAME);  
  23.   cout<<"display name = "<<sfi.szDisplayName<<endl;  
  24.   
  25.   // attribute  
  26.   SHGetFileInfo(file_name, 0, &sfi, sizeof(sfi), SHGFI_USEFILEATTRIBUTES | SHGFI_ATTRIBUTES);  
  27.   cout<<"attribute = "<<hex<<sfi.dwAttributes<<endl;  // 使用IShellFolder::GetAttributesOf函数解析  
  28.     
  29.   // HICON  
  30.   // 除了SHGFI_ICON之外还有SHGFI_LARGEICON(大图标), SHGFI_SMALLICON(小图标)  
  31.   SHGetFileInfo(file_name, 0, &sfi, sizeof(sfi), SHGFI_USEFILEATTRIBUTES | SHGFI_ICON);  
  32.   cout<<"HICON = 0x"<<hex<<sfi.hIcon<<endl;  // 使用IShellFolder::GetAttributesOf函数解析  
  33.   DestroyIcon(sfi.hIcon);  
  34.   
  35.   // HICON system index  
  36.   SHGetFileInfo(file_name, 0, &sfi, sizeof(sfi), SHGFI_USEFILEATTRIBUTES | SHGFI_SYSICONINDEX);  
  37.   cout<<"HICON system index = "<<sfi.iIcon<<endl;  // 使用IShellFolder::GetAttributesOf函数解析  
  38.   
  39.   CoUninitialize();  
  40.   
  41.   // SHGFI_USEFILEATTRIBUTES 的说明  
  42.   // Indicates that the function should not attempt to access the file specified by pszPath.   
  43.   // Rather, it should act as if the file specified by pszPath exists with the file attributes passed in dwFileAttributes.  
  44.   // This flag cannot be combined with the SHGFI_ATTRIBUTES, SHGFI_EXETYPE, or SHGFI_PIDL flags.  
  45.   return 0;  
  46. }  

运行结果如图所示:

.mp3文件:

.txt文件

.docx文件

 

 

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

0 0