Cette action est irreversible, confirmez la suppression du commentaire ?
Signaler le commentaire
Veuillez sélectionner un problème
Nudité
Violence
Harcèlement
Fraude
Vente illégale
Discours haineux
Terrorisme
Autre
François Picalausa
Bonjour/soir,
C'est tout à fait possible à partir d'un contrôle listview associé à un imagelist. Exemple:
'Dans une feuille, Form1 'Ajouter Microsoft Windows Common Controls 6 dans la boite à outils 'Une imagelist, ImageList1 'Une Listview, ListView1
Option Explicit
Private Sub Form_Load() 'On va comencer par ajouter les fichiers à la racine du c Dim MyIcon As Picture Dim strBuffer As String
strBuffer = Dir("c:")
Do While Len(strBuffer) 'On tente de récupérer un icône Set MyIcon = GetFileIconLarge("c:" & strBuffer)
'Si on l'a récupérée, on l'ajoute à la collection d'images If Not MyIcon Is Nothing Then ImageList1.ListImages.Add , strBuffer, MyIcon ListView1.ListItems.Add , , strBuffer, strBuffer Else ListView1.ListItems.Add , , strBuffer End If Set MyIcon = Nothing 'On ajoute une item à la listview
strBuffer = Dir Loop
'On peut aussi ajouter un fichier par son extension Set MyIcon = GetFileIconLarge("toto.jpg") 'Si on l'a récupérée, on l'ajoute à la collection d'images If Not MyIcon Is Nothing Then '* est interdit dans un nom de fichier. On sera donc sûr que l'item n'existe pas déjà ImageList1.ListImages.Add , "*testicon", MyIcon ListView1.ListItems.Add , , "testitem", "*testicon" Else ListView1.ListItems.Add , , "testitem" End If Set MyIcon = Nothing
'Ou retrouver un exécutable sans son path complet Set MyIcon = GetFileIconLarge2("calc.exe") 'Si on l'a récupérée, on l'ajoute à la collection d'images If Not MyIcon Is Nothing Then ImageList1.ListImages.Add , "*calc", MyIcon ListView1.ListItems.Add , , "calc.exe", "*calc" Else ListView1.ListItems.Add , , "calc.exe" End If Set MyIcon = Nothing End Sub
'Dans un module du nom IconFromFile: Option Explicit
Private Const MAX_PATH = 260
Private Type SHFILEINFO hIcon As Long iIcon As Long dwAttributes As Long szDisplayName As String * MAX_PATH 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 cbFileInfo As Long, _ ByVal uFlags As Long _ ) _ As Long
Private Declare Function DestroyIcon _ Lib "user32" _ ( _ ByVal hIcon As Long _ ) _ As Long
Private Type PICTDESC cbSizeOfStruct As Long picType As Long hPicture As Long xExt As Long yExt As Long End Type
Private Type GUID Data1 As Long Data2 As Integer Data3 As Integer Data4(0 To 7) As Byte End Type
Private Declare Function OleCreatePictureIndirect _ Lib "olepro32.dll" _ ( _ lpPictDesc As PICTDESC, _ riid As GUID, _ ByVal fOwn As Long, _ ppvObj As Any _ ) _ 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
Public Function GetFileIconLarge(strFileName As String) As Picture Dim Infos As SHFILEINFO Dim hLargeIcon As Long
If SHGetFileInfo(strFileName, _ 0, _ Infos, _ Len(Infos), _ SHGFI_FLAGS.SHGFI_ICON Or SHGFI_FLAGS.SHGFI_LARGEICON Or SHGFI_FLAGS.SHGFI_USEFILEATTRIBUTES) Then
If Infos.hIcon Then Set GetFileIconLarge = GetPictureFromIcon(Infos.hIcon) End If End If End Function
Public Function GetFileIconLarge2(strFileName As String) As Picture Dim hLargeIcon As Long
If ExtractIconEx(strFileName, 0, hLargeIcon, ByVal 0&, 1) = 1 Then Set GetFileIconLarge2 = GetPictureFromIcon(hLargeIcon) End If End Function
Public Function GetFileIconSmall(strFileName As String) As Picture Dim Infos As SHFILEINFO Dim hSmallIcon As Long
If SHGetFileInfo(strFileName, _ 0, _ Infos, _ Len(Infos), _ SHGFI_FLAGS.SHGFI_ICON Or SHGFI_FLAGS.SHGFI_SMALLICON Or SHGFI_FLAGS.SHGFI_USEFILEATTRIBUTES) Then
If Infos.hIcon Then Set GetFileIconSmall = GetPictureFromIcon(Infos.hIcon) End If End If End Function
Public Function GetFileIconSmall2(strFileName As String) As Picture Dim hSmallIcon As Long
If ExtractIconEx(strFileName, 0, ByVal 0&, hSmallIcon, 1) = 1 Then Set GetFileIconSmall2 = GetPictureFromIcon(hSmallIcon) End If End Function
Public Function GetPictureFromIcon(hIcon As Long) As IPicture Dim PictureDesc As PICTDESC Dim IPictureGUID As GUID Dim PictureObject As Picture
"eon2" a écrit dans le message de news:c1cbp6$1nc$
Existe t'il un moyen d'afficher les icones des fichiers dans une FileListBox ou un controle simmilaire?
Merci
Bonjour/soir,
C'est tout à fait possible à partir d'un contrôle listview associé à un
imagelist.
Exemple:
'Dans une feuille, Form1
'Ajouter Microsoft Windows Common Controls 6 dans la boite à outils
'Une imagelist, ImageList1
'Une Listview, ListView1
Option Explicit
Private Sub Form_Load()
'On va comencer par ajouter les fichiers à la racine du c
Dim MyIcon As Picture
Dim strBuffer As String
strBuffer = Dir("c:")
Do While Len(strBuffer)
'On tente de récupérer un icône
Set MyIcon = GetFileIconLarge("c:" & strBuffer)
'Si on l'a récupérée, on l'ajoute à la collection d'images
If Not MyIcon Is Nothing Then
ImageList1.ListImages.Add , strBuffer, MyIcon
ListView1.ListItems.Add , , strBuffer, strBuffer
Else
ListView1.ListItems.Add , , strBuffer
End If
Set MyIcon = Nothing
'On ajoute une item à la listview
strBuffer = Dir
Loop
'On peut aussi ajouter un fichier par son extension
Set MyIcon = GetFileIconLarge("toto.jpg")
'Si on l'a récupérée, on l'ajoute à la collection d'images
If Not MyIcon Is Nothing Then
'* est interdit dans un nom de fichier. On sera donc sûr que l'item
n'existe pas déjà
ImageList1.ListImages.Add , "*testicon", MyIcon
ListView1.ListItems.Add , , "testitem", "*testicon"
Else
ListView1.ListItems.Add , , "testitem"
End If
Set MyIcon = Nothing
'Ou retrouver un exécutable sans son path complet
Set MyIcon = GetFileIconLarge2("calc.exe")
'Si on l'a récupérée, on l'ajoute à la collection d'images
If Not MyIcon Is Nothing Then
ImageList1.ListImages.Add , "*calc", MyIcon
ListView1.ListItems.Add , , "calc.exe", "*calc"
Else
ListView1.ListItems.Add , , "calc.exe"
End If
Set MyIcon = Nothing
End Sub
'Dans un module du nom IconFromFile:
Option Explicit
Private Const MAX_PATH = 260
Private Type SHFILEINFO
hIcon As Long
iIcon As Long
dwAttributes As Long
szDisplayName As String * MAX_PATH
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 cbFileInfo As Long, _
ByVal uFlags As Long _
) _
As Long
Private Declare Function DestroyIcon _
Lib "user32" _
( _
ByVal hIcon As Long _
) _
As Long
Private Type PICTDESC
cbSizeOfStruct As Long
picType As Long
hPicture As Long
xExt As Long
yExt As Long
End Type
Private Type GUID
Data1 As Long
Data2 As Integer
Data3 As Integer
Data4(0 To 7) As Byte
End Type
Private Declare Function OleCreatePictureIndirect _
Lib "olepro32.dll" _
( _
lpPictDesc As PICTDESC, _
riid As GUID, _
ByVal fOwn As Long, _
ppvObj As Any _
) _
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
Public Function GetFileIconLarge(strFileName As String) As Picture
Dim Infos As SHFILEINFO
Dim hLargeIcon As Long
If SHGetFileInfo(strFileName, _
0, _
Infos, _
Len(Infos), _
SHGFI_FLAGS.SHGFI_ICON Or SHGFI_FLAGS.SHGFI_LARGEICON Or
SHGFI_FLAGS.SHGFI_USEFILEATTRIBUTES) Then
If Infos.hIcon Then
Set GetFileIconLarge = GetPictureFromIcon(Infos.hIcon)
End If
End If
End Function
Public Function GetFileIconLarge2(strFileName As String) As Picture
Dim hLargeIcon As Long
If ExtractIconEx(strFileName, 0, hLargeIcon, ByVal 0&, 1) = 1 Then
Set GetFileIconLarge2 = GetPictureFromIcon(hLargeIcon)
End If
End Function
Public Function GetFileIconSmall(strFileName As String) As Picture
Dim Infos As SHFILEINFO
Dim hSmallIcon As Long
If SHGetFileInfo(strFileName, _
0, _
Infos, _
Len(Infos), _
SHGFI_FLAGS.SHGFI_ICON Or SHGFI_FLAGS.SHGFI_SMALLICON Or
SHGFI_FLAGS.SHGFI_USEFILEATTRIBUTES) Then
If Infos.hIcon Then
Set GetFileIconSmall = GetPictureFromIcon(Infos.hIcon)
End If
End If
End Function
Public Function GetFileIconSmall2(strFileName As String) As Picture
Dim hSmallIcon As Long
If ExtractIconEx(strFileName, 0, ByVal 0&, hSmallIcon, 1) = 1 Then
Set GetFileIconSmall2 = GetPictureFromIcon(hSmallIcon)
End If
End Function
Public Function GetPictureFromIcon(hIcon As Long) As IPicture
Dim PictureDesc As PICTDESC
Dim IPictureGUID As GUID
Dim PictureObject As Picture
C'est tout à fait possible à partir d'un contrôle listview associé à un imagelist. Exemple:
'Dans une feuille, Form1 'Ajouter Microsoft Windows Common Controls 6 dans la boite à outils 'Une imagelist, ImageList1 'Une Listview, ListView1
Option Explicit
Private Sub Form_Load() 'On va comencer par ajouter les fichiers à la racine du c Dim MyIcon As Picture Dim strBuffer As String
strBuffer = Dir("c:")
Do While Len(strBuffer) 'On tente de récupérer un icône Set MyIcon = GetFileIconLarge("c:" & strBuffer)
'Si on l'a récupérée, on l'ajoute à la collection d'images If Not MyIcon Is Nothing Then ImageList1.ListImages.Add , strBuffer, MyIcon ListView1.ListItems.Add , , strBuffer, strBuffer Else ListView1.ListItems.Add , , strBuffer End If Set MyIcon = Nothing 'On ajoute une item à la listview
strBuffer = Dir Loop
'On peut aussi ajouter un fichier par son extension Set MyIcon = GetFileIconLarge("toto.jpg") 'Si on l'a récupérée, on l'ajoute à la collection d'images If Not MyIcon Is Nothing Then '* est interdit dans un nom de fichier. On sera donc sûr que l'item n'existe pas déjà ImageList1.ListImages.Add , "*testicon", MyIcon ListView1.ListItems.Add , , "testitem", "*testicon" Else ListView1.ListItems.Add , , "testitem" End If Set MyIcon = Nothing
'Ou retrouver un exécutable sans son path complet Set MyIcon = GetFileIconLarge2("calc.exe") 'Si on l'a récupérée, on l'ajoute à la collection d'images If Not MyIcon Is Nothing Then ImageList1.ListImages.Add , "*calc", MyIcon ListView1.ListItems.Add , , "calc.exe", "*calc" Else ListView1.ListItems.Add , , "calc.exe" End If Set MyIcon = Nothing End Sub
'Dans un module du nom IconFromFile: Option Explicit
Private Const MAX_PATH = 260
Private Type SHFILEINFO hIcon As Long iIcon As Long dwAttributes As Long szDisplayName As String * MAX_PATH 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 cbFileInfo As Long, _ ByVal uFlags As Long _ ) _ As Long
Private Declare Function DestroyIcon _ Lib "user32" _ ( _ ByVal hIcon As Long _ ) _ As Long
Private Type PICTDESC cbSizeOfStruct As Long picType As Long hPicture As Long xExt As Long yExt As Long End Type
Private Type GUID Data1 As Long Data2 As Integer Data3 As Integer Data4(0 To 7) As Byte End Type
Private Declare Function OleCreatePictureIndirect _ Lib "olepro32.dll" _ ( _ lpPictDesc As PICTDESC, _ riid As GUID, _ ByVal fOwn As Long, _ ppvObj As Any _ ) _ 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
Public Function GetFileIconLarge(strFileName As String) As Picture Dim Infos As SHFILEINFO Dim hLargeIcon As Long
If SHGetFileInfo(strFileName, _ 0, _ Infos, _ Len(Infos), _ SHGFI_FLAGS.SHGFI_ICON Or SHGFI_FLAGS.SHGFI_LARGEICON Or SHGFI_FLAGS.SHGFI_USEFILEATTRIBUTES) Then
If Infos.hIcon Then Set GetFileIconLarge = GetPictureFromIcon(Infos.hIcon) End If End If End Function
Public Function GetFileIconLarge2(strFileName As String) As Picture Dim hLargeIcon As Long
If ExtractIconEx(strFileName, 0, hLargeIcon, ByVal 0&, 1) = 1 Then Set GetFileIconLarge2 = GetPictureFromIcon(hLargeIcon) End If End Function
Public Function GetFileIconSmall(strFileName As String) As Picture Dim Infos As SHFILEINFO Dim hSmallIcon As Long
If SHGetFileInfo(strFileName, _ 0, _ Infos, _ Len(Infos), _ SHGFI_FLAGS.SHGFI_ICON Or SHGFI_FLAGS.SHGFI_SMALLICON Or SHGFI_FLAGS.SHGFI_USEFILEATTRIBUTES) Then
If Infos.hIcon Then Set GetFileIconSmall = GetPictureFromIcon(Infos.hIcon) End If End If End Function
Public Function GetFileIconSmall2(strFileName As String) As Picture Dim hSmallIcon As Long
If ExtractIconEx(strFileName, 0, ByVal 0&, hSmallIcon, 1) = 1 Then Set GetFileIconSmall2 = GetPictureFromIcon(hSmallIcon) End If End Function
Public Function GetPictureFromIcon(hIcon As Long) As IPicture Dim PictureDesc As PICTDESC Dim IPictureGUID As GUID Dim PictureObject As Picture