'下面一个例子,在窗体上放一个DriveList、一个DirList、一个FileList、一个TextBox、两个Label,两个PictrueBoxOption ExplicitPrivate Type TypeIcon cbSize As Long picType As PictureTypeConstants hIcon As Long End TypePrivate Type CLSID id(16) As Byte End TypePrivate Const MAX_PATH = 260 Private Type SHFILEINFO hIcon As Long ' out: icon iIcon As Long ' out: icon index dwAttributes As Long ' out: SFGAO_ flags szDisplayName As String * MAX_PATH ' out: display name (or path) szTypeName As String * 80 ' out: type name End TypePrivate Declare Function OleCreatePictureIndirect Lib "oleaut32.dll" (pDicDesc As TypeIcon, riid As CLSID, ByVal fown As Long, lpUnk As Object) As Long Private Declare Function SHGetFileInfo Lib "shell32.dll" Alias "SHGetFileInfoA" (ByVal pszPath As String, ByVal dwFileAttributes As Long, psfi As SHFILEINFO, ByVal cbFileInfo As Long, ByVal uFlags As Long) As LongPrivate Const SHGFI_ICON = &H100 Private Const SHGFI_LARGEICON = &H0 Private Const SHGFI_SMALLICON = &H1' Convert an icon handle into an IPictureDisp. Private Function IconToPicture(hIcon As Long) As IPictureDisp Dim cls_id As CLSID Dim hRes As Long Dim new_icon As TypeIcon Dim lpUnk As IUnknown With new_icon .cbSize = Len(new_icon) .picType = vbPicTypeIcon .hIcon = hIcon End With With cls_id .id(8) = &HC0 .id(15) = &H46 End With hRes = OleCreatePictureIndirect(new_icon, _ cls_id, 1, lpUnk) If hRes = 0 Then Set IconToPicture = lpUnk End FunctionPrivate Function GetIcon(filename As String, icon_size As Long) As IPictureDisp Dim index As Integer Dim hIcon As Long Dim item_num As Long Dim icon_pic As IPictureDisp Dim sh_info As SHFILEINFO SHGetFileInfo filename, 0, sh_info, _ Len(sh_info), SHGFI_ICON + icon_size hIcon = sh_info.hIcon Set icon_pic = IconToPicture(hIcon) Set GetIcon = icon_pic End FunctionPrivate Sub DirList_Change() FileList.Path = DirList.Path End Sub Private Sub DriveList_Change() On Error GoTo DriveError DirList.Path = DriveList.Drive Exit SubDriveError: DriveList.Drive = DirList.Path Exit Sub End SubPrivate Sub FileList_Click() Dim fname As String On Error GoTo LoadPictureError fname = FileList.Path + "\" + FileList.filename Caption = "ShowIcons [" & fname & "]"
Exit SubLoadPictureError: Beep Caption = "ShowIcons [Invalid picture]" Exit Sub End SubPrivate Sub Form_Resize() Dim wid As Integer Dim hgt As Integer If WindowState = vbMinimized Then Exit Sub PatternText.Move _ 0, ScaleHeight - PatternText.Height hgt = (PatternText.Top - DriveList.Top - _ DriveList.Height) / 2 If hgt < 10 Then hgt = 10 wid = DriveList.Width DirList.Move 0, DriveList.Top + _ DriveList.Height, wid, hgt FileList.Move 0, DirList.Top + _ DirList.Height, wid, hgt End Sub Private Sub PatternText_Change() FileList.Pattern = PatternText.Text End Sub
cbSize As Long
picType As PictureTypeConstants
hIcon As Long
End TypePrivate Type CLSID
id(16) As Byte
End TypePrivate Const MAX_PATH = 260
Private Type SHFILEINFO
hIcon As Long ' out: icon
iIcon As Long ' out: icon index
dwAttributes As Long ' out: SFGAO_ flags
szDisplayName As String * MAX_PATH ' out: display name (or path)
szTypeName As String * 80 ' out: type name
End TypePrivate Declare Function OleCreatePictureIndirect Lib "oleaut32.dll" (pDicDesc As TypeIcon, riid As CLSID, ByVal fown As Long, lpUnk As Object) As Long
Private Declare Function SHGetFileInfo Lib "shell32.dll" Alias "SHGetFileInfoA" (ByVal pszPath As String, ByVal dwFileAttributes As Long, psfi As SHFILEINFO, ByVal cbFileInfo As Long, ByVal uFlags As Long) As LongPrivate Const SHGFI_ICON = &H100
Private Const SHGFI_LARGEICON = &H0
Private Const SHGFI_SMALLICON = &H1' Convert an icon handle into an IPictureDisp.
Private Function IconToPicture(hIcon As Long) As IPictureDisp
Dim cls_id As CLSID
Dim hRes As Long
Dim new_icon As TypeIcon
Dim lpUnk As IUnknown With new_icon
.cbSize = Len(new_icon)
.picType = vbPicTypeIcon
.hIcon = hIcon
End With
With cls_id
.id(8) = &HC0
.id(15) = &H46
End With
hRes = OleCreatePictureIndirect(new_icon, _
cls_id, 1, lpUnk)
If hRes = 0 Then Set IconToPicture = lpUnk
End FunctionPrivate Function GetIcon(filename As String, icon_size As Long) As IPictureDisp
Dim index As Integer
Dim hIcon As Long
Dim item_num As Long
Dim icon_pic As IPictureDisp
Dim sh_info As SHFILEINFO SHGetFileInfo filename, 0, sh_info, _
Len(sh_info), SHGFI_ICON + icon_size
hIcon = sh_info.hIcon
Set icon_pic = IconToPicture(hIcon)
Set GetIcon = icon_pic
End FunctionPrivate Sub DirList_Change()
FileList.Path = DirList.Path
End Sub
Private Sub DriveList_Change()
On Error GoTo DriveError
DirList.Path = DriveList.Drive
Exit SubDriveError:
DriveList.Drive = DirList.Path
Exit Sub
End SubPrivate Sub FileList_Click()
Dim fname As String On Error GoTo LoadPictureError fname = FileList.Path + "\" + FileList.filename
Caption = "ShowIcons [" & fname & "]"
SmallIconPicture.Picture = _
GetIcon(fname, SHGFI_SMALLICON)
SmallIconLabel.Caption = _
Format$(SmallIconPicture.ScaleWidth) & _
"x" & _
Format$(SmallIconPicture.ScaleHeight) LargeIconPicture.Picture = _
GetIcon(fname, SHGFI_LARGEICON)
LargeIconLabel.Caption = _
Format$(LargeIconPicture.ScaleWidth) & _
"x" & _
Format$(LargeIconPicture.ScaleHeight)
Exit SubLoadPictureError:
Beep
Caption = "ShowIcons [Invalid picture]"
Exit Sub
End SubPrivate Sub Form_Resize()
Dim wid As Integer
Dim hgt As Integer If WindowState = vbMinimized Then Exit Sub PatternText.Move _
0, ScaleHeight - PatternText.Height hgt = (PatternText.Top - DriveList.Top - _
DriveList.Height) / 2
If hgt < 10 Then hgt = 10
wid = DriveList.Width
DirList.Move 0, DriveList.Top + _
DriveList.Height, wid, hgt
FileList.Move 0, DirList.Top + _
DirList.Height, wid, hgt
End Sub
Private Sub PatternText_Change()
FileList.Pattern = PatternText.Text
End Sub