CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
IBM Rational 系统开发最佳实践工具包 WebSphere MQ 最佳实践 TOP 15
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  .NET技术 >  VB.NET

怎么样根据后缀显示系统图标?

楼主copico(北北)2006-03-03 15:21:26 在 .NET技术 / VB.NET 提问

在listveiw里,怎么样根据文件的后缀来显示系统的图标呢? 问题点数:20、回复次数:3Top

1 楼fengjun19912(学于睿智)回复于 2006-03-03 15:25:01 得分 20

正好我做过,给一段代码你参考一下:  
   
  using   System;  
  using   System.Drawing;  
  using   System.Windows.Forms;  
   
  namespace   YJDSoft.Control.Data.LocalDataExplorer  
  {  
  ///   <summary>  
  ///   ImageIconManage   的摘要说明。  
  ///   </summary>  
  public   class   ImageIconManage  
  {  
  private   System.Windows.Forms.ImageList       m_SmallImageList;  
  private   System.Windows.Forms.ImageList       m_LargeImageList;  
  //private   System.IntPtr     m_FormHandle;  
  private   int   IconIndex   =   0   ;  
  ///   <summary>  
  ///   图表管理类  
  ///   </summary>  
  ///   <param   name="SmallImageList">小图标集合的list</param>  
  ///   <param   name="LargeImageList">大图标集合的list</param>  
  public   ImageIconManage(System.Windows.Forms.ImageList   SmallImageList,System.Windows.Forms.ImageList   LargeImageList)  
  {  
  //  
  //   TODO:   在此处添加构造函数逻辑  
  //  
  m_SmallImageList   =   SmallImageList;  
  m_LargeImageList   =   LargeImageList;  
  //m_FormHandle   =   FormHandle;  
  m_LargeImageList.ImageSize   =   new   Size(32,32);  
   
  this.m_LargeImageList.Images.Clear();  
  this.m_SmallImageList.Images.Clear();  
  Icon   ic0=myExtractIcon("%SystemRoot%\\system32\\shell32.dll",3);  
  m_LargeImageList.Images.Add(ic0);  
  m_SmallImageList.Images.Add(ic0);  
  }  
  ///   <summary>  
  ///   得到该文件对应的图标(包括大图标,小图标)  
  ///   </summary>  
  ///   <param   name="FileNameOrExtension">文件的全路径或者文件的带点扩展名(如:   .Dll)</param>  
  ///   <returns>返回该文件或扩展名的图标在SmallImageList或LargeImageList里的索引值</returns>  
  public   int   FileBindingIcon(string   FileNameOrExtension)  
  {  
  try  
  {  
  SetIcon(m_SmallImageList,FileNameOrExtension,false);//得到小图标平铺  
  SetIcon(m_LargeImageList,FileNameOrExtension,true);//得到大图标平铺  
  return   IconIndex   =   IconIndex   +   1;  
  }  
  catch(Exception   ex)  
  {  
  MessageBox.Show(ex.Message,"错误提示",0,MessageBoxIcon.Error);  
  return   -1;  
  }  
  }  
  ///   <summary>  
  ///   得的目录的图标的索引值  
  ///   </summary>  
  public   int   GetFolderIcon  
  {  
  get  
  {  
  return   0; //目录图标的索引值默认为0  
  }  
  }  
   
  #region 用API函数调用取的文件对应的图标的所有方法  
  //*********************************************************************************************************//  
  [System.Runtime.InteropServices.DllImport("Shell32.dll")]    
  public   static   extern   int   ExtractIcon(IntPtr   h,string   strx,int   ii);  
   
  [System.Runtime.InteropServices.DllImport("Shell32.dll")]    
  public   static   extern   int   SHGetFileInfo(string   pszPath,uint   dwFileAttributes,ref   SHFILEINFO   psfi,uint   cbFileInfo,   uint   uFlags);  
   
  public   struct   SHFILEINFO  
  {    
  public   IntPtr   hIcon;      
  public   int       iIcon;      
  public   uint   dwAttributes;  
  public   char   szDisplayName;    
  public   char   szTypeName;    
  }  
   
  protected   virtual   void   SetIcon(ImageList   imageList,string   FileName,bool   tf)  
  {  
  SHFILEINFO   fi=new   SHFILEINFO();  
  if(tf==true)  
  {//得到大图标平铺  
  int   iTotal=(int)SHGetFileInfo(FileName,0,ref   fi,100,     16640);//SHGFI_ICON|SHGFI_SMALLICON  
  try  
  {  
  if(iTotal   >0)  
  {  
  Icon   ic=Icon.FromHandle(fi.hIcon);  
  imageList.Images.Add(ic);  
  //return   ic;  
  }  
  }  
  catch(Exception   ex)  
  {   MessageBox.Show(ex.Message,"错误提示",0,MessageBoxIcon.Error);}    
  }  
  else  
  {//得到小图标平铺  
  int   iTotal=(int)SHGetFileInfo(FileName,0,ref   fi,100,     257);  
  try  
  {  
  if(iTotal   >0)  
  {  
  Icon   ic=Icon.FromHandle(fi.hIcon);  
  imageList.Images.Add(ic);  
  //return   ic;  
  }  
  }  
  catch(Exception   ex)  
  {   MessageBox.Show(ex.Message,"错误提示",0,MessageBoxIcon.Error);}    
  }  
  }  
  //*********************************************************************************************************//  
  //*************************************************************************************    
     
  protected   virtual   Icon   myExtractIcon(string   FileName,int   iIndex)  
  {  
  try  
  {  
  //IntPtr   hIcon=(IntPtr)ExtractIcon(this.m_FormHandle,FileName,iIndex);  
  IntPtr   hIcon=(IntPtr)ExtractIcon(this.m_SmallImageList.Handle,FileName,iIndex);  
  if(!   hIcon.Equals(null))  
  {  
  Icon   icon=Icon.FromHandle(hIcon);  
  return   icon;  
  }  
  }  
  catch(Exception   ex)  
  {   MessageBox.Show(ex.Message,"错误提示",0,MessageBoxIcon.Error);}    
  return   null;  
  }  
  //*************************************************************************************  
  #endregion  
   
  }  
  }  
  Top

