我想在listctrl显示某一个文件夹里面的文件!有很多种类型,我想有没有什么方法获得系统的那个文件图标!!

解决方案 »

  1.   

    我只会用
    CString strSystemDir;
    GetSystemDirect(strSystemDir.GetBuffer(MAX_PATH),MAX_PATH);

    strSystemDir.ReleaseBuffer();
    strSystemDir+="\\";
    ExtractIconEx(strSystemDir+"shell32.dll",...............);
      

  2.   

    使用API函数:SHGetFileInfo(),并指定参数逶SHGFI_ICON
      

  3.   

    要啊!我想获得任意一个文件和它的图标,然后在listctrl控件中显示这个文件名和它的系统图标!有程序最好了!!::))多谢!
      

  4.   

    SHFILEINFO  sfi;
    CImageList FileImageList;
    HIMAGELIST hSysImageList;
    hSysImageList = (HIMAGELIST)SHGetFileInfo(TEXT("C:\\"), 0, &sfi,
    sizeof(SHFILEINFO), SHGFI_SYSICONINDEX|SHGFI_SMALLICON ); //得到系统的imagelist的句柄
    m_FileImageList.Attach(m_hSysImageList));
    m_YourListCtrl.SetImageList(&m_FileImageList,LVSIL_SMALL);
    CFileFind Finder;
    BOOL bWorking=Finder.FindFile((LPCTSTR)(strYourPath+"\\*.*"),0);
    CString Filename;
    SHFILEINFO  sfi;
    while(bWorking)
    {
    bWorking=Finder.FindNextFile();
    if(bWorking) 
    {
    Filename=Finder.GetFileName();
    if(Finder.IsDots()) continue;
    SHGetFileInfo(Finder.GetFilePath(),0,&sfi,sizeof(SHFILEINFO),SHGFI_SMALLICON |SHGFI_ICON|SHGFI_TYPENAME  );//得到该文件的图标的index:sfi.iIcon
    InsertItem(0,Finder.GetFileTitle(),sfi.iIcon);

    }
    }
      

  5.   

    更正:
    InsertItem(0,Finder.GetFileTitle(),sfi.iIcon);
    应为:
    YourListCtrl.InsertItem(0,Finder.GetFileTitle(),sfi.iIcon);
    不好意思。
      

  6.   

    再次更正:
    去掉 Filename=Finder.GetFileName();
    否则,会少列出一个文件/文件夹。
    疏忽疏忽。
      

  7.   

    再再次更正:
    去掉if(bWorking) 
    这才是导致少列出一个文件的原因
      

  8.   

    SHFILEINFO  sfi;
    CImageList FileImageList;
    HIMAGELIST hSysImageList;
    hSysImageList = (HIMAGELIST)SHGetFileInfo(TEXT("C:\\"), 0, &sfi,
    sizeof(SHFILEINFO), SHGFI_SYSICONINDEX|SHGFI_SMALLICON ); 
    //得到系统的imagelist的句柄
    m_FileImageList.Attach(m_hSysImageList));
    m_YourListCtrl.SetImageList(&m_FileImageList,LVSIL_SMALL);
    //设置listctrl的imagelist
    CFileFind Finder;
    BOOL bWorking=Finder.FindFile((LPCTSTR)(strYourPath+_T("\\*.*")),0);
    CString Filename;
    SHFILEINFO  sfi;
    while(bWorking)
    {
    bWorking=Finder.FindNextFile();
    if(bWorking) 
    {
    Filename=Finder.GetFileName();
    if(Finder.IsDots()) continue;//忽略..和.
    SHGetFileInfo(Finder.GetFilePath(),0,&sfi,sizeof(SHFILEINFO),SHGFI_SMALLICON |SHGFI_ICON|SHGFI_TYPENAME  );
                       //得到该文件图标的index: sfi.iIcon
    InsertItem(0,Finder.GetFileTitle(),sfi.iIcon);

    }
    }
      

  9.   

    SHFILEINFO  sfi;
    CImageList FileImageList;
    HIMAGELIST hSysImageList;
    hSysImageList = (HIMAGELIST)SHGetFileInfo(_T("C:\\"), 0, &sfi,
    sizeof(SHFILEINFO), SHGFI_SYSICONINDEX|SHGFI_SMALLICON ); 
    //得到系统的imagelist的句柄
    m_FileImageList.Attach(m_hSysImageList));
    m_YourListCtrl.SetImageList(&m_FileImageList,LVSIL_SMALL);
    //设置listctrl的imagelist
    CFileFind Finder;
    BOOL bWorking=Finder.FindFile((LPCTSTR)(strYourPath+_T("\\*.*")),0);
    CString Filename;
    SHFILEINFO  sfi;
    while(bWorking)
    {
    bWorking=Finder.FindNextFile();
    Filename=Finder.GetFileName();
    if(Finder.IsDots()) continue;//忽略.和..
    SHGetFileInfo(Finder.GetFilePath(),0,&sfi,sizeof(SHFILEINFO),SHGFI_SMALLICON |SHGFI_ICON|SHGFI_TYPENAME  );
             //得到该文件图标的index置于sfi.iIcon中
    YourListCtrl.InsertItem(0,Finder.GetFileTitle(),sfi.iIcon);

    }
      

  10.   

    对于 seanhut(段寄存器) 的责任心和关心!小弟感激不尽!!实验后马上回来发分!!:)
      

  11.   

    还有兄台!那程序我基本看明白了!不过就是运行效果不好!
    如果我在f盘的2000系统下,那么那个c:是不是换成f!不过这样我的程序还是不能显示啊!图标老是空!就是那个iIcon不被识别啊?
      

  12.   

    DirLV Sample Populates a ListView Control Similar to Explorer Q234310
    --------------------------------------------------------------------------------
    The information in this article applies to:The Microsoft Foundation Classes (MFC), included with:
    Microsoft Visual C++, 32-bit Enterprise Edition, version 5.0 
    Microsoft Visual C++, 32-bit Professional Edition, version 5.0 
    Microsoft Visual C++, 32-bit Enterprise Edition, version 6.0 
    Microsoft Visual C++, 32-bit Professional Edition, version 6.0 
    Microsoft Visual C++, 32-bit Learning Edition, version 6.0--------------------------------------------------------------------------------
    SUMMARY
    DirLV.exe is a sample that demonstrates how to implement an Explorer-like ListView control. The control handles some user-defined messages that make it display the contents of a specified directory. The ListView control displays the associated icon, the long file name, the size of the file, the file type, its creation date, and the file attributes.The sample illustrates how to: Use the system image list in an MFC application to display the icon associated with every file. 
    Implement the ListView control as a virtual ListView, that is, specify LVS_OWNERDATA as a style for the ListView control. This causes the system to send LVN_GETDISPINFO notifications to the control's parent for every item in the list that needs to be displayed. This approach greatly improves performance for large directories. 
    Implement mouse and keyboard navigation: double-clicking a directory, or pressing ENTER when the focus item is a directory, changes the displayed directory. 
    Use a fast sorting algorithm provided in the Standard Template Library (STL), to order the files alphabetically. 
    The sample does not use ITEMIDLISTs to identify shell elements (that is, files) nor does it use any of the associated shell interfaces. As a consequence, it does not recognize link files as such, it does not handle virtual directories, and it fails to identify an icon for console applications that don't have an icon associated with them. MORE INFORMATION
    The following files are available for download from the Microsoft Download Center:http://download.microsoft.com/download/vc60pro/other/1.0/win98/EN-US/DirLV.exeFor additional information about how to download Microsoft Support files, click the article number below to view the article in the Microsoft Knowledge Base: 
    Q119591 How to Obtain Microsoft Support Files from Online Services 
    Microsoft used the most current virus detection software available on the date of posting to scan this file for viruses. Once posted, the file is housed on secure servers that prevent any unauthorized changes to the file. REFERENCES
    See the VListVw sample in the Online documentation.(c) Microsoft Corporation Cosmin Radu, All Rights Reserved. Contributions by 1999, Microsoft Corporation.Additional query words: List View CListCtrl SHGetFileInfo Directory LB_DIR Virtual Control Keywords : kbsample kbCmnCtrls kbListView kbMFC kbVC500 kbVC600 kbDSupport kbGrpDSMFCATL 
    Issue type : 
    Technology : kbAudDeveloper kbMFC 
    Last Reviewed: May 3, 2001
    © 2001 Microsoft Corporation. All rights reserved. Terms of Use.
     --------------------------------------------------------------------------------
    Send feedback to MSDN.Look here for MSDN Online resources.
      

  13.   

    对于“C:",只要是系统的一个驱动器就行了,无所谓,它的作用是取得系统(不是某个驱动器)的imagelist。你显示不出图标可能使因为你的FileImageList被释放了的缘故,FileImageList对象必须在你的LISTCTRL存在的期间也存在。我把调好的类寄给你吧,ok?
      

  14.   

    对于“C:",只要是系统的一个驱动器就行了,无所谓,它的作用是取得系统(不是某个驱动器)的imagelist。你显示不出图标可能使因为你的FileImageList被释放了的缘故,FileImageList对象必须在你的LISTCTRL存在的期间也存在。我把调好的类寄给你吧,ok?