⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 新建 文本文档.txt

📁 进程跟踪器程序代码
💻 TXT
字号:
我这里有一段代码,可获取任意文件的图标:
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);
                          }
                        }
             }
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -