show icons based on file extension

Dear All

 I have to show the file extension icon in a picture box when the user chooses the extension. How do i do it?

A list box would have doc, xls, ppt, pdf as entries. When the user changes the list box, the corresponding icon should be shown in the picture box. There are 2 picture box, one showing a small icon and the other showing big icon.

I cannot use SHGetFileInfo, bec the user will not be choosing a file.

Any help would be greatly appreciated.

Who is Participating?
BelfryConnect With a Mentor Commented:
Yeap, still use SHGetFileInfo, but with the flags param set with SHGFI_USEFILEATTRIBUTES, thus you can specify a non-exist file, e.g.: c:\abc.doc where c:\abc.doc does not exist in fact.

hope this helps.
Ryan ChongCommented:
Here is an workable example:

Private Declare Function FindExecutable Lib "shell32" _
   Alias "FindExecutableA" _
  (ByVal lpFile As String, _
   ByVal lpDirectory As String, _
   ByVal sResult As String) As Long

Private Const MAX_PATH As Long = 260
Private Const ERROR_FILE_NO_ASSOCIATION As Long = 31
Private Const ERROR_FILE_NOT_FOUND As Long = 2
Private Const ERROR_PATH_NOT_FOUND As Long = 3
Private Const ERROR_FILE_SUCCESS As Long = 32 'my constant
Private Const ERROR_BAD_FORMAT As Long = 11

Const DI_MASK = &H1
Const DI_IMAGE = &H2
Private Declare Function ExtractAssociatedIcon Lib "shell32.dll" Alias "ExtractAssociatedIconA" (ByVal hInst As Long, ByVal lpIconPath As String, lpiIcon As Long) As Long
Private 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
Private Declare Function DestroyIcon Lib "user32" (ByVal hIcon As Long) As Long

Private Sub Command1_Click()

   Dim success As Long
   Dim pos As Long
   Dim sResult As String
   Dim msg As String
   sResult = Space$(MAX_PATH)

  'lpFile: name of the file of interest
  'lpDirectory: location of lpFile
  'sResult: path and name of executable associated with lpFile
   success = FindExecutable("temp.txt", "E:\dbs\HPB\", sResult)
   Select Case success
      Case ERROR_FILE_NO_ASSOCIATION: msg = "no association"
      Case ERROR_FILE_NOT_FOUND: msg = "file not found"
      Case ERROR_PATH_NOT_FOUND: msg = "path not found"
      Case ERROR_BAD_FORMAT:     msg = "bad format"
      Case Is >= ERROR_FILE_SUCCESS:
         pos = InStr(sResult, Chr$(0))
         If pos Then
            msg = Left$(sResult, pos - 1)
         End If
   End Select
   MsgBox msg
   If msg <> "" Then
        Dim mIcon As Long
        'Extract the associated icon
        mIcon = ExtractAssociatedIcon(App.hInstance, msg, 2)
        'Draw the icon on the form
        DrawIconEx Picture1.hdc, 0, 0, mIcon, 0, 0, 0, 0, DI_NORMAL
        'remove the icon from the memory
        DestroyIcon mIcon

   End If
End Sub

Hope this help
This old question (QID 20568321) needs to be finalized -- accept an answer, split points, or get a refund.  Please see for information and options.
All Courses

From novice to tech pro — start learning today.