我这里有个例子,获取任意文件的图标: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);
}
}
}
}
}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.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);
}
}
}
}
}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);
}
}
}
解决方案 »
- C# List列表中添加 socket
- 问一下各位大大哦!socket。帮我解决下,上图了!
- 小菜求助高手
- 超棘手问题...线程mscorwks.dll!ReleaseFusionInterface超耗CPU...
- 散分(http://community.csdn.net/Expert/topic/5152/5152739.xml?temp=.5267145)
- 一个winform的DataGrid问题
- 有大神帮忙看看正则表达是的问题
- 更新了DataGridView的单元格,与之绑定的DataTable的单元格也更新了,可是RowState没有更新
- socket客户端断开重连后不能与服务器通信
- 請問那位大哥知道Visual Studio .NET 繁體版那有下載嗎?
- 改变用户输入的字母.
- 请教如何使每个窗体来访问一个全局变量
www.codeproject.com
www,csharpcorner.com
上有很多示例可以参考,都还是做的不错的,有兴趣可以去看看
这个例子可以解决你的问题,有代码和演示程序下载.http://www.codeproject.com/cs/miscctrl/ExplorerTree.asp