Jump to content
Sign in to follow this  
MachinistProgrammer

File manager listview icon help

Recommended Posts

MachinistProgrammer

so i was writing a file manager window but i don't know how to put icons on the files & folders

; -- Created with ISN Form Studio 2 for ISN AutoIt Studio -- ;
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Include <GuiButton.au3>
#include <array.au3>
#include <file.au3>
#include <GuiListView.au3>
Global $currentdir1,$currentdir2
_fm_get_stuff1()
$Xplore_file_manager = GUICreate("Xplore file manager",400,565,-1,-1,-1,-1)
GUISetBkColor(0xFFFFFF,$Xplore_file_manager)
GUICtrlSetBkColor(-1,"0xBFBFFF")
$Filelist1a = GUICtrlCreatelistview("Name                                  |Size",10,100,380,420,0,$LVS_EX_GRIDLINES + 0x00000020)
$filelist1 = GUICtrlGetHandle($Filelist1a)
_arraytolist(_FM_Get_stuff1(),$filelist1a)
$path = GUICtrlCreateInput("My Computer",110,70,280,30,-1,512)
GUICtrlSetFont(-1,12,400,0,"MS Sans Serif")
$b = GUICtrlCreateButton("Back",10,70,100,30,-1,-1)
GUISetState(@SW_SHOW,$Xplore_file_manager)
GUIRegisterMsg($WM_NOTIFY,"_Update_on_click")
While 1
 $nMsg = GUIGetMsg()
 Switch $nMsg
  Case $GUI_EVENT_CLOSE
   Exit
  Case $b
   $ix = StringSplit($currentdir1,'\',2)
   $iu = UBound($ix) - 2
   $ilen = StringLen($ix[$iu] & '\')
   $iprev = StringTrimRight($currentdir1,$ilen)
   _GUICtrlListView_DeleteAllItems($filelist1a)
   _arraytolist(_fm_get_stuff1($iprev),$filelist1a)
   If $iprev = '' Then
   GUICtrlSetData($path,'My Computer')
   Else
   GUICtrlSetData($path,$iprev)
   EndIf
   
 EndSwitch
WEnd
Func _FM_Get_stuff1($dir = '')
 If $dir = '' Then
 $drv =  DriveGetDrive('all')
 Global $dat[UBound($drv)]
 For $i = 1 To UBound($drv) - 1
  $drv[$i] = StringUpper($drv[$i])
  $dat[$i] = FileGetSize($drv[$i])
 Next
  $currentdir1 = ''
 Return $drv
 Else
  $currentdir1 = $dir
  $iarray = _FileListToArray($dir)
  If @error = 4 Then Return 0
  Global $dat[UBound($iarray)]
  For $i = 1 to UBound($iarray) - 1
   $dat[$i] = FileGetSize($dir & $iarray[$i])
  Next
  Return $iarray
 EndIf
EndFunc

Func _arraytolist($array,$list)
$iubound = UBound($array) - 1
For $i = 1 To $iUBound
  GUICtrlCreateListViewItem($array[$i] & '|' & $dat[$i] ,$list)
 Next
EndFunc
Func _Update_on_click($hWnd, $iMsg, $iwParam, $ilParam)
 #forceref $hWnd, $iMsg, $iwParam
 Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $tInfo
 $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
 $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
 $iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
 $iCode = DllStructGetData($tNMHDR, "Code")
 Switch $hWndFrom
  Case $Filelist1
   Switch $iCode
    Case $NM_DBLCLK
     $str = StringSplit(GUICtrlRead(GUICtrlRead($Filelist1a)),"|")
      If stringinstr(FileGetAttrib($currentdir1 & $str[1]),"d") Then
      _GUICtrlListView_DeleteAllItems($Filelist1a)
      $currentdir1 &= $str[1]
      _arraytolist(_fm_get_stuff1($currentdir1 & '\'),$Filelist1a)
      GUICtrlSetData($path,$currentdir1)
      ElseIf FileExists($currentdir1 & $str[1]) Then
      ShellExecute($currentdir1 & $str[1])
      ElseIf StringRegExp($CurrentDir1 & $str[1], "[A-z]:") Then
       $currentdir1 &= $str[1]
       _GUICtrlListView_DeleteAllItems($Filelist1a)
       _arraytolist(_fm_get_stuff1(),$filelist1a)
       GUICtrlSetData($path,'My Computer')
      EndIf
     EndSwitch
 EndSwitch
EndFunc
Func _isdrive($drive)
$id = DriveGetDrive('all')
For $i = 0 To UBound($id) - 1
 If $drive = $id[$i] Then
  MsgBox(0,'',$id[$i])
  Return 1
 EndIf
Next
Return 0
EndFunc

All my projects live on github

Share this post


Link to post
Share on other sites
Clark

What you have to do is:

Set the main form style to include $LVS_ICON

Load up some icons using _GDIPlus_BitmapCreateFromFile and _GUIImageListCreate, then use _GUICtrlListView_SetImageList

Then use _GUICtrlListView_AddSubItem for each of the Listview rows you want to add icons to.

Share this post


Link to post
Share on other sites
PhoenixXL

Check this

#include <GUIConstantsEx.au3>
#include <GuiListView.au3>
#include <GuiImageList.au3>
#include <GDIPlus.au3>
#include <WinAPIEx.au3>

$Debug_LV = False ; Check ClassName being passed to ListView functions, set to True and use a handle to another control to see it work

_Main()

;Item is the first column of any row
;Subitem is the second, third... column of any row.

Func _Main()
    Local $hImage, $hListView, $exStyles = BitOR($LVS_EX_FULLROWSELECT, $LVS_EX_SUBITEMIMAGES)

    GUICreate("ListView Set Item Image", 400, 300)
    $hListView = GUICtrlCreateListView("", 2, 2, 394, 268)
    _GUICtrlListView_SetExtendedListViewStyle($hListView, $exStyles)

    GUISetState()

    _GDIPlus_Startup()

    $hImage = _GUIImageList_Create()

    ; Load images from small icons
    $hHbmp = _IconExtract_FromFile_Return_hHBmp(@SystemDir & "\notepad.exe")
    _GUIImageList_Add($hImage, $hHbmp)
    _WinAPI_DeleteObject($hHbmp)

    $hHbmp = _IconExtract_FromFile_Return_hHBmp(@AutoItExe)
    _GUIImageList_Add($hImage, $hHbmp)
    _WinAPI_DeleteObject($hHbmp)

    $hHbmp = _IconExtract_FromFile_Return_hHBmp(@SystemDir & "\calc.exe")
    _GUIImageList_Add($hImage, $hHbmp)
    _WinAPI_DeleteObject($hHbmp)

    _GUICtrlListView_SetImageList($hListView, $hImage, 1)

    ;The native data adding won't work with this, you have to add the data in the following way.

    ; Add columns
    _GUICtrlListView_AddColumn($hListView, "Column 1", 100)
    _GUICtrlListView_AddColumn($hListView, "Column 2", 100)
    _GUICtrlListView_AddColumn($hListView, "Column 3", 100)

    ;all the items have the notepad icon
    ;all the first sub-items have autoit icon
    ;all the second sub-items have calculator icon

    ; Add items
    _GUICtrlListView_AddItem($hListView, "Row 1: Col 1", 0) ;notepad icon
    _GUICtrlListView_AddSubItem($hListView, 0, "Row 1: Col 2", 1, 1)    ;autoit icon
    _GUICtrlListView_AddSubItem($hListView, 0, "Row 1: Col 3", 2, 2)    ;calc icon

    _GUICtrlListView_AddItem($hListView, "Row 2: Col 1", 0) ;notepad icon
    _GUICtrlListView_AddSubItem($hListView, 1, "Row 2: Col 2", 1, 1)    ;autoit icon

    _GUICtrlListView_AddItem($hListView, "Row 3: Col 1", 0) ;notepad icon

    #cs - this could even be directly when adding the item/sub-items.

        ;Set the image of the items(zeroth) to the first index(0) on imagelist( notepad )
        _GUICtrlListView_SetItemImage($hListView, 0, 0)
        _GUICtrlListView_SetItemImage($hListView, 1, 0)
        _GUICtrlListView_SetItemImage($hListView, 2, 0)

        ;Set the image of the first sub-items(zeroth) to the second index(1) on imagelist( autoit )
        _GUICtrlListView_SetItemImage($hListView, 0, 1, 1)
        _GUICtrlListView_SetItemImage($hListView, 1, 1, 1)

        ;Set the image of the second sub-items(zeroth) to the third index(2) on imagelist( calc )
        _GUICtrlListView_SetItemImage($hListView, 0, 2, 2)

    #ce

    ; Loop until user exits
    Do
    Until GUIGetMsg() = $GUI_EVENT_CLOSE
    GUIDelete()

    _GDIPlus_Shutdown()
EndFunc   ;==>_Main

;Use the following functions to resize bitmap extracted from icon.
Func _GDIPlus_ImageResize(ByRef $hBmp, $iW, $iH)

    $hBmp_Ret = DllCall($ghGDIPDll, "uint", "GdipCreateBitmapFromScan0", "int", $iW, "int", $iH, "int", 0, "int", 0x0026200A, "ptr", 0, "int*", 0)
    $hBmp_Ret = $hBmp_Ret[6]

    ;Get the graphic context of the new bitmap
    $hGraphic = _GDIPlus_ImageGetGraphicsContext($hBmp_Ret)

    Const $InterpolationModeHighQualityBilinear = 7
    ;Set the interpolat
    DllCall($ghGDIPDll, "int", "GdipSetInterpolationMode", "hwnd", $hGraphic, "int", $InterpolationModeHighQualityBilinear)

    ;Draw the loaded image onto the new bitmap at the size you want
    _GDIPlus_GraphicsDrawImageRect($hGraphic, $hBmp, 0, 0, $iW, $iH)

    ;Clean up.
    _GDIPlus_ImageDispose($hBmp)
    _GDIPlus_GraphicsDispose($hGraphic)

    ;Replace the bitmap with the newly created bitmap
    $hBmp = $hBmp_Ret
    $hBmp_Ret = 0 ;release the copy of bitmap.

    Return SetError(0, 0, 1)
EndFunc   ;==>_GDIPlus_ImageResize

Func _IconExtract_FromFile_Return_hHBmp($S_File, $iSmallIcon = True)

    $hIcon = _WinAPI_ShellExtractAssociatedIcon($S_File, $iSmallIcon) ;Get the respective icon
    $hHbmp = _WinAPI_Create32BitHBITMAP($hIcon, 1, 1)

    Return $hHbmp

EndFunc   ;==>_IconExtract_FromFile_Return_hHBmp
Edited by PhoenixXL

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Share this post


Link to post
Share on other sites
PhoenixXL

I got some free time

Here is what I have for you

Features Added

  •     AutoSuggestion and AutoAppend upon entering in the Edit.
  •     Pressing backspace in the GUI goes to the last folder.
  •     Pressing Enter in the edit makes the text the current directory.
  •     Icons with folders and files.

 

MainScript.au3
;by Phoenix XL 15/05/2013

;Features Added
;   AutoSuggestion and AutoAppend upon entering in the Edit.
;   Pressing backspace in the GUI goes to the last folder.
;   Pressing Enter in the edit makes the text the current directory.
;   Icons with folders and files.

; -- Created with ISN Form Studio 2 for ISN AutoIt Studio -- ;

#include "MyHeader.au3"
#include <GUIConstantsEx.au3>
#include <EditConstants.au3>

Opt("TrayIconDebug", 1) ;hover the trayicon and find where Autoit is.

$Xplore_file_manager = GUICreate("Xplore file manager", 400, 565)
GUISetBkColor(0xFFFFFF, $Xplore_file_manager)
GUICtrlSetBkColor(-1, 0xBFBFFF)

;create a dummy for backspace and enter
Local $iBackSpace = GUICtrlCreateDummy()
Local $iEnter = GUICtrlCreateDummy()
Local Const $aAccel[2][2] = [["{BACKSPACE}", $iBackSpace],["{ENTER}", $iEnter]]
GUISetAccelerators($aAccel, $Xplore_file_manager)

$iListView = GUICtrlCreateListView("", 10, 100, 380, 420, 0)
$hListView = GUICtrlGetHandle($iListView)
_GUICtrlListView_SetExtendedListViewStyle($hListView, BitOR($LVS_EX_FULLROWSELECT, $LVS_EX_GRIDLINES))

; Add columns
_GUICtrlListView_AddColumn($hListView, "Name", 260)
_GUICtrlListView_AddColumn($hListView, "Size", 100)

UpdateList()

$iInput_DirPath = GUICtrlCreateEdit("", 110, 70, 280, 30, $ES_AUTOHSCROLL, 512) ;Input controls will block ENTER processing
GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
_SHAutoComplete(-1) ;

$iBtn_Back = GUICtrlCreateButton("Back", 10, 70, 100, 30, -1, -1)

GUIRegisterMsg($WM_NOTIFY, "_Update_on_Dblclick")

GUISetState()

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

        Case $iEnter
            $hfocus = _WinAPI_GetFocus()
            $iControlID = _WinAPI_GetDlgCtrlID($hfocus)

            If $iControlID = $iInput_DirPath Then
                $sRead = GUICtrlRead($iControlID)

                If IsDirectory($sRead) Then

                    UpdateList($sRead)

                Else

                    ShellExecute($sRead)

                EndIf
            Else
                ControlSend($Xplore_file_manager, "", $iControlID, "{ENTER}")
            EndIf

        Case $iBtn_Back, $iBackSpace
            Go_PrevDir()

    EndSwitch
WEnd

MyHeader.au3

#OnAutoItStartRegister "CoInitialize"
#include <WinAPIEx.au3>
#include <GUIListView.au3>
#include <GUIImageList.au3>
#include <Misc.au3>
#include <File.au3>
#include <WindowsConstants.au3>
OnAutoItExitRegister("CoUnInitialize")

;global variables
Global $s_Selected_Folder, $ag_Size[1], $Xplore_file_manager, $hListView, $iListView, $iInput_DirPath, $iBtn_Back

Func Get_Files_Folders($s_Directory = '')

    $aRet = _Iif($s_Directory = "", DriveGetDrive('all'), _FileListToArray($s_Directory))
    If @error Or IsArray($aRet) = 0 Then Return SetError(1, 0, 0)

    ReDim $ag_Size[UBound($aRet)]
    For $i = 1 To UBound($aRet) - 1
        If IsDirectory($aRet[$i]) Then ;Takes a lot of time
;~          $ag_Size[$i] = DirGetSize($aRet[$i])
        Else
            $ag_Size[$i] = FileGetSize($aRet[$i])
        EndIf
    Next
    $s_Selected_Folder = $s_Directory
    Return $aRet

EndFunc   ;==>Get_Files_Folders

Func UpdateList($s_Directory = "")

    If $s_Directory And StringRight($s_Directory, 1) <> "\" Then $s_Directory &= "\"

    $array = Get_Files_Folders($s_Directory)
    If IsArray($array) = 0 Then Return 0

    ;always pass a handle as $list otherwise there might be an error.
    _GUICtrlListView_DeleteAllItems($hListView)

    Static $hImageList

    If $hImageList Then _GUIImageList_Destroy($hImageList)
    $hImageList = _GUIImageList_Create()
    _GUICtrlListView_SetImageList($hListView, $hImageList, 1) ;small icons only

    $iubound = UBound($array) - 1
    For $i = 1 To $iubound
        $hImage = _IconExtract_FromFile_Return_hHBmp($s_Selected_Folder & $array[$i])

        _GUICtrlListView_AddItem($hListView, $array[$i], _GUIImageList_Add($hImageList, $hImage))
        _GUICtrlListView_AddSubItem($hListView, $i - 1, $ag_Size[$i], 1)

        _WinAPI_DeleteObject($hImage)
    Next
EndFunc   ;==>UpdateList

Func _Update_on_Dblclick($hWnd, $iMsg, $iwParam, $ilParam)
    #forceref $hWnd, $iMsg, $iwParam

    Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $tInfo
    $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))

    $iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
    $iCode = DllStructGetData($tNMHDR, "Code")

    Switch $hWndFrom
        Case $hListView

            Switch $iCode

                Case $NM_DBLCLK

                    $aHitTest = _GUICtrlListView_HitTest($hWndFrom)
                    If @error Or $aHitTest[0] = -1 Then Return
                    $s_ItemText = _GUICtrlListView_GetItemText($hWndFrom, $aHitTest[0])

                    If IsDirectory($s_Selected_Folder & $s_ItemText) Then

                        $s_Selected_Folder &= $s_ItemText & "\"
                        UpdateList($s_Selected_Folder)
                        GUICtrlSetData($iInput_DirPath, $s_Selected_Folder)

                    Else

                        ShellExecute($s_Selected_Folder & $s_ItemText)

                    EndIf
            EndSwitch

    EndSwitch

EndFunc   ;==>_Update_on_Dblclick


;======== Added Functions =======================================================

Func CoInitialize()
    DllCall('ole32.dll', 'long', 'CoInitialize', 'ptr', '')
EndFunc   ;==>CoInitialize

Func CoUnInitialize()
    DllCall('ole32.dll', 'long', 'CoUnInitialize', 'ptr', '')
EndFunc   ;==>CoUnInitialize

;make the script to go to the previous directory
Func Go_PrevDir()
    $iBack_Dir = StringRegExpReplace($s_Selected_Folder, '(.*\\).+', "$1")
    If @extended = 0 Then $iBack_Dir = ""

    UpdateList($iBack_Dir)
    GUICtrlSetData($iInput_DirPath, $iBack_Dir)
EndFunc   ;==>Go_PrevDir

Func _SHAutoComplete($ControlID, $dwFlags = -1)

    If $dwFlags = -1 Then
        Local Const $SHACF_AUTOAPPEND_FORCE_ON = 0x40000000, $SHACF_AUTOSUGGEST_FORCE_ON = 0x10000000, $SHACF_FILESYSTEM = 0x00000001
        $dwFlags = BitOR($SHACF_AUTOAPPEND_FORCE_ON, $SHACF_AUTOSUGGEST_FORCE_ON, $SHACF_FILESYSTEM)
    EndIf

    Local $aRet = DllCall('shlwapi.dll', "long", "SHAutoComplete", "hwnd", GUICtrlGetHandle($ControlID), "int", $dwFlags)

    Return $aRet[0]
EndFunc   ;==>_SHAutoComplete

Func _IconExtract_FromFile_Return_hHBmp($S_File, $iSmallIcon = True)

    $hIcon = _WinAPI_ShellExtractAssociatedIcon($S_File, $iSmallIcon) ;Get the respective icon
    $hHbmp = _WinAPI_Create32BitHBITMAP($hIcon, 1, 1)
    _WinAPI_DestroyIcon($hIcon)
    Return $hHbmp

EndFunc   ;==>_IconExtract_FromFile_Return_hHBmp

Func IsDirectory($S_File)
    Return StringInStr(FileGetAttrib($S_File), "d") <> 0
EndFunc   ;==>IsDirectory

;======== Added Functions End =======================================================

Thumbs up if it helped

Regards :)

Edited by PhoenixXL
  • Like 1

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

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
Sign in to follow this  

×