2 楼Qqwwee_Com(http://qqwwee.com)回复于 2006-03-03 15:50:40 得分 0

收藏。  
   
   
   
  ====CSDN   小助手   V2.5   ====  
  CSDN小助手是一款脱离浏览器也可以访问Csdn论坛的软件  
  速度快;使用方便;提供源代码。  
  界面:http://blog.csdn.net/Qqwwee_Com/category/146601.aspx  
  下载:http://szlawbook.com/csdnv2  
   
  Top

3 楼copico(北北)回复于 2006-03-05 00:27:12 得分 0

哇,你后面的数字给了我太大的帮助啦Top

相关问题

  • 如何用程序强制系统显示文件后缀名?
  • 是否有这样的后缀---可以使这个文件不可见?或者让它变成系统图标??
  • 什么办法可以获得某一类文件后缀(如.avi)的系统默认图标?
  • 图标的后缀名是什么?
  • 图标显示?
  • 怎样在系统托盘上显示程序图标????
  • WinXP系统托盘图标不显示,怎么办?
  • 【时钟显示】将图标显示在系统时间的位置
  • 是否有这样的文件名后缀---可以使这个文件不可见?或者让它变成系统图标??
  • 怎么按不同文件显示不同的图标?怎么取得系统图标?请教

关键词

  • 文件
  • ic
  • csdn
  • shell
  • dll
  • largeimagelist
  • 图标
  • smallimagelist
  • imageiconmanage
  • filenameorextension

得分解答快速导航

  • 帖主:copico
  • fengjun19912

相关链接

  • CSDN .NET频道
  • .NET类图书
  • C#类图书
  • .NET类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo