关于获得系统文件图标的问题

digitalghost 2003-08-30 03:29:41
我想做一个文件浏览器,用LISTVIEW 或TREEVIEW实现浏览区域!
对于每个文件夹及相应的文件,希望其显示的图标和"我的电脑"里显示的图标一致,比如WORD文档显示的是"W"的图标!
请问如何实现!
...全文
90 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
雪狼1234567 2003-08-30
  • 打赏
  • 举报
回复
http://www.kennyandkarin.com/Kenny/CodeCorner/Tools/IconBrowser/
我这里有一段代码,可获取任意文件的图标:
using System;
using System.Runtime.InteropServices;
using System.Drawing;
namespace WindowsAppTmp
{
public class ExtractIcon
{
[DllImport("Shell32.dll")]
private static extern int SHGetFileInfo
(
string pszPath,
uint dwFileAttributes,
out SHFILEINFO psfi,
uint cbfileInfo,
SHGFI uFlags
);

[StructLayout(LayoutKind.Sequential)]
private struct SHFILEINFO
{
public SHFILEINFO(bool b)
{
hIcon=IntPtr.Zero;iIcon=0;dwAttributes=0;szDisplayName="";szTypeName="";
}
public IntPtr hIcon;
public int iIcon;
public uint dwAttributes;
[MarshalAs(UnmanagedType.LPStr, SizeConst=260)]
public string szDisplayName;
[MarshalAs(UnmanagedType.LPStr, SizeConst=80)]
public string szTypeName;
};

private ExtractIcon()
{
}

private enum SHGFI
{
SmallIcon = 0x00000001,
LargeIcon = 0x00000000,
Icon = 0x00000100,
DisplayName = 0x00000200,
Typename = 0x00000400,
SysIconIndex = 0x00004000,
UseFileAttributes = 0x00000010
}


public static Icon GetIcon(string strPath, bool bSmall)
{
SHFILEINFO info = new SHFILEINFO(true);
int cbFileInfo = Marshal.SizeOf(info);
SHGFI flags;
if (bSmall)
flags = SHGFI.Icon|SHGFI.SmallIcon|SHGFI.UseFileAttributes;
else
flags = SHGFI.Icon|SHGFI.LargeIcon|SHGFI.UseFileAttributes;

SHGetFileInfo(strPath, 256, out info,(uint)cbFileInfo, flags);
return Icon.FromHandle(info.hIcon);
}
}
}



using System;
using System.Windows.Forms;
using System.Drawing;

namespace MyForm {
public class CreatedForm : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button;
private Label label;
public static void Main( )
{
Application.Run( new CreatedForm( ) );
}
public CreatedForm()
{
InitializeComponents();
}

void InitializeComponents() {
//
// Set up generated class form
//
this.SuspendLayout();
this.Name = "form";
this.Size = new System.Drawing.Size(200, 96);
this.Text = "Test";

//
// Set up member button
//
button = new System.Windows.Forms.Button();
button.Name = "button";
button.Location = new System.Drawing.Point(56, 24);
button.Size = new Size(100,22);
button.Text = "按钮";
button.Anchor = (System.Windows.Forms.AnchorStyles.Top|System.Windows.Forms.AnchorStyles.Left);
button.TabIndex = 0;
this.Controls.Add(button);
button.Click+=new EventHandler(this.button_Click);
this.ResumeLayout(false);
//
label = new Label();
//label.Text = "文件图标";
label.Location=new Point(5,5);
label.Size = new Size(40,40);
this.Controls.Add(label);
}
protected void button_Click(object sender, System.EventArgs e)
{
//Stream myStream;
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = "c:\\" ;
openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*" ;
openFileDialog1.FilterIndex = 2 ;
openFileDialog1.RestoreDirectory = true ;
if(openFileDialog1.ShowDialog() == DialogResult.OK)
{
if((openFileDialog1.OpenFile())!= null)
{
//label.Text=openFileDialog1.FileName;
label.Image=WindowsAppTmp.ExtractIcon.GetIcon(openFileDialog1.FileName, false).ToBitmap();
//Console.WriteLine(openFileDialog1.FileName);
}
}
}
}
}
jiezhi 2003-08-30
  • 打赏
  • 举报
回复
http://expert.csdn.net/Expert/topic/2199/2199100.xml?temp=.9248468
jiezhi 2003-08-30
  • 打赏
  • 举报
回复
Obtaining (and managing) file and folder icons using SHGetFileInfo in C#
http://www.codeproject.com/csharp/fileicon.asp

HOW TO: Use the SHGetFileInfo Function to Get the Icons That Are Associated with Files in Visual C# .NET
http://support.microsoft.com/default.aspx?scid=kb;en-us;319350
digitalghost 2003-08-30
  • 打赏
  • 举报
回复
还有,您上面说的内容里面我还不明白如何得到"WORD.EXE"资源库里的第N号图标,请明示!
最好能写出详细一些的在C#里的实现方法!
谢谢
digitalghost 2003-08-30
  • 打赏
  • 举报
回复
这确实是一个通过注册表查找图标的好办法,但是我想知道如何用C#得到那个文件自己带的那种图标,而不是注册表里已经注册过的信息得到的图标!
我记得VC++里面好像可以用某个API从文件本身结构里得到信息!
谢谢帮忙
ArLi2003 2003-08-30
  • 打赏
  • 举报
回复
再如 .png 文件:
HKEY_CLASSES_ROOT\.png 值为pngfile
再查
HKEY_CLASSES_ROOT\pngfile\DefaultIcon 值为shimgvw.dll,2

就说明是shimgvw.dll 的资源库第三个图标
ArLi2003 2003-08-30
  • 打赏
  • 举报
回复
注意一下windows 的file 机制

基本上可以:

1,从.doc 查HKEY_CLASSES_ROOT\.doc 值为 Word.Document.8
2,再查 HKEY_CLASSES_ROOT\Word.Document.8\DefaultIcon 值为
C:\WINDOWS\Installer\{00000804-78E1-11D2-B60F-006097C998E7}\wordicon.exe,1
2,从clsID: 00000804-78E1-11D2-B60F-006097C998E7 查到是c:\progra~1\office\word.exe,再由上面的,1 查到是word.exe 的资源库的第2个图形

如上所示,word 2000 后用的是clsid 来标志路径,如果是其它类型文件就更容易了

110,499

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