[StructLayout(LayoutKind.Sequential)]
public struct SHFILEINFO
{
public IntPtr hIcon;
public int iIcon;
public int dwAttributes;
public string szDisplayName;
public string szTypeName;
}
[DllImport("shell32.dll", EntryPoint="SHGetFileInfo")]
public static extern int SHGetFileInfo (
string pszPath,
int dwFileAttributes,
ref SHFILEINFO psfi,
int cbFileInfo,
int uFlags
); public const int FILE_ATTRIBUTE_NORMAL = 0x80;
public const int SHGFI_ICON = 0x100;
public const int SHGFI_ICONLOCATION = 0x1000;
==================================
Usage:SHFILEINFO shfi = new SHFILEINFO();
SHGetFileInfo(filename,FILE_ATTRIBUTE_NORMAL,ref shfi,(int)System.Runtime.InteropServices.Marshal.SizeOf(shfi),SHGFI_ICON|SHGFI_ICONLOCATION);
/// shfi.hIcon;就是图标句柄
public struct SHFILEINFO
{
public IntPtr hIcon;
public int iIcon;
public int dwAttributes;
public string szDisplayName;
public string szTypeName;
}
[DllImport("shell32.dll", EntryPoint="SHGetFileInfo")]
public static extern int SHGetFileInfo (
string pszPath,
int dwFileAttributes,
ref SHFILEINFO psfi,
int cbFileInfo,
int uFlags
); public const int FILE_ATTRIBUTE_NORMAL = 0x80;
public const int SHGFI_ICON = 0x100;
public const int SHGFI_ICONLOCATION = 0x1000;
==================================
Usage:SHFILEINFO shfi = new SHFILEINFO();
SHGetFileInfo(filename,FILE_ATTRIBUTE_NORMAL,ref shfi,(int)System.Runtime.InteropServices.Marshal.SizeOf(shfi),SHGFI_ICON|SHGFI_ICONLOCATION);
/// shfi.hIcon;就是图标句柄
Imports System.Drawing
Imports System.Runtime.InteropServices
Imports System.Windows.Forms
Imports System.IOPublic Class IconExtractor Private Const SHGFI_SMALLICON = &H1
Private Const SHGFI_LARGEICON = &H0
Private Const SHGFI_ICON = &H100
Private Const SHGFI_USEFILEATTRIBUTES = &H10 Public Enum IconSize
SmallIcon = SHGFI_SMALLICON
LargeIcon = SHGFI_LARGEICON
End Enum <StructLayout(LayoutKind.Sequential)> _
Private Structure SHFILEINFO
' pointer to icon handle
Public hIcon As IntPtr
' icon index
Public iIcon As Integer
' not used in this example
Public dwAttributes As Integer
' file pathname--marshal this as
' an unmanaged LPSTR of MAX_SIZE
<MarshalAs(UnmanagedType.LPStr, SizeConst:=260)> _
Public szDisplayName As String
' file type--marshal as unmanaged
' LPSTR of 80 chars
<MarshalAs(UnmanagedType.LPStr, SizeConst:=80)> _
Public szTypeName As String
End Structure Private Declare Auto Function SHGetFileInfo _
Lib "shell32" (ByVal pszPath As String, ByVal dwFileAttributes As Integer, ByRef psfi As SHFILEINFO, _
ByVal cbFileInfo As Integer, ByVal uFlags As Integer) As Integer Public Shared Function GetSmallIcon(ByVal fn As String) As Icon
Return IconExtractor.GetIcon(fn, SHGFI_SMALLICON)
End Function Public Shared Function GetLargeIcon(ByVal fn As String) As Icon
Return IconExtractor.GetIcon(fn, SHGFI_LARGEICON)
End Function Private Shared Function GetIcon(ByVal fn As String, ByVal anIconSize As Integer) As Icon Dim aSHFileInfo As New SHFILEINFO()
Dim cbFileInfo As Integer = Marshal.SizeOf(aSHFileInfo)
Dim uflags As Integer = SHGFI_ICON Or SHGFI_USEFILEATTRIBUTES Or anIconSize
Try
SHGetFileInfo(fn, 0, aSHFileInfo, cbFileInfo, uflags)
Return Icon.FromHandle(aSHFileInfo.hIcon)
Catch ex As Exception
Return Nothing
End Try
End FunctionEnd Class
从句柄到Icon对象:
System.Drawing.Icon.FromHandle(hIcon)