mwc
Mar 15th, 2001, 11:51 AM
Hi,
I'm trying to get an icon associated with a txt file, but whenever I change the icon for the txt file (control panel/folder options/file types) to see the change in my program I have to restart it.
Is it possible to get the changed icon without restarting my program ?
Here's the code i'm talking about
Add Picture1 and Command1
Const MAX_PATH2 = 260
Const SHGFI_DISPLAYNAME = &H200
Const SHGFI_EXETYPE = &H2000
Const SHGFI_SYSICONINDEX = &H4000 ' System icon index
Const SHGFI_LARGEICON = &H0 ' Large icon
Const SHGFI_SMALLICON = &H1 ' Small icon
Const ILD_TRANSPARENT = &H1 ' Display transparent
Const SHGFI_SHELLICONSIZE = &H4
Const SHGFI_TYPENAME = &H400
Const BASIC_SHGFI_FLAGS = SHGFI_TYPENAME _
Or SHGFI_SHELLICONSIZE Or SHGFI_SYSICONINDEX _
Or SHGFI_DISPLAYNAME Or SHGFI_EXETYPE
Private Type SHFILEINFO
hIcon As Long
iIcon As Long
dwAttributes As Long
szDisplayName As String * MAX_PATH2
szTypeName As String * 80
End Type
Private 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&, ByVal i&, ByVal hDCDest&, _
ByVal x&, ByVal y&, ByVal flags&) As Long
Function GetIconFromFile(filepath As String, Pic As PictureBox, LargeIcon As Boolean)
Dim hImg As Long
Dim FileP As String
Dim r As Long
Dim shinfo As SHFILEINFO
FileP$ = filepath
If LargeIcon Then
hImg& = SHGetFileInfo(FileP$, 0&, shinfo, Len(shinfo), _
BASIC_SHGFI_FLAGS Or SHGFI_LARGEICON)
Else
hImg& = SHGetFileInfo(FileP$, 0&, shinfo, Len(shinfo), _
BASIC_SHGFI_FLAGS Or SHGFI_SMALLICON)
End If
Pic.Picture = LoadPicture()
r& = ImageList_Draw(hImg&, shinfo.iIcon, Pic.hDC, 0, 0, ILD_TRANSPARENT)
End Function
Private Sub Command1_Click()
Dim CreatedFile As String
On Error Resume Next
CreatedFile = App.Path & "\empty.txt"
FileNumber = FreeFile
'Create a file
Open CreatedFile For Output As #FileNumber
Close #FileNumber
GetIconFromFile CreatedFile, Picture1, True
Kill CreatedFile
End Sub
I'm trying to get an icon associated with a txt file, but whenever I change the icon for the txt file (control panel/folder options/file types) to see the change in my program I have to restart it.
Is it possible to get the changed icon without restarting my program ?
Here's the code i'm talking about
Add Picture1 and Command1
Const MAX_PATH2 = 260
Const SHGFI_DISPLAYNAME = &H200
Const SHGFI_EXETYPE = &H2000
Const SHGFI_SYSICONINDEX = &H4000 ' System icon index
Const SHGFI_LARGEICON = &H0 ' Large icon
Const SHGFI_SMALLICON = &H1 ' Small icon
Const ILD_TRANSPARENT = &H1 ' Display transparent
Const SHGFI_SHELLICONSIZE = &H4
Const SHGFI_TYPENAME = &H400
Const BASIC_SHGFI_FLAGS = SHGFI_TYPENAME _
Or SHGFI_SHELLICONSIZE Or SHGFI_SYSICONINDEX _
Or SHGFI_DISPLAYNAME Or SHGFI_EXETYPE
Private Type SHFILEINFO
hIcon As Long
iIcon As Long
dwAttributes As Long
szDisplayName As String * MAX_PATH2
szTypeName As String * 80
End Type
Private 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&, ByVal i&, ByVal hDCDest&, _
ByVal x&, ByVal y&, ByVal flags&) As Long
Function GetIconFromFile(filepath As String, Pic As PictureBox, LargeIcon As Boolean)
Dim hImg As Long
Dim FileP As String
Dim r As Long
Dim shinfo As SHFILEINFO
FileP$ = filepath
If LargeIcon Then
hImg& = SHGetFileInfo(FileP$, 0&, shinfo, Len(shinfo), _
BASIC_SHGFI_FLAGS Or SHGFI_LARGEICON)
Else
hImg& = SHGetFileInfo(FileP$, 0&, shinfo, Len(shinfo), _
BASIC_SHGFI_FLAGS Or SHGFI_SMALLICON)
End If
Pic.Picture = LoadPicture()
r& = ImageList_Draw(hImg&, shinfo.iIcon, Pic.hDC, 0, 0, ILD_TRANSPARENT)
End Function
Private Sub Command1_Click()
Dim CreatedFile As String
On Error Resume Next
CreatedFile = App.Path & "\empty.txt"
FileNumber = FreeFile
'Create a file
Open CreatedFile For Output As #FileNumber
Close #FileNumber
GetIconFromFile CreatedFile, Picture1, True
Kill CreatedFile
End Sub