2011-07-29 32 views

Trả lời

10

Bạn có thể trích xuất các biểu tượng từ một DLL với mã này:

public class IconExtractor 
{ 

    public static Icon Extract(string file, int number, bool largeIcon) 
    { 
     IntPtr large; 
     IntPtr small; 
     ExtractIconEx(file, number, out large, out small, 1); 
     try 
     { 
      return Icon.FromHandle(largeIcon ? large : small); 
     } 
     catch 
     { 
      return null; 
     } 

    } 
    [DllImport("Shell32.dll", EntryPoint = "ExtractIconExW", CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)] 
    private static extern int ExtractIconEx(string sFile, int iIndex, out IntPtr piLargeVersion, out IntPtr piSmallVersion, int amountIcons); 

} 

... 

form.Icon = IconExtractor.Extract("shell32.dll", 42, true); 

Tất nhiên bạn cần phải biết chỉ số của hình ảnh trong các DLL ...

1

Một số trong số họ có sẵn trong %Program Files%\Microsoft Visual Studio 10.0\Common7\VS2010ImageLibrary - cho người khác, bạn sẽ cần phải nói chuyện với luật sư của Microsoft về việc cấp phép cho họ để phân phối lại trong ứng dụng của bạn

3

This thread trên các diễn đàn phát triển MSDN cung cấp một giải pháp:

Cách điển hình để thực hiện các điều này trong .NET là sử dụng đồ họa được cung cấp trong tệp ZIP có tại C: \ Program Files \ Microsoft Visual Studio X \ Common7 \ VS200XImageLibrary.

Bạn không nêu phiên bản Visual Studio nào bạn đã cài đặt nhưng bạn cần phải thay thế "200X" bằng số phiên bản của mình.

0

Xem mã này. Nó sẽ trợ giúp

public class ExtractIcon 
{ 
    [DllImport("Shell32.dll")] 
    private static extern int SHGetFileInfo(
     string pszPath, uint dwFileAttributes, 
     out SHFILEINFO psfi, uint cbfileInfo, 
     SHGFI uFlags); 

    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; 
     public string szDisplayName; 
     public string szTypeName; 
    }; 

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

    public static Icon GetIcon(string strPath, bool bSmall, bool bOpen) 
    { 
     SHFILEINFO info = new SHFILEINFO(true); 
     int cbFileInfo = Marshal.SizeOf(info); 
     SHGFI flags; 

     if (bSmall) 
      flags = SHGFI.Icon | SHGFI.SmallIcon; 
     else 
      flags = SHGFI.Icon | SHGFI.LargeIcon; 

     if (bOpen) flags = flags | SHGFI.OpenIcon; 

     SHGetFileInfo(strPath, 0, out info, (uint)cbFileInfo, flags); 

     return Icon.FromHandle(info.hIcon); 
    } 
} 
Các vấn đề liên quan