Private Type PicBmp Size As Long tType As Long hBmp As Long hPal As Long Reserved As Long End Type
Private Type GUID Data1 As Long Data2 As Integer Data3 As Integer Data4(7) As Byte End Type
Private Declare Function OleCreatePictureIndirect _ Lib "olepro32.dll" (PicDesc As PicBmp, RefIID As GUID, _ ByVal fPictureOwnsHandle As Long, IPic As IPicture) As Long
Private Declare Function ExtractIconEx Lib "shell32.dll" _ Alias "ExtractIconExA" (ByVal lpszFile As String, ByVal _ nIconIndex As Long, phiconLarge As Long, phiconSmall As _ Long, ByVal nIcons As Long) As Long
Private Declare Function ExtractIcon Lib "shell32.dll" _ Alias "ExtractIconA" (ByVal hInst As Long, ByVal _ lpszExeFileName As String, ByVal nIconIndex As Long) As Long
Private Declare Function DestroyIcon Lib "user32" (ByVal _ hIcon As Long) As Long
Public Function GetIconFromFile(FileName As String, _ IconIndex As Long, UseLargeIcon As Boolean) As Picture
Dim hlargeicon As Long Dim hsmallicon As Long Dim selhandle As Long Dim pic As PicBmp Dim IPic As IPicture Dim IID_IDispatch As GUID
'从文件中获取图标 If ExtractIconEx(FileName, IconIndex, hlargeicon, _ hsmallicon, 1) > 0 Then
If UseLargeIcon Then selhandle = hlargeicon Else selhandle = hsmallicon End If
With IID_IDispatch .Data1 = &H20400 .Data4(0) = &HC0 .Data4(7) = &H46 End With
With pic .Size = Len(pic) ' pic结构的长度 .tType = vbPicTypeIcon ' Picture的类型 .hBmp = selhandle ' 位图句柄 End With
DestroyIcon hsmallicon DestroyIcon hlargeicon End If End Function
Private Sub Command1_Click() Dim hIcon As Long Dim sName As String
sName = "c:\win2000\explorer.exe" Set Picture1.Picture = GetIconFromFile(sName, 0, True) SavePicture Picture1, "c:\heart.ico" End Sub
把图表改成其它名字,然后在VB中用“添加自定义资源”的形式把改名后的图表放到程序的资源文件中然后在程序运行时,用读写二进制文件的方法把图标直接写到硬盘上 '假设图标已做成资源,资源类型为"MYICON",资源ID为101 Private Sub Form_Load() Const MYICONFILE = "C:\XX.ico" If Len(Dir(MYICONFILE)) = 0 Then Dim byteBuff() As Byte byteBuff = LoadResData(101, "MYICON") Dim lngFileId As Long lngFileId = FreeFile Open MYICONFILE For Binary As lngFileId Put lngFileId, , byteBuff Close lngFileId End If End Sub
Private Type PicBmp
Size As Long
tType As Long
hBmp As Long
hPal As Long
Reserved As Long
End Type
Private Type GUID
Data1 As Long
Data2 As Integer
Data3 As Integer
Data4(7) As Byte
End Type
Private Declare Function OleCreatePictureIndirect _
Lib "olepro32.dll" (PicDesc As PicBmp, RefIID As GUID, _
ByVal fPictureOwnsHandle As Long, IPic As IPicture) As Long
Private Declare Function ExtractIconEx Lib "shell32.dll" _
Alias "ExtractIconExA" (ByVal lpszFile As String, ByVal _
nIconIndex As Long, phiconLarge As Long, phiconSmall As _
Long, ByVal nIcons As Long) As Long
Private Declare Function ExtractIcon Lib "shell32.dll" _
Alias "ExtractIconA" (ByVal hInst As Long, ByVal _
lpszExeFileName As String, ByVal nIconIndex As Long) As Long
Private Declare Function DestroyIcon Lib "user32" (ByVal _
hIcon As Long) As Long
Public Function GetIconFromFile(FileName As String, _
IconIndex As Long, UseLargeIcon As Boolean) As Picture
Dim hlargeicon As Long
Dim hsmallicon As Long
Dim selhandle As Long
Dim pic As PicBmp
Dim IPic As IPicture
Dim IID_IDispatch As GUID
'从文件中获取图标
If ExtractIconEx(FileName, IconIndex, hlargeicon, _
hsmallicon, 1) > 0 Then
If UseLargeIcon Then
selhandle = hlargeicon
Else
selhandle = hsmallicon
End If
With IID_IDispatch
.Data1 = &H20400
.Data4(0) = &HC0
.Data4(7) = &H46
End With
With pic
.Size = Len(pic) ' pic结构的长度
.tType = vbPicTypeIcon ' Picture的类型
.hBmp = selhandle ' 位图句柄
End With
' 建立图片对象
Call OleCreatePictureIndirect(pic, IID_IDispatch, 1, IPic)
Set GetIconFromFile = IPic
DestroyIcon hsmallicon
DestroyIcon hlargeicon
End If
End Function
Private Sub Command1_Click()
Dim hIcon As Long
Dim sName As String
sName = "c:\win2000\explorer.exe"
Set Picture1.Picture = GetIconFromFile(sName, 0, True)
SavePicture Picture1, "c:\heart.ico"
End Sub
'假设图标已做成资源,资源类型为"MYICON",资源ID为101
Private Sub Form_Load()
Const MYICONFILE = "C:\XX.ico"
If Len(Dir(MYICONFILE)) = 0 Then
Dim byteBuff() As Byte
byteBuff = LoadResData(101, "MYICON")
Dim lngFileId As Long
lngFileId = FreeFile
Open MYICONFILE For Binary As lngFileId
Put lngFileId, , byteBuff
Close lngFileId
End If
End Sub