GordonFreeman

How to get icon associated with an extension

3 posts in this topic

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.

Share this post


Link to post
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

 

1 person likes this

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

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
2 people like this

Share this post


Link to post
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

  • Similar Content

    • nacerbaaziz
      By nacerbaaziz
      Hi guys
      I have an inquiry please
      I want an example of how to add an icon file to the compiled autoit file and how to use it in GUICtrlCreateIcon
      Preferably in a separate list within the resourceFor example myIcons
      I hope my request is clear and I hope you will help me solve this problem
      Because I found an example in the help files but I did not understand the method well
    • amimemeami
      By amimemeami
      I have a simple script that switches between IE and Chrome for the default browser, I even made a custom half chrome half ie icon for it. Question is, can I have the script toggle between two icons to signify the last browser it changed to? I would have the active browser's half of the icon in color, and inactive half grey scale. As a workaround, I put link next to it, and refresh the icon cache after it runs. 
      #RequireAdmin #Region ;**** Directives created by AutoIt3Wrapper_GUI **** #AutoIt3Wrapper_Icon=..\..\Icons\IE_Chrome.ico #AutoIt3Wrapper_Outfile=..\..\..\..\Users\dmking\Desktop\Browser Switcher.exe #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI **** $sCurrentDefault = RegRead('HKCU\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http\UserChoice',"ProgId") if $sCurrentDefault = "IE.HTTP" Then     ChromeDefault() Else     IEDefault() EndIf ;~ Run("ie4uinit.exe -ClearIconCache") Run("C:\Windows\System32\ie4uinit.exe -ClearIconCache") Func IEDefault()     RegWrite("HKCU\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\ftp\UserChoice","Progid","REG_SZ","IE.FTP")     RegWrite("HKCU\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http\UserChoice","Progid","REG_SZ","IE.HTTP")     RegWrite("HKCU\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\https\UserChoice","Progid","REG_SZ","IE.HTTPS")     RegWrite("HKCU\Software\Clients\StartMenuInternet","","REG_SZ","IEXPLORE.EXE") EndFunc Func ChromeDefault()     RegWrite("HKCU\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\ftp\UserChoice","Progid","REG_SZ","ChromeHTML")     RegWrite("HKCU\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http\UserChoice","Progid","REG_SZ","ChromeHTML")     RegWrite("HKCU\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\https\UserChoice","Progid","REG_SZ","ChromeHTML")     RegWrite("HKCU\Software\Clients\StartMenuInternet","","REG_SZ","Google Chrome") EndFunc  
    • Masum
      By Masum
      Hi all,
      Can the default Koda icon for forms be changed? If so, how?
      Thanks
    • Queener
      By Queener
      This is the info of the interface:
      Window
      Title:    Password Safe
      Class:    #32770
      Control
      Class:    ToolbarWindow32
      Instance:    2
      ClassnameNN:    ToolbarWindow322
      Name:    
      Advanced (Class):    [CLASS:ToolbarWindow32; INSTANCE:2]
       ToolsBar
      1:    32003    Make New Database
      2:    32002    Open Another Database
      3:    32004    Close Database
      4:    32005    Save Database
      5:    0    
      6:    32066    Copy Password to Clipboard
      7:    32068    Copy Username to Clipboard
      8:    32069    Copy Notes to Clipboard
      9:    32065    Clear the clipboard contents
      10:    0    
       
      I wanted to click on 1:    32003    Make New Database. But unfortunately, I'm unable to click on that. Any help is much appreciated. Below are the code I tried:

      $Title = "Password Safe" $Class = "[CLASS:ToolbarWindow32; INSTANCE:2]" WinWaitActive($Title) Sleep(500) Dim $hWnd = WinGetHandle($Title,"") Dim $hToolBar = ControlGetHandle($hWnd, '', $Class) ConsoleWrite($hWnd & @TAB & $hToolBar & @LF) _GUICtrlToolbar_ClickIndex($hToolBar, 32003)
    • wakillon
      By wakillon
      A little animated Screensaver based on FavIcons