Lm th? no ? s? d?ng ch?c nng SHGetFileInfo ? c ?c cc bi?u t?ng lin k?t v?i cc t?p tin trong Visual C#.NET

D?ch tiu ? D?ch tiu ?
ID c?a bi: 319350 - Xem s?n ph?m m bi ny p d?ng vo.
Bung t?t c? | Thu g?n t?t c?

? Trang ny

TM T?T

Bi t?ng b?c ny m t? cch s? d?ng cc SHGetFileInfo ch?c nng ? c ?c cc bi?u t?ng lin k?t v?i cc t?p tin.

T?o m?t ?ng d?ng Windows Forms

  1. B?t ?u Microsoft Visual Studio.NET.
  2. Trn cc Tp tr?nh n, i?m ?n M?i, sau b?m D an.
  3. Trong cc D? n m?i h?p tho?i h?p, b?m vo Visual C# cc d? n d?i Cc lo?i d? n, sau b?m ?ng d?ng Windows d?i Khun mu.
  4. Trong cc Tn h?p, lo?i GetIconSample.

S? d?ng ch?c nng SHGetFileInfo

  1. Thm m? sau trong t?p tin Form1.cs ? ph?n cu?i c?a cc B?NG CCH S? D?NG pht bi?u.
    using System.Runtime.InteropServices;
    					
  2. Thm m? sau y sau khi cc Form1 l?p h?c c?a cc GetIconSample khng gian tn.
    [StructLayout(LayoutKind.Sequential)]
    public struct SHFILEINFO 
    {
    	public IntPtr hIcon;
    	public IntPtr iIcon;
    	public uint dwAttributes;
    	[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
    	public string szDisplayName;
    	[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
    	public string szTypeName;
    };
    
    class Win32
    {
    	public const uint SHGFI_ICON = 0x100;
    	public const uint SHGFI_LARGEICON = 0x0; // 'Large icon
    	public const uint SHGFI_SMALLICON = 0x1; // 'Small icon
    		
    	[DllImport("shell32.dll")]
    	public static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbSizeFileInfo, uint uFlags);
    }
    					
  3. Thm m? sau trong cc Form1 l?p sau khi cc T NHN bo co:
    private int nIndex = 0;
    					
  4. Thm m?t listView ki?m sot, m?t nt ki?m sot, v m?t imageList ki?m sot m?u. Tn m?c ?nh l listView1, button1 v imageList1 tng ?ng.
  5. Trong c?a s? thu?c tnh c?a button1, thi?t l?p cc vn b?n nt ? Ch?n m?t t?p tin, v sau thm m? sau y trong cc button1_click s? ki?n:
    IntPtr hImgSmall; //the handle to the system image list
    IntPtr hImgLarge; //the handle to the system image list
    string fName; //  'the file name to get icon from
    SHFILEINFO shinfo = new SHFILEINFO();
       
    OpenFileDialog openFileDialog1 = new OpenFileDialog();
    openFileDialog1.InitialDirectory = "c:\\temp\\";
    openFileDialog1.Filter = "All files (*.*)|*.*";
    openFileDialog1.FilterIndex = 2;
    openFileDialog1.RestoreDirectory = true ;
    
    listView1.SmallImageList = imageList1;
    listView1.LargeImageList = imageList1;
    			
    if(openFileDialog1.ShowDialog() == DialogResult.OK) 
    {
         fName = openFileDialog1.FileName;
         //Use this to get the small Icon
         hImgSmall = Win32.SHGetFileInfo(fName, 0, ref shinfo,(uint)Marshal.SizeOf(shinfo),Win32.SHGFI_ICON |Win32.SHGFI_SMALLICON);
    
        //Use this to get the large Icon
        //hImgLarge = SHGetFileInfo(fName, 0, 
        //	ref shinfo, (uint)Marshal.SizeOf(shinfo), 
        //	Win32.SHGFI_ICON | Win32.SHGFI_LARGEICON);
    
        //The icon is returned in the hIcon member of the shinfo struct
        System.Drawing.Icon myIcon = System.Drawing.Icon.FromHandle(shinfo.hIcon);
    				
        imageList1.Images.Add(myIcon);
    				
        //Add file name and icon to listview
        listView1.Items.Add(fName, nIndex++); 
    }
    					

Ch?y cc d? n

  1. Bin d?ch d? n: trn cc Xy d?ng tr?nh n, nh?p vo Xy d?ng gi?i php.
  2. Nh?n F5 ? ch?y cc d? n.
  3. Nh?p vo Ch?n m?t t?p tin, v sau ch?n m?t t?p tin trong cc M? h?p tho?i. Tn c?a t?p v bi?u t?ng l lin k?t v?i cc t?p tin xu?t hi?n trong cc ListView ki?m sot.

THAM KH?O

? bi?t thm thng tin trong m?t Microsoft Visual Basic.NET Phin b?n c?a bi vi?t ny, nh?p vo s? bi vi?t sau y ? xem bi vi?t trong c s? ki?n th?c Microsoft:
319340Lm th? no ? s? d?ng ch?c nng SHGetFileInfo ? c ?c cc bi?u t?ng c lin quan ?n tc ph?m trong Visual Basic.NET

Thu?c tnh

ID c?a bi: 319350 - L?n xem xt sau cng: 27 Thang Tam 2011 - Xem xt l?i: 2.0
p d?ng
  • Microsoft Visual Studio .NET 2003 Professional Edition
  • Microsoft Visual Studio .NET 2003 Enterprise Architect
  • Microsoft Visual Studio .NET 2003 Enterprise Developer
  • Microsoft Visual Studio .NET 2003 Academic Edition
  • Microsoft Visual Studio .NET 2002 Professional Edition
  • Microsoft Visual Studio .NET 2002 Enterprise Architect
  • Microsoft Visual Studio .NET 2002 Enterprise Developer
  • Microsoft Visual Studio .NET 2002 Academic Edition
T? kha:
kbhowtomaster kbmt KB319350 KbMtvi
My d?ch
QUAN TRONG: Bi vi?t ny ?c d?ch b?ng ph?n m?m d?ch my c?a Microsoft ch? khng ph?i do con ng?i d?ch. Microsoft cung c?p cc bi vi?t do con ng?i d?ch v c? cc bi vi?t do my d?ch ? b?n c th? truy c?p vo t?t c? cc bi vi?t trong C s? Ki?n th?c c?a chng ti b?ng ngn ng? c?a b?n. Tuy nhin, bi vi?t do my d?ch khng ph?i lc no c?ng hon h?o. Lo?i bi vi?t ny c th? ch?a cc sai st v? t? v?ng, c php ho?c ng? php, gi?ng nh m?t ng?i n?c ngoi c th? m?c sai st khi ni ngn ng? c?a b?n. Microsoft khng ch?u trch nhi?m v? b?t k? s? thi?u chnh xc, sai st ho?c thi?t h?i no do vi?c d?ch sai n?i dung ho?c do ho?t ?ng s? d?ng c?a khch hng gy ra. Microsoft c?ng th?ng xuyn c?p nh?t ph?n m?m d?ch my ny.
Nh?p chu?t vo y ? xem b?n ti?ng Anh c?a bi vi?t ny:319350

Cung cp Phan hi

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com