Extract Icon From File

'This example will show you how to extract icon from Any file.
'Add a module to your project (In the menu choose Project -> Add Module, Then click Open)
'Add 1 Picture Box (named Picture1) to your form. Set The Picture Box AutoRedraw
'property to True.
'Insert this code to the module :

Public Const DI_MASK = &H1
Public Const DI_IMAGE = &H2
Public Const DI_NORMAL = DI_MASK Or DI_IMAGE
Declare Function ExtractAssociatedIcon Lib "shell32.dll" Alias "ExtractAssociatedIconA" _
(ByVal hInst As Long, ByVal lpIconPath As String, lpiIcon As Long) As Long
Declare Function DrawIconEx Lib "user32" (ByVal hdc As Long, ByVal xLeft As Long, _
ByVal yTop As Long, ByVal hIcon As Long, ByVal cxWidth As Long, ByVal cyWidth _
As Long, ByVal istepIfAniCur As Long, ByVal hbrFlickerFreeDraw As Long, ByVal _
diFlags As Long) As Long
Declare Function DestroyIcon Lib "user32" (ByVal hIcon As Long) As Long

'Insert the following code to your form:

Private Sub Form_Load()
'Replace 'C:\Autoexec.bat' with the file you want to get its icon.
mIcon = ExtractAssociatedIcon(App.hInstance, "C:\Autoexec.bat", 2)
'Draw the icon on the Picture Box. To draw the icon on a Form, Replace the
'Picture1' Below with your form name
DrawIconEx Picture1.hdc, 0, 0, mIcon, 0, 0, 0, 0, DI_NORMAL
'Remove the icon from the memory
DestroyIcon mIcon
End Sub

Go Back