Link to home
Start Free TrialLog in
Avatar of sundar_papco
sundar_papco

asked on

Getting ICON information

Hai,

Did anybody please tell me, how to get and use the associated icon with a specific extension? For example say, if i want to add a icon associated with the .txt files to a grid control, how can I do it. I want to add icons of all the file types named word,corel draw etc..

Please answer me. I am searching the answer for a long time. I will be very much thanfull to you if you reply.

No problem if it involves lot of API calls and other technical information.

Yours,
Sundar.
Avatar of anthony_glenwright
anthony_glenwright
Flag of Australia image

Here's an extract of code.. hopefully you can work it out - let me know if you need help.

Declare Function ExtractAssociatedIcon Lib "shell32.dll" Alias "ExtractAssociatedIconA" (ByVal hInst As Long, ByVal lpIconPath As String, lpiIcon As Long) As Long

Declare Function DrawIcon Lib "user32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal hIcon As Long) As Long
Declare Function DestroyIcon Lib "user32" (ByVal hIcon As Long) As Integer
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


Sub SetIcon(a_file As String, a_Item As ListItem)
  l_hIcon = ExtractAssociatedIcon(App.hInstance, a_file & Chr$(0), 0)
 
  If l_hIcon <> 0 Then
    picIcon(0).Height = 36 * Screen.TwipsPerPixelY
    picIcon(0).Width = 36 * Screen.TwipsPerPixelX
       
    picIcon(0).AutoRedraw = -1  ' Make the picture's hDC point to the persistent bitmap.
    picIcon(1).AutoRedraw = -1  ' Make the picture's hDC point to the persistent bitmap.
   
    picIcon(0).Cls
    iRetval = DrawIconEx(picIcon(0).hdc, 1, 1, l_hIcon, 32, 32, 0, 0, DI_NORMAL)
    picIcon(0).Refresh
    Set newItem = ImageListListView.ListImages.Add(, GetExtension(a_file), picIcon(0).Image)
   
    picIcon(1).Cls
    iRetval = DrawIconEx(picIcon(1).hdc, 1, 1, l_hIcon, 16, 16, 0, 0, DI_NORMAL)
    picIcon(1).Refresh
    Set newItem = ImageListListViewSmall.ListImages.Add(, GetExtension(a_file), picIcon(1).Image)
   
    iRetval = DestroyIcon(l_hIcon)
  Else
    Set newItem = ImageListListView.ListImages.Add(, GetExtension(a_file), picDefault.Image)
    Set newItem = ImageListListViewSmall.ListImages.Add(, GetExtension(a_file), picDefault.Image)
  End If
  a_Item.SmallIcon = ImageListListViewSmall.ListImages.Item(GetExtension(a_file)).Index
  a_Item.Icon = ImageListListView.ListImages.Item(GetExtension(a_file)).Index

End Sub

There's a bit more to it then this - this is just a quick copy/paste, but hopefully you can work it out from this code..
ASKER CERTIFIED SOLUTION
Avatar of anthony_glenwright
anthony_glenwright
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial