Jump to content

How to get icon associated with an extension


Recommended Posts

Hi friends!

I making a listview who displays files and i need set correct icon example: If computer have installed WinRAR zip files will have winrar icon, if have installed adobe reader pdf files have adobe reader icon. I trying to find a way to get icon associated with a extension on the computer, to after put in my listview. Thanks in advance.

Link to comment
Share on other sites

Try this :

#include <WinAPI.au3>
#Include <Array.au3> ; Just for _ArrayDisplay()
 
$aIcon = _GetDefaultIcon(".txt")
_ArrayDisplay($aIcon)
 
Func _GetDefaultIcon($sExt)
    Local $aRet[2]
    If NOT StringRegExp($sExt, "^\.") Then $sExt = "." & $sExt
 
    Local $sFileType = RegRead("HKCR\" & $sExt, "")
    If $sFileType = "" Then Return SetError(1, 0, "")
 
    Local $sDefaultIcon = StringReplace ( RegRead("HKCR\" & $sFileType & "\DefaultIcon", ""), '"', '')
    If $sDefaultIcon = "" Then Return SetError(1, 0, "")
 
    If StringRegExp($sDefaultIcon, ",-?\d+$") Then
        $aRet = StringRegExp($sDefaultIcon, "(.*),(-?\d+)$", 3)
    Else
        $aRet[0] = $sDefaultIcon
        $aRet[1] = 0
    EndIf
 
    ; Replaces each environment variable by its expanded value
    $aRet[0] = _WinAPI_ExpandEnvironmentStrings ($aRet[0])
    Return $aRet
EndFunc

 

Link to comment
Share on other sites

This is a totally different solution. A system image list is used to store icons, which are extracted from files/folders with _WinAPI_ShellGetFileInfo. File/folder names are specified by PIDLs. The code contains a version of _WinAPI_ShellGetFileInfo that supports PIDLs. Then the icons are shown in a listview.

#include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>
#include <WinAPICom.au3>
#include <WinAPIShPath.au3>
#include <WinAPIShellEx.au3>

Opt( "MustDeclareVars", 1 )

Example()


Func Example()
  ; Initialize COM
  _WinAPI_CoInitialize()

  ; Create GUI
  Local $hGui = GUICreate( "Drop files or folders (one at a time) on the listview to show icons", 550, 300, 300, 200, -1, BitOR( $WS_EX_ACCEPTFILES, $WS_EX_TOPMOST ) )

  ; System image list
  Local $hImlLarge = _WinAPI_ShellGetImageList()

  ; Create ListView
  Local $idListView = GUICtrlCreateListView( "", 0, 0, 550, 300 )
  GUICtrlSetStyle( $idListView, BitOR( $GUI_SS_DEFAULT_LISTVIEW, $LVS_ICON ) )
  GUICtrlSetState( $idListView, $GUI_DROPACCEPTED )
  Local $hListView = GUICtrlGetHandle( $idListView )
  _GUICtrlListView_SetExtendedListViewStyle( $hListView, $LVS_EX_DOUBLEBUFFER )
  _GUICtrlListView_SetImageList( $hListView, $hImlLarge, 0 )

  ; Show GUI
  GUISetState( @SW_SHOW )

  ; Message loop
  While 1
    Switch GUIGetMsg()
      Case $GUI_EVENT_DROPPED
        Local $sFile = @GUI_DragFile
        Local $iImage = GetIconIndex( $sFile )
        Local $sText = StringRight( $sFile, StringLen( $sFile ) - StringInStr( $sFile, "\", 0, -1 ) )
        _GUICtrlListView_AddItem( $hListView, $sText, $iImage )
      Case $GUI_EVENT_CLOSE
        ExitLoop
    EndSwitch
  WEnd

  ; Cleanup
  _WinAPI_CoUninitialize()
  GUIDelete( $hGui )
  Exit
EndFunc

Func GetIconIndex( $sFileName )
  Local $pPIDL = _WinAPI_ShellILCreateFromPath( $sFileName )
  Local $tSHFILEINFO = DllStructCreate( $tagSHFILEINFO )
  Local $iFlags = BitOr( $SHGFI_PIDL, $SHGFI_SYSICONINDEX )
  _WinAPI_ShellGetFileInfoEx( $pPIDL, $iFlags, 0, $tSHFILEINFO )
  Local $iIcon = DllStructGetData( $tSHFILEINFO, "iIcon" )
  _WinAPI_CoTaskMemFree( $pPIDL )
  Return $iIcon
EndFunc

Func _WinAPI_ShellGetFileInfoEx($pPIDL, $iFlags, $iAttributes, ByRef $tSHFILEINFO)
  Local $aRet = DllCall('shell32.dll', 'dword_ptr', 'SHGetFileInfoW', 'ptr', $pPIDL, 'dword', $iAttributes, 'struct*', $tSHFILEINFO, 'uint', DllStructGetSize($tSHFILEINFO), 'uint', $iFlags)
  If @error Then Return SetError(@error, @extended, 0)
  Return $aRet[0]
EndFunc

Take a look at System Image Lists if you want to show state information (overlay icon (eg. the small link overlay in lower left corner of an icon), ghosted state (used for protected system folders) or compressed state (blue text color if compressed file)).

Edited by LarsJ
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...