Jump to content

Images in ListView without imagelist?


Recommended Posts

I wanted to adapt this example to virtual ListView where thumbnails would be stored in db, but i do not know method to dynamically replace ImageList in ListView and also - is it even good idea to have huge ImageList? It's size would be (5...8) x Rows in virtual ListView.

Is it possible to {dinamically?} set images as binaries to ListView cells without use ImageList?

 

#include <GUIConstantsEx.au3>
#include <GuiImageList.au3>
#include <GuiListView.au3>
#include <WinAPI.au3>
#include <WindowsConstants.au3>
#include <GDIplus.au3>
#include <Array.au3>
#include <File.au3>
#include <MsgBoxConstants.au3>

Global $iGUIWidth, $iGUIHeidth, $iGUILeft, $iGUIRight
Global $iEntryWidth, $iEntryHeidth
Global $aJpgList

Local $aJpgList = get_list_jpg()


Example($aJpgList)



Func get_list_jpg()
    ; List all the files and folders in the desktop directory using the default parameters.
    Local $aFileList = _FileListToArrayRec (@ScriptDir & "\Pic\", "*.jpg", $FLTA_FILES, $FLTAR_NORECUR, $FLTAR_SORT, $FLTAR_NOPATH)
    
    If @error = 1 Then
        MsgBox($MB_SYSTEMMODAL, "", "Path was invalid.")
        Exit
    EndIf
    If @error = 4 Then
        MsgBox($MB_SYSTEMMODAL, "", "No file(s) were found.")
        Exit
    EndIf
    ; Display the results returned by _FileListToArray.
   ;_ArrayDisplay($aFileList, "$aFileList")
    Return($aFileList)
EndFunc



Func Example($aJpgList)
    _GDIPlus_Startup()
    Local $idListview, $hImage
    
    $iGUIWidth = 988
    $iGUIHeidth = 1210
    $iGUILeft = -1
    $iGUIRight = -1
    
    $iEntryWidth = 198
    $iEntryHeidth = 288
    
    $iPicWidth = 190
    $iPicHeidth = 285
    
    $iColumnCount = Round($iGUIWidth/$iEntryWidth)
    $iGUIWidth = $iColumnCount * $iEntryWidth + 16 + 14
    
    GUICreate("JpegList", $iGUIWidth, $iGUIHeidth, -1, -1,  $WS_OVERLAPPEDWINDOW)
    $idListview = GUICtrlCreateListView("", 2, 2, $iGUIWidth - 4, $iGUIHeidth - 32, BitOR($LVS_SHOWSELALWAYS, $LVS_NOSORTHEADER, $LVS_REPORT))
    _GUICtrlListView_SetExtendedListViewStyle($idListview, BitOR($LVS_EX_GRIDLINES, $LVS_EX_DOUBLEBUFFER, $LVS_EX_INFOTIP, $LVS_EX_SUBITEMIMAGES))
    GUISetState(@SW_SHOW)

    ; Load images
    $hImage = _GUIImageList_Create($iPicWidth, $iPicHeidth)
    
    For $i = 1 To $iColumnCount
        _GUICtrlListView_AddColumn($idListview, "Image", $iEntryWidth)
    Next
    
    _GUICtrlListView_SetImageList($idListview, $hImage, 1)

    Global $GDIpBmpLarge
    Global $GDIpBmpResized = 0
    Global $GDIbmp
    
    $iIndex = 0
    $iSubItem = 0
    
    $iPic_RatioMax = $iPicWidth/$iPicHeidth
    
    For $r = 1 To UBound($aJpgList)-1
        local $sFileName = @ScriptDir&"\Pic\"&$aJpgList[$r]
        
        $GDIpBmpLarge = _GDIPlus_BitmapCreateFromFile($sFileName) ;GDI+ image!
        $aDim = _GDIPlus_ImageGetDimension($GDIpBmpLarge)
        
        if $aDim[0] <> $iPicWidth Or $aDim[1] > $iPicHeidth Then
            
            $iPic_Ratio = $aDim[0]/$aDim[1]
            
            Select
                Case $iPic_Ratio > $iPic_RatioMax
                    $iPic_WNew = $iPicWidth
                    $iPic_HNew = Round($iPicWidth/$iPic_Ratio,0)

                Case $iPic_Ratio < $iPic_RatioMax
                    $iPic_WNew = Round($iPicHeidth*$iPic_Ratio,0)
                    $iPic_HNew = $iPicHeidth
                    
                Case $iPic_Ratio = $iPic_RatioMax
                    $iPic_WNew = $iPicWidth
                    $iPic_HNew = $iPicHeidth

            EndSelect
            
            $GDIpBmpResized = _GDIPlus_ImageResize($GDIpBmpLarge, 190,$iPic_HNew) ;GDI+ image
            $GDIbmp = _GDIPlus_BitmapCreateHBITMAPFromBitmap($GDIpBmpResized) ;GDI image!
            $imgInd = _GUIImageList_Add($hImage, $GDIbmp)
        Else
            $GDIbmp = _GDIPlus_BitmapCreateHBITMAPFromBitmap($GDIpBmpLarge) ;GDI image!
            $imgInd = _GUIImageList_Add($hImage, $GDIbmp)
        endif
        
        If $iSubItem = 0 Then
            _GUICtrlListView_AddItem($idListview, $aJpgList[$r], $imgInd)
        Else
            _GUICtrlListView_AddSubItem($idListview, $iIndex,  $aJpgList[$r], $iSubItem, $imgInd)
        EndIf
        
        $iSubItem += 1
        
        If $iSubItem > ($iColumnCount-1) Then
            $iIndex += 1
            $iSubItem = 0
        EndIf
        
        ;cleanup resources
        _GDIPlus_BitmapDispose($GDIpBmpLarge)
        If $GDIpBmpResized <> 0 Then
            _GDIPlus_BitmapDispose($GDIpBmpResized)
            $GDIpBmpResized = 0
        EndIf
        _WinAPI_DeleteObject($GDIbmp)
        
    Next
    
    Do
    Until GUIGetMsg() = $GUI_EVENT_CLOSE

    _GDIPlus_Shutdown()
    GUIDelete()
EndFunc   ;==>Example

 

Link to post
Share on other sites

Yes, You can custom or owner draw the images directly with GDI/GDI+ functions without relying on an imagelist.

A Virtual listview immediately supports the custom draw technique. To set the height of the rows in the listview to match the height of the images, you can probably add an imagelist with a single image of the correct height.

To use the owner draw method, you can combine the virtual ($LVS_OWNERDATA) and owner draw ($LVS_OWNERDRAWFIXED) styles. By replying to WM_MEASUREITEM messages it's easy to set the height of the listview rows.

Search forums for the topics and it should be possible to find code examples.

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
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...