Get icon from file or extension

Windows specific forum
Dr. Dri
Enthusiast
Enthusiast
Posts: 243
Joined: Sat Aug 23, 2003 6:45 pm

Get icon from file or extension

Post by Dr. Dri »

Code: Select all

Procedure.l GetFileIcon(Filename.s, IconSize.l)
  Protected IconID.l, Open.l, KeyID.l, Small.l, Large.l
  Protected KeyName.s, Length.l, Count.l, Index.l, Value.s
  
  KeyName  = GetExtensionPart("." + Filename)
  Filename = #NULL$
  
  If KeyName
    KeyName = "." + KeyName
    
    Open = RegOpenKeyEx_(#HKEY_CLASSES_ROOT, KeyName, #Null, #KEY_ALL_ACCESS, @KeyID)
    
    If Open = #ERROR_SUCCESS
      KeyName = Space(#MAX_PATH)
      Length  = #MAX_PATH
      
      RegQueryValueEx_(KeyID, #NULL$, #Null, #Null,  @KeyName, @Length)
      RegCloseKey_(KeyID)
      
      KeyName + "\DefaultIcon"
      Open = RegOpenKeyEx_(#HKEY_CLASSES_ROOT, KeyName, #Null, #KEY_ALL_ACCESS, @KeyID)
    EndIf
    
    If Open = #ERROR_SUCCESS
      Filename = Space(#MAX_PATH)
      Length   = #MAX_PATH
      
      RegQueryValueEx_(KeyID, #NULL$, #Null, #Null, @Filename, @Length)
      RegCloseKey_(KeyID)
      
      Filename = Trim(Filename)
    EndIf
  EndIf
  
  If Filename = #NULL$
    Filename = "shell32.dll,0"
  EndIf
  
  Length = Len(Filename)
  Count  = CountString(Filename, ",") + 1
  Value  = StringField(Filename, Count, ",")
  Index  = Val( Value )
  
  Length   - (Len(Value) + 1)
  Filename = Left(Filename, Length)
  
  ExtractIconEx_(Filename, Index, @Large, @Small, 1)
  
  If IconSize & #SHGFI_SMALLICON
    IconID = Small
  Else         ;#SHGFI_LARGEICON
    IconID = Large
  EndIf
  
  ProcedureReturn IconID
EndProcedure

;-test

SmallIcon = GetFileIcon("mpeg", #SHGFI_SMALLICON)
LargeIcon = GetFileIcon("mpeg", #SHGFI_LARGEICON)

If OpenWindow(0, 0, 0, 245, 105, #PB_Window_MinimizeGadget|#PB_Window_ScreenCentered, "Extension Icon")
  CreateGadgetList(WindowID(0))
  
  ImageGadget(0,  10, 10, 100, 83,LargeIcon)
  ImageGadget(1, 130, 10, 100, 83,SmallIcon)
  
  Repeat
  Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
Dri ;)
xgp
Enthusiast
Enthusiast
Posts: 128
Joined: Mon Jun 13, 2005 6:03 pm

Post by xgp »

Really nice piece of code:P
Congrats

xgp
User avatar
Droopy
Enthusiast
Enthusiast
Posts: 658
Joined: Thu Sep 16, 2004 9:50 pm
Location: France
Contact:

Post by Droopy »

Thanks for sharing
Straker
Enthusiast
Enthusiast
Posts: 701
Joined: Wed Apr 13, 2005 10:45 pm
Location: Idaho, USA

Post by Straker »

Nice. Thanks.
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

Post by rsts »

Pretty cool.

Thanks for sharing the code.

cheers
Waussie
User
User
Posts: 24
Joined: Fri Nov 11, 2005 12:14 pm

Post by Waussie »

I see an extraction lib comming...

Isn't this something for your lib, Droopy?
Currently coding the unbelievable WFS program...
You'll know it, when it gets released...
Dr. Dri
Enthusiast
Enthusiast
Posts: 243
Joined: Sat Aug 23, 2003 6:45 pm

Post by Dr. Dri »

Waussie wrote:I see an extraction lib comming...

Isn't this something for your lib, Droopy?
I don't code for Droopy's lib. But you can use it the way you want... No problem if Droopy wants to put it in his lib.

Dri ;)
Post Reply