show icons based on file extension

Posted on 2003-03-30
Medium Priority
Last Modified: 2010-04-07
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.

Question by:VBBEGINER
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Accepted Solution

Belfry earned 150 total points
ID: 8236367
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.
LVL 53

Expert Comment

by:Ryan Chong
ID: 8236457
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

Expert Comment

ID: 8531293
This old question (QID 20568321) needs to be finalized -- accept an answer, split points, or get a refund.  Please see http://www.cityofangels.com/Experts/Closing.htm for information and options.

Featured Post


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question