分类

链接

2011 年 6 月
 12345
6789101112
13141516171819
20212223242526
27282930  

近期文章

热门标签

新人福利,免费薅羊毛

现在位置:    首页 > .NET > 正文
共享办公室出租
C#获取软件图标
.NET 暂无评论 阅读(1,947)
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Windows.Forms;
  5. using System.Runtime.InteropServices;
  6. using System.IO;
  7. namespace ShowIcon
  8. {
  9.             [StructLayout( LayoutKind.Sequential)]
  10.         public struct FileInfomation 
  11.         { 
  12.                public IntPtr hIcon; 
  13.             public int iIcon; 
  14.             public int dwAttributes; 
  15.  
  16.             [ MarshalAs( UnmanagedType.ByValTStr, SizeConst = 260 )] 
  17.             public string szDisplayName; 
  18.  
  19.             [ MarshalAs( UnmanagedType.ByValTStr, SizeConst = 80 )] 
  20.             public string szTypeName; 
  21.         }
  22.  
  23.     /// <summary>
  24.     /// Description of MainForm.
  25.     /// </summary>
  26.     public partial class MainForm
  27.     {
  28.         [STAThread]
  29.         public static void Main(string[] args)
  30.         {
  31.             Application.EnableVisualStyles();
  32.             Application.SetCompatibleTextRenderingDefault(false);
  33.             Application.Run(new MainForm());
  34.         }
  35.         
  36.  
  37.         [DllImport("shell32.dll", EntryPoint="SHGetFileInfo")] 
  38.         public static extern int GetFileInfo(string pszPath, int dwFileAttributes, ref FileInfomation psfi, int cbFileInfo,int uFlags);
  39.         public MainForm()
  40.         {
  41.             //
  42.             // The InitializeComponent() call is required for Windows Forms designer support.
  43.             //
  44.             InitializeComponent();
  45.             
  46.             //
  47.             // TODO: Add constructor code after the InitializeComponent() call.
  48.             //
  49.         }
  50.         public static Icon GetSmallIcon(string path) 
  51.         { 
  52.          FileInfomation _info = new FileInfomation(); 
  53.  
  54.          GetFileInfo(path, 0, ref _info, Marshal.SizeOf(_info), (int)(0x000000100| 0x000000001)); 
  55.          try 
  56.           { 
  57.           return Icon.FromHandle(_info.hIcon); 
  58.           } 
  59.           catch 
  60.           { 
  61.           return null; 
  62.           } 
  63.         }
  64.         void Button1Click(object sender, System.EventArgs e)
  65.         {
  66.             this.openFileDialog1.ShowDialog();
  67.         }
  68.         
  69.         void OpenFileDialog1FileOk(object sender, System.ComponentModel.CancelEventArgs e)
  70.         {
  71.             this.textBox1.Text=this.openFileDialog1.FileName;
  72.             this.pictureBox1.Image=GetSmallIcon(this.openFileDialog1.FileName).ToBitmap();
  73.         }
  74.     }
  75.     
  76. }

============ 欢迎各位老板打赏~ ===========

本文版权归Bruce's Blog所有,转载引用请完整注明以下信息:
本文作者:Bruce
本文地址:C#获取软件图标 | Bruce's Blog

发表评论

留言无头像?