'Example Name:Retrieving an Associated 16x16 Icon Using SHGetFileInfo '------------------------------------------------------------------------------ ' ' BAS Moduel Code ' '------------------------------------------------------------------------------ Option ExplicitPublic Const MAX_PATH = 260 Public Const SHGFI_DISPLAYNAME = &H200 Public Const SHGFI_EXETYPE = &H2000 Public Const SHGFI_SYSICONINDEX = &H4000 'system icon index Public Const SHGFI_LARGEICON = &H0 'large icon Public Const SHGFI_SMALLICON = &H1 'small icon Public Const SHGFI_SHELLICONSIZE = &H4 Public Const SHGFI_TYPENAME = &H400 Public Const ILD_TRANSPARENT = &H1 'display transparent Public Const BASIC_SHGFI_FLAGS = SHGFI_TYPENAME Or _ SHGFI_SHELLICONSIZE Or _ SHGFI_SYSICONINDEX Or _ SHGFI_DISPLAYNAME Or _ SHGFI_EXETYPEPublic Type SHFILEINFO hIcon As Long iIcon As Long dwAttributes As Long szDisplayName As String * MAX_PATH szTypeName As String * 80 End TypePublic Declare Function SHGetFileInfo Lib "shell32" _ Alias "SHGetFileInfoA" _ (ByVal pszPath As String, _ ByVal dwFileAttributes As Long, _ psfi As SHFILEINFO, _ ByVal cbSizeFileInfo As Long, _ ByVal uFlags As Long) As LongPublic Declare Function ImageList_Draw Lib "comctl32" _ (ByVal himl As Long, ByVal i As Long, _ ByVal hDCDest As Long, ByVal x As Long, _ ByVal y As Long, ByVal flags As Long) As LongPublic shinfo As SHFILEINFO '--end block--' '------------------------------------------------------------------------------ ' ' Form Code ' '------------------------------------------------------------------------------ Option ExplicitPrivate Sub Form_Load()
'centre the form on the screen Me.Move (Screen.Width - Me.Width) / 2, (Screen.Height - Me.Height) / 2End Sub Private Sub Command2_Click() Unload MeEnd Sub Private Sub Command1_Click()
'working variables... Dim hImgSmall as Long 'the handle to the system image list Dim fName As String 'the file name to get icon from Dim fnFilter As String 'the file name filter Dim r As Long
'a little error handling to trap a cancel On Local Error GoTo Command1ErrorHandler
'get the file from the user fnFilter$ = "All Files (*.*)|*.*|" fnFilter$ = fnFilter$ & "Applications (*.exe)|*.exe|" fnFilter$ = fnFilter$ & "Windows Bitmap (*.bmp)|*.bmp|" fnFilter$ = fnFilter$ & "Icon Files (*.ico)|*.ico" CommonDlg1.CancelError = True CommonDlg1.Filter = fnFilter$ CommonDlg1.ShowOpen fName = CommonDlg1.FileName
'get the system icon associated with that file hImgSmall& = SHGetFileInfo(fName, 0&, _ shinfo, Len(shinfo), _ BASIC_SHGFI_FLAGS Or SHGFI_SMALLICON)
'set a picture box to receive the small icon 'its size must be 16x16 pixels (240x240 twips), 'with no 3d or border! 'clear any existing image pixSmall.Picture = LoadPicture() pixSmall.AutoRedraw = True
'draw the associated icon into the picturebox Call ImageList_Draw(hImgSmall&, shinfo.iIcon, _ pixSmall.hDC, 0, 0, ILD_TRANSPARENT)
'realize the image by assigning its image property '(where the icon was drawn) to the actual picture property pixSmall.Picture = pixSmall.Image 'Uncomment out the following code to save 'the image to the current path. Note that 'the background colour of the icon saved 'will be the background colour of the 'pixSmall control. 'SavePicture pixSmall, "test.bmp"Exit SubCommand1ErrorHandler: Exit SubEnd Sub fox api的例程
模块 '模块 Option Explicit'通过文件扩展名获取系统关联的图标Private Const SHGFI_LARGEICON = &H0 Private Const SHGFI_SMALLICON = &H1 Private Const SHGFI_SHELLICONSIZE = &H4 Private Const SHGFI_USEFILEATTRIBUTES = &H10 Private Const SHGFI_DISPLAYNAME = &H200 Private Const SHGFI_TYPENAME = &H400 Private Const SHGFI_EXETYPE = &H2000 Private Const SHGFI_SYSICONINDEX = &H4000Private Const BASIC_SHGFI_FLAGS = SHGFI_TYPENAME _ Or SHGFI_SHELLICONSIZE Or SHGFI_SYSICONINDEX _ Or SHGFI_DISPLAYNAME Or SHGFI_EXETYPE _ Or SHGFI_USEFILEATTRIBUTESPrivate Const MAX_PATH = 260 Private Const ILD_TRANSPARENT = &H1Private Type SHFILEINFO hIcon As Long iIcon As Long dwAttributes As Long szDisplayName As String * MAX_PATH szTypeName As String * 80 End TypePrivate Declare Function SHGetFileInfo Lib "shell32.dll" Alias "SHGetFileInfoA" (ByVal pszPath As String, ByVal dwFileAttributes As Long, psfi As SHFILEINFO, ByVal cbSizeFileInfo As Long, ByVal uFlags As Long) As Long Private Declare Function ImageList_Draw Lib "comctl32.dll" (ByVal himl As Long, ByVal i As Long, ByVal hDCDest As Long, ByVal x As Long, ByVal y As Long, ByVal flags As Long) As LongPublic Sub GetFileTypeIcon(FileName As String, DisplayObject As Object) '在调用此过程前要确定 DisplayObject 所引用的对象具有以下四项属性, '它们是 AutoRedraw 属性、hDC 属性、Image 属性和 Picture 属性。 '比如 Form、PictureBox 等都具有以上四项属性。 Dim shInfo As SHFILEINFO Dim Ret As Long Dim hImgLarge As Long Dim strFileName As String, strFileType As String hImgLarge& = SHGetFileInfo(FileName$, 0&, _ shInfo, Len(shInfo), _ BASIC_SHGFI_FLAGS Or SHGFI_LARGEICON) strFileName = Left$(shInfo.szDisplayName, _ InStr(shInfo.szDisplayName, Chr$(0)) - 1) strFileType = Left$(shInfo.szTypeName, _ InStr(shInfo.szTypeName, Chr$(0)) - 1)
Debug.Print strFileName; Tab; strFileType DisplayObject.Picture = LoadPicture() DisplayObject.AutoRedraw = True Ret = ImageList_Draw(hImgLarge&, shInfo.iIcon, DisplayObject.hDC, 0, 0, ILD_TRANSPARENT) Set DisplayObject.Picture = DisplayObject.Image End Sub窗体:在窗体中添加一个 TextBox 和一个 CommandButton '窗体 Option ExplicitPrivate Sub Form_Load() Text1.Text = "*.txt" End SubPrivate Sub Command1_Click() GetFileTypeIcon Text1.Text, Me End Sub 上个图 程序执行后的效果
*****************************************************************************
欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码)
http://feiyun0112.cnblogs.com/
'
' BAS Moduel Code
'
'------------------------------------------------------------------------------
Option ExplicitPublic Const MAX_PATH = 260
Public Const SHGFI_DISPLAYNAME = &H200
Public Const SHGFI_EXETYPE = &H2000
Public Const SHGFI_SYSICONINDEX = &H4000 'system icon index
Public Const SHGFI_LARGEICON = &H0 'large icon
Public Const SHGFI_SMALLICON = &H1 'small icon
Public Const SHGFI_SHELLICONSIZE = &H4
Public Const SHGFI_TYPENAME = &H400
Public Const ILD_TRANSPARENT = &H1 'display transparent
Public Const BASIC_SHGFI_FLAGS = SHGFI_TYPENAME Or _
SHGFI_SHELLICONSIZE Or _
SHGFI_SYSICONINDEX Or _
SHGFI_DISPLAYNAME Or _
SHGFI_EXETYPEPublic Type SHFILEINFO
hIcon As Long
iIcon As Long
dwAttributes As Long
szDisplayName As String * MAX_PATH
szTypeName As String * 80
End TypePublic Declare Function SHGetFileInfo Lib "shell32" _
Alias "SHGetFileInfoA" _
(ByVal pszPath As String, _
ByVal dwFileAttributes As Long, _
psfi As SHFILEINFO, _
ByVal cbSizeFileInfo As Long, _
ByVal uFlags As Long) As LongPublic Declare Function ImageList_Draw Lib "comctl32" _
(ByVal himl As Long, ByVal i As Long, _
ByVal hDCDest As Long, ByVal x As Long, _
ByVal y As Long, ByVal flags As Long) As LongPublic shinfo As SHFILEINFO
'--end block--'
'------------------------------------------------------------------------------
'
' Form Code
'
'------------------------------------------------------------------------------
Option ExplicitPrivate Sub Form_Load()
'centre the form on the screen
Me.Move (Screen.Width - Me.Width) / 2, (Screen.Height - Me.Height) / 2End Sub
Private Sub Command2_Click() Unload MeEnd Sub
Private Sub Command1_Click()
'working variables...
Dim hImgSmall as Long 'the handle to the system image list
Dim fName As String 'the file name to get icon from
Dim fnFilter As String 'the file name filter
Dim r As Long
'a little error handling to trap a cancel
On Local Error GoTo Command1ErrorHandler
'get the file from the user
fnFilter$ = "All Files (*.*)|*.*|"
fnFilter$ = fnFilter$ & "Applications (*.exe)|*.exe|"
fnFilter$ = fnFilter$ & "Windows Bitmap (*.bmp)|*.bmp|"
fnFilter$ = fnFilter$ & "Icon Files (*.ico)|*.ico" CommonDlg1.CancelError = True
CommonDlg1.Filter = fnFilter$
CommonDlg1.ShowOpen fName = CommonDlg1.FileName
'get the system icon associated with that file
hImgSmall& = SHGetFileInfo(fName, 0&, _
shinfo, Len(shinfo), _
BASIC_SHGFI_FLAGS Or SHGFI_SMALLICON)
'set a picture box to receive the small icon
'its size must be 16x16 pixels (240x240 twips),
'with no 3d or border!
'clear any existing image
pixSmall.Picture = LoadPicture()
pixSmall.AutoRedraw = True
'draw the associated icon into the picturebox
Call ImageList_Draw(hImgSmall&, shinfo.iIcon, _
pixSmall.hDC, 0, 0, ILD_TRANSPARENT)
'realize the image by assigning its image property
'(where the icon was drawn) to the actual picture property
pixSmall.Picture = pixSmall.Image 'Uncomment out the following code to save
'the image to the current path. Note that
'the background colour of the icon saved
'will be the background colour of the
'pixSmall control.
'SavePicture pixSmall, "test.bmp"Exit SubCommand1ErrorHandler:
Exit SubEnd Sub
fox api的例程
'模块
Option Explicit'通过文件扩展名获取系统关联的图标Private Const SHGFI_LARGEICON = &H0
Private Const SHGFI_SMALLICON = &H1
Private Const SHGFI_SHELLICONSIZE = &H4
Private Const SHGFI_USEFILEATTRIBUTES = &H10
Private Const SHGFI_DISPLAYNAME = &H200
Private Const SHGFI_TYPENAME = &H400
Private Const SHGFI_EXETYPE = &H2000
Private Const SHGFI_SYSICONINDEX = &H4000Private Const BASIC_SHGFI_FLAGS = SHGFI_TYPENAME _
Or SHGFI_SHELLICONSIZE Or SHGFI_SYSICONINDEX _
Or SHGFI_DISPLAYNAME Or SHGFI_EXETYPE _
Or SHGFI_USEFILEATTRIBUTESPrivate Const MAX_PATH = 260
Private Const ILD_TRANSPARENT = &H1Private Type SHFILEINFO
hIcon As Long
iIcon As Long
dwAttributes As Long
szDisplayName As String * MAX_PATH
szTypeName As String * 80
End TypePrivate Declare Function SHGetFileInfo Lib "shell32.dll" Alias "SHGetFileInfoA" (ByVal pszPath As String, ByVal dwFileAttributes As Long, psfi As SHFILEINFO, ByVal cbSizeFileInfo As Long, ByVal uFlags As Long) As Long
Private Declare Function ImageList_Draw Lib "comctl32.dll" (ByVal himl As Long, ByVal i As Long, ByVal hDCDest As Long, ByVal x As Long, ByVal y As Long, ByVal flags As Long) As LongPublic Sub GetFileTypeIcon(FileName As String, DisplayObject As Object)
'在调用此过程前要确定 DisplayObject 所引用的对象具有以下四项属性,
'它们是 AutoRedraw 属性、hDC 属性、Image 属性和 Picture 属性。
'比如 Form、PictureBox 等都具有以上四项属性。 Dim shInfo As SHFILEINFO
Dim Ret As Long
Dim hImgLarge As Long
Dim strFileName As String, strFileType As String hImgLarge& = SHGetFileInfo(FileName$, 0&, _
shInfo, Len(shInfo), _
BASIC_SHGFI_FLAGS Or SHGFI_LARGEICON) strFileName = Left$(shInfo.szDisplayName, _
InStr(shInfo.szDisplayName, Chr$(0)) - 1) strFileType = Left$(shInfo.szTypeName, _
InStr(shInfo.szTypeName, Chr$(0)) - 1)
Debug.Print strFileName; Tab; strFileType DisplayObject.Picture = LoadPicture()
DisplayObject.AutoRedraw = True Ret = ImageList_Draw(hImgLarge&, shInfo.iIcon, DisplayObject.hDC, 0, 0, ILD_TRANSPARENT) Set DisplayObject.Picture = DisplayObject.Image
End Sub窗体:在窗体中添加一个 TextBox 和一个 CommandButton
'窗体
Option ExplicitPrivate Sub Form_Load()
Text1.Text = "*.txt"
End SubPrivate Sub Command1_Click()
GetFileTypeIcon Text1.Text, Me
End Sub
上个图
程序执行后的效果