得到给定扩展名的文件图标

原文:

地址:http://www.flawlesscode.com/post/2008/02/Getting-the-icon-for-a-given-file-extension.aspx

代码下载:http://www.flawlesscode.com/file.axd?file=IconUtils.zip

主要代码:

 

using  System;
using  System.Drawing;
using  System.Runtime.InteropServices;

namespace  FlawlessCode.IconUtils
{
    
public   static   class  IconUtils
    {
        
///   <summary>
        
///  Used to access system folder icons.
        
///   </summary>
        
///   <param name="largeIcon"> Specify large or small icons. </param>
        
///   <param name="openFolder"> Specify open or closed FolderType. </param>
        
///   <returns> The requested Icon. </returns>
         public   static  Icon GetIconForFolder(Boolean largeIcon, Boolean openFolder)
        {
            SHFILEINFO shellFileInfo 
=   new  SHFILEINFO();
            
try
            {
                
uint  flags  =  SHGFI_ICON  |  SHGFI_USEFILEATTRIBUTES;
                flags 
|=  openFolder  ?  SHGFI_OPENICON :  0 ;
                flags 
|=  largeIcon  ?  SHGFI_LARGEICON : SHGFI_SMALLICON;

                SHGetFileInfo(
null ,
                    FILE_ATTRIBUTE_DIRECTORY,
                    
ref  shellFileInfo,
                    (
uint )Marshal.SizeOf(shellFileInfo),
                    flags);

                
return  (Icon)Icon.FromHandle(shellFileInfo.hIcon).Clone();
            }
            
finally
            {
                DestroyIcon(shellFileInfo.hIcon);        
//  Cleanup
            }
        }

        
///   <summary>
        
///  Used to access file icons for a given extension.
        
///   </summary>
        
///   <param name="extension"> The file extension to get the icon for. </param>
        
///   <param name="largeIcon"> Specify large or small icons. </param>
        
///   <param name="linkOverlay"> Specify link overlay on the icon. </param>
        
///   <returns> The requested Icon </returns>
         public   static  Icon GetIconForFileExtension(String extension, Boolean largeIcon, Boolean linkOverlay)
        {
            
if  ( ! extension.StartsWith( " . " ))
                extension 
=   " . "   +  extension;

            SHFILEINFO shellFileInfo 
=   new  SHFILEINFO();
            
try
            {
                
uint  flags  =  SHGFI_ICON  |  SHGFI_USEFILEATTRIBUTES;
                flags 
|=  linkOverlay  ?  SHGFI_LINKOVERLAY :  0 ;
                flags 
|=  largeIcon  ?  SHGFI_LARGEICON : SHGFI_SMALLICON;

                SHGetFileInfo(extension,
                    FILE_ATTRIBUTE_NORMAL,
                    
ref  shellFileInfo,
                    (
uint )Marshal.SizeOf(shellFileInfo),
                    flags);

                
return  (Icon)Icon.FromHandle(shellFileInfo.hIcon).Clone();
            }
            
finally
            {
                DestroyIcon(shellFileInfo.hIcon);
            }
        }

        
#region  P/Invoke
        
private   const   int  MAX_PATH  =   256 ;
        
        
private   const   uint  SHGFI_ICON  =   0x000000100 ;
        
private   const   uint  SHGFI_LINKOVERLAY  =   0x000008000 ;
        
private   const   uint  SHGFI_LARGEICON  =   0x000000000 ;
        
private   const   uint  SHGFI_SMALLICON  =   0x000000001 ;
        
private   const   uint  SHGFI_OPENICON  =   0x000000002 ;
        
private   const   uint  SHGFI_USEFILEATTRIBUTES  =   0x000000010 ;

        
private   const   uint  FILE_ATTRIBUTE_DIRECTORY  =   0x00000010 ;
        
private   const   uint  FILE_ATTRIBUTE_NORMAL  =   0x00000080 ;

        [StructLayout(LayoutKind.Sequential)]
        
private   struct  SHFILEINFO
        {
            
public   const   int  NAMESIZE  =   80 ;
            
public  IntPtr hIcon;
            
public   int  iIcon;
            
public   uint  dwAttributes;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst 
=  MAX_PATH)]
            
public   string  szDisplayName;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst 
=  NAMESIZE)]
            
public   string  szTypeName;
        };

        [DllImport(
" Shell32.dll " )]
        
private   static   extern  IntPtr SHGetFileInfo(
            
string  pszPath,
            
uint  dwFileAttributes,
            
ref  SHFILEINFO psfi,
            
uint  cbFileInfo,
            
uint  uFlags
            );

        [DllImport(
" User32.dll " )]
        
private   static   extern   int  DestroyIcon(IntPtr hIcon);
        
#endregion
    }
}
下面是一个简单的示例,展示了如何使用Qt编写一个嵌入式终端应用程序,实现文件浏览器模块。在这个示例中,我们将.h文件和.cpp文件分开。 首先,创建一个名为 "FileBrowserWidget" 的自定义QWidget类,用于实现文件浏览器的界面和逻辑。 **filebrowserwidget.h**: ```cpp #ifndef FILEBROWSERWIDGET_H #define FILEBROWSERWIDGET_H #include <QWidget> #include <QTreeView> #include <QStandardItemModel> class FileBrowserWidget : public QWidget { Q_OBJECT public: explicit FileBrowserWidget(QWidget *parent = nullptr); private: QTreeView *treeView; QStandardItemModel *model; void setupUI(); void populateTreeView(const QString &path); void openFile(const QModelIndex &index); private slots: void onItemDoubleClicked(const QModelIndex &index); }; #endif // FILEBROWSERWIDGET_H ``` **filebrowserwidget.cpp**: ```cpp #include "filebrowserwidget.h" #include <QVBoxLayout> #include <QDir> #include <QMessageBox> #include <QFile> #include <QTextStream> #include <QImage> #include <QLabel> FileBrowserWidget::FileBrowserWidget(QWidget *parent) : QWidget(parent) { setupUI(); } void FileBrowserWidget::setupUI() { QVBoxLayout *layout = new QVBoxLayout(this); treeView = new QTreeView(this); model = new QStandardItemModel(this); treeView->setModel(model); connect(treeView, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(onItemDoubleClicked(QModelIndex))); layout->addWidget(treeView); setLayout(layout); populateTreeView(QDir::rootPath()); } void FileBrowserWidget::populateTreeView(const QString &path) { model->clear(); QDir dir(path); QFileInfoList fileList = dir.entryInfoList(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot); foreach (const QFileInfo &fileInfo, fileList) { QStandardItem *item = new QStandardItem(fileInfo.fileName()); item->setData(fileInfo.filePath(), Qt::UserRole); if (fileInfo.isDir()) { item->setIcon(QIcon(":/icons/folder.png")); } else { item->setIcon(QIcon(":/icons/file.png")); } model->appendRow(item); } } void FileBrowserWidget::onItemDoubleClicked(const QModelIndex &index) { QString filePath = index.data(Qt::UserRole).toString(); QFileInfo fileInfo(filePath); if (fileInfo.isFile()) { if (fileInfo.suffix() == "jpg" || fileInfo.suffix() == "png") { // 图片操作 QImage image(filePath); QLabel *label = new QLabel; label->setPixmap(QPixmap::fromImage(image)); label->show(); } else if (fileInfo.suffix() == "txt") { // 文档浏览 QFile file(filePath); if (file.open(QIODevice::ReadOnly)) { QTextStream stream(&file); QString content = stream.readAll(); file.close(); // 在此处显示文档内容 QMessageBox::information(this, "文档内容", content); } } } else if (fileInfo.isDir()) { populateTreeView(filePath); } } ``` 在这个示例中,我们创建了一个FileBrowserWidget类,继承自QWidget。它包含一个QTreeView用于显示文件文件夹,并使用QStandardItemModel作为数据模型。 在setupUI()函数中,我们创建了一个垂直布局并将QTreeView添加到布局中。然后,我们连接了QTreeView的doubleClicked()信号到槽函数onItemDoubleClicked()。 populateTreeView()函数用于根据给定的路径填充QTreeView。它使用QDir获取路径下的文件文件夹,并创建相应的QStandardItem添加到model中。 onItemDoubleClicked()槽函数处理双击事件。根据文件类型进行不同的操作,如果是图片文件,则显示图片;如果是文本文件,则显示文档内容。 这只是一个简单的示例,你可以根据自己的需求进行进一步的功能扩展和界面优化。同时,你可以添加适当的按钮和输入框来实现新建文件夹和文档的功能。 请注意,示例中的图标文件需要在资源文件中添加,并相应地修改图标的路径。 希望这个示例能对你有所帮助!如果有任何问题,请随时提问。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值