怎样在程序中获得一个可执行文件的图标

想在程序中调用一个外部的可执行文件(比如说NotePad.exe),怎样能获得它的图标。
---------------------------------------------------------------

建立这样的一个类:
Imports System
Imports System.Drawing
Imports System.Runtime.InteropServices
Imports System.Windows.Forms
Imports System.IO

Public 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

 1<structlayout(layoutkind.sequential)> _   
 2Private Structure SHFILEINFO   
 3' pointer to icon handle   
 4Public hIcon As IntPtr   
 5' icon index   
 6Public iIcon As Integer   
 7' not used in this example   
 8Public dwAttributes As Integer   
 9' file pathname--marshal this as   
10' an unmanaged LPSTR of MAX_SIZE   
11<marshalas(unmanagedtype.lpstr, sizeconst:="260)"> _   
12Public szDisplayName As String   
13' file type--marshal as unmanaged   
14' LPSTR of 80 chars   
15<marshalas(unmanagedtype.lpstr, sizeconst:="80)"> _   
16Public szTypeName As String   
17End Structure   
18  
19Private Declare Auto Function SHGetFileInfo _   
20Lib "shell32" (ByVal pszPath As String, ByVal dwFileAttributes As Integer, ByRef psfi As SHFILEINFO, _   
21ByVal cbFileInfo As Integer, ByVal uFlags As Integer) As Integer   
22  
23Public Shared Function GetSmallIcon(ByVal fsi As FileSystemInfo) As Icon   
24Return IconExtractor.GetIcon(fsi, SHGFI_SMALLICON)   
25End Function   
26  
27Public Shared Function GetLargeIcon(ByVal fsi As FileSystemInfo) As Icon   
28Return IconExtractor.GetIcon(fsi, SHGFI_LARGEICON)   
29End Function   
30  
31Private Shared Function GetIcon(ByVal fsi As FileSystemInfo, ByVal anIconSize As Integer) As Icon   
32  
33Dim aSHFileInfo As New SHFILEINFO()   
34Dim cbFileInfo As Integer = Marshal.SizeOf(aSHFileInfo)   
35Dim uflags As Integer = SHGFI_ICON Or SHGFI_USEFILEATTRIBUTES Or anIconSize   
36Try   
37SHGetFileInfo(fsi.FullName, fsi.Attributes, aSHFileInfo, cbFileInfo, uflags)   
38Return Icon.FromHandle(aSHFileInfo.hIcon)   
39Catch ex As Exception   
40Return Nothing   
41End Try   
42End Function   
43  
44End Class   
45  
46  
47\---------------------------------------------------------------   
48  
49调用:   
50Dim fsi As System.IO.FileSystemInfo   
51fsi = New FileInfo("c:\pp.txt")   
52Dim icon1 As Icon   
53icon1 = IconExtractor.GetLargeIcon(fsi)   
54Me.Icon = icon1   
55  
56\---------------------------------------------------------------   
57  
58Declare Function ExtractAssociatedIcon Lib "shell32.dll" Alias "ExtractAssociateIconA" (ByVal hInst As Long, ByVal lpIconPath As String, lpiIcon As Long) As Long   
59说明   
60这个函数可判断一个可执行程序或DLL中是否存在图标,或是否有图标与系统注册表中指定的文件存在关联。随后,它允许我们提取出那些图标   
61返回值   
62Long,如果找到任何图标,就返回图标的句柄;否则返回零   
63参数表   
64参数 类型及说明   
65hInst Long,当前应用程序的实例句柄。也可用GetWindowWord函数取得拥有一个窗体或控件的示例的句柄   
66lpIconPath String,指定一个文件名,准备从该文件中提取图标。如果文件并非执行程序或DLL本身,但通过系统注册表与一个可执行文件关联,就用这个字串装载可执行程序的名字   
67lpiIcon Long,在其中装载图标在可执行文件中的资源标识符   
68注解   
69注意至少要把lpIconPath字串定义成MAX_PATH个字符的长度   
70  
71这是在VB6下的声明 如果在.NET下没反应 你要琢磨着把格式转换成.NET支持的</marshalas(unmanagedtype.lpstr,></marshalas(unmanagedtype.lpstr,></structlayout(layoutkind.sequential)>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus