Jump to content

Imagelist Add Image to specific row and column


Surya
 Share

Recommended Posts

Hi guys,😇

I need to add image to a specific cell inside an imagelist how can i do so

From example of the guictrlimagelist_addsubitem https://www.autoitscript.com/autoit3/docs/libfunctions/_GUICtrlListView_SetItemImage.htm i found out that you can only go by order in filling the cells ,can i set an image to a specific cell like we normally do in an array

No matter whatever the challenge maybe control on the outcome its on you its always have been.

MY UDF: Transpond UDF (Sent vriables to Programs) , Utter UDF (Speech Recognition)

Link to comment
Share on other sites

I don't know what is the problem but nothing seems to show in column 2. I have pictures(jpg) named 1-1.jpg,1-2.jpg,2-1.jpg the 1-1.jpg should go in 1st column 1st row 2-1.jpg should go in 2nd column 1st row likewise, but it seems nothing is loading. I am STUCK!😢  Here is the code I am using:

 

#include <String.au3>
#include <Array.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>
#include <File.au3>
#include <GuiImageList.au3>
#include <GDIPlus.au3>

Global $sSource, $aImgURL, $sKeyWord
Local $GDIpBmpLarge, $GDIpBmpResized, $GDIbmp


$hGUI = GUICreate("Imagelist", 540, 500, -1, -1, $WS_MAXIMIZEBOX) ;, BitOR($WS_SIZEBOX, $WS_SYSMENU))
;GUICtrlSetResizing(-1, $GUI_DOCKLEFT + $GUI_DOCKRIGHT + $GUI_DOCKTOP)

;create listviews
$idListview = GUICtrlCreateListView("", 15, 150, 250, 300, BitOR($LVS_SHOWSELALWAYS, $LVS_NOSORTHEADER, $LVS_REPORT))
_GUICtrlListView_SetExtendedListViewStyle($idListview, BitOR($LVS_EX_FULLROWSELECT, $LVS_EX_GRIDLINES, $LVS_EX_DOUBLEBUFFER))
$hImage = _GUIImageList_Create(100, 100, 5, 3)
_GUICtrlListView_SetImageList($idListview, $hImage)

; Add columns
_GUICtrlListView_InsertColumn($idListview, 0, "Column1", 200)
_GUICtrlListView_InsertColumn($idListview, 1, "Column2", 200)
_GUICtrlListView_InsertColumn($idListview, 2, "Column3", 200)
_GUICtrlListView_InsertColumn($idListview, 3, "Column4", 200)
_GUICtrlListView_InsertColumn($idListview, 4, "Column5", 200)
_GUICtrlListView_InsertColumn($idListview, 5, "Column6", 200)

;show everything
GUISetState(@SW_MAXIMIZE)
GUISetState(@SW_SHOW)
_loadpic()

Func _loadpic()
    _GUIImageList_Remove($hImage) ;Remove everything
    $aFileList = _FileListToArray(@ScriptDir & "", "*.jpg", Default, True)
    _ArrayDisplay($aFileList)
    _GUICtrlListView_SetImageList($idListview, $hImage, 1)
    Local $arylist[0]
    For $i = 1 To $aFileList[0]
        Local $sDrive = "", $sDir = "", $sFileName = "", $sExtension = ""
        Local $aPathSplit = _PathSplit($aFileList[$i], $sDrive, $sDir, $sFileName, $sExtension)
        $GDIpBmpLarge = _GDIPlus_ImageLoadFromFile($aFileList[$i]) ;GDI+ image!
        $GDIpBmpResized = _GDIPlus_ImageResize($GDIpBmpLarge, 100, 100) ;GDI+ image
        $GDIbmp = _GDIPlus_BitmapCreateHBITMAPFromBitmap($GDIpBmpResized) ;GDI image!
        $ind = _GUIImageList_Add($hImage, $GDIbmp)
        $out = _GUICtrlListView_AddItem($idListview,$sFileName,$ind)
        _ArrayAdd($arylist,$sFileName &"-" &$out )

    Next
    _ArrayDisplay($arylist)

    For $i = 0 To UBound($arylist)-1
        $strout = _StringExplode($arylist[$i], "-")
        If UBound($strout) - 1 > 0 Then
        _GUICtrlListView_SetItemImage($idListview,1, 0, 1)
        ConsoleWrite(@error)
        EndIf
    Next
EndFunc   ;==>_loadpic


While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            ExitLoop

    EndSwitch
WEnd

; Delete the previous GUIs and all controls.
GUIDelete($hGUI)
_GDIPlus_BitmapDispose($GDIpBmpLarge)
_GDIPlus_BitmapDispose($GDIpBmpResized)
_WinAPI_DeleteObject($GDIbmp)
_GDIPlus_Shutdown()

 

 

resources.zip

Edited by Surya
Removed includes

No matter whatever the challenge maybe control on the outcome its on you its always have been.

MY UDF: Transpond UDF (Sent vriables to Programs) , Utter UDF (Speech Recognition)

Link to comment
Share on other sites

Here one way :

#include <GUIConstantsEx.au3>
#include <GuiImageList.au3>
#include <GuiListView.au3>
#include <MsgBoxConstants.au3>
#include <GDIPlus.au3>
#include <File.au3>

Example()

Func Example()
  Local $hImage, $idListview, $iStylesEx = BitOR($LVS_EX_FULLROWSELECT, $LVS_EX_SUBITEMIMAGES)

  GUICreate("ListView Set Item Image", 400, 300)
  $idListview = GUICtrlCreateListView("", 2, 2, 394, 268)
  _GUICtrlListView_SetExtendedListViewStyle($idListview, $iStylesEx)
  GUISetState(@SW_SHOW)

  ; Load images
  $hImage = _GUIImageList_Create()
  _LoadPic($hImage)
  _GUICtrlListView_SetImageList($idListview, $hImage, 1)

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

  ; Add items
  _GUICtrlListView_AddItem($idListview, "Row 1: Col 1", 0)
  _GUICtrlListView_AddSubItem($idListview, 0, "Row 1: Col 2", 1, 1)
  _GUICtrlListView_AddItem($idListview, "Row 2: Col 1", 2)
  _GUICtrlListView_AddSubItem($idListview, 1, "Row 2: Col 2", 1, 3)

  ; Loop until the user exits.
  Do
  Until GUIGetMsg() = $GUI_EVENT_CLOSE
  GUIDelete()
EndFunc   ;==>Example

Func _LoadPic(ByRef $hImage)
  _GDIPlus_Startup()
  Local $aFileList = _FileListToArray(@ScriptDir, "*.jpg", Default, True)
  Local $sDrive = "", $sDir = "", $sFileName = "", $sExtension = ""
  Local $GDIpBmpLarge, $GDIpBmpResized, $GDIbmp

  For $i = 1 To $aFileList[0]
    _PathSplit($aFileList[$i], $sDrive, $sDir, $sFileName, $sExtension)
    $GDIpBmpLarge = _GDIPlus_ImageLoadFromFile($aFileList[$i])     ;GDI+ image!
    $GDIpBmpResized = _GDIPlus_ImageResize($GDIpBmpLarge, 16, 16)     ;GDI+ image
    $GDIbmp = _GDIPlus_BitmapCreateHBITMAPFromBitmap($GDIpBmpResized)     ;GDI image!
    _GUIImageList_Add($hImage, $GDIbmp)
    _GDIPlus_BitmapDispose($GDIpBmpLarge)
    _GDIPlus_BitmapDispose($GDIpBmpResized)
    _WinAPI_DeleteObject($GDIbmp)
  Next
  _GDIPlus_Shutdown()
EndFunc   ;==>_loadpic

Notice the disposal and deletion of images after each usage...

Edited by Nine
Link to comment
Share on other sites

thankyou @Nine but what if there are more than 4 images?

And this is filling row-wise I want 1-1,1-2,1-3 to be in the same column Is it possible☺️

Edited by Surya

No matter whatever the challenge maybe control on the outcome its on you its always have been.

MY UDF: Transpond UDF (Sent vriables to Programs) , Utter UDF (Speech Recognition)

Link to comment
Share on other sites

Yes of course, you need to create items first then subitems accordingly.  You could even read the file name, register its position (1-2, 2-1, 3-2, etc.) and set image based on it.

Edited by Nine
Link to comment
Share on other sites

Can u add 

_GUICtrlListView_AddItem($idListview, "Row 1: Col 1", 0)
_GUICtrlListView_AddSubItem($idListview, 0, "Row 1: Col 2", 1, 1)
_GUICtrlListView_AddItem($idListview, "Row 2: Col 1", 2)
_GUICtrlListView_AddSubItem($idListview, 1, "Row 2: Col 2", 1, 3)

This segment in a for loop? ie.from where is the images being added to the list,is there an array or something that establishes a relation b/w the images and their indexes 

Edited by Surya

No matter whatever the challenge maybe control on the outcome its on you its always have been.

MY UDF: Transpond UDF (Sent vriables to Programs) , Utter UDF (Speech Recognition)

Link to comment
Share on other sites

Personally, I wouldn't mix the image list creation with the listView items creation.  Those are 2 very different tasks that it should not be combined into a single function.  That is not a good programming practice.  But when you create the image list, you could register the name of the files into an Array and use that Array to create Listview items and subitems along with their images.

Link to comment
Share on other sites

I found the solution

#include <GUIConstantsEx.au3>
#include <GuiImageList.au3>
#include <GuiListView.au3>
#include <MsgBoxConstants.au3>
#include <GDIPlus.au3>
#include <string.au3>
#include <File.au3>

Local $hImage, $idListview, $iStylesEx = BitOR($LVS_EX_FULLROWSELECT, $LVS_EX_SUBITEMIMAGES)

GUICreate("ListView Set Item Image", 400, 300)
$idListview = GUICtrlCreateListView("", 2, 2, 394, 268)
_GUICtrlListView_SetExtendedListViewStyle($idListview, $iStylesEx)
GUISetState(@SW_SHOW)

; Load images
$hImage = _GUIImageList_Create(100, 100)
$indx = _LoadPic($hImage)

 ; Add columns
_GUICtrlListView_AddColumn($idListview, "Column 1", 100)
_GUICtrlListView_AddColumn($idListview, "Column 2", 100)
_GUICtrlListView_AddColumn($idListview, "Column 3", 100)
_GUICtrlListView_AddColumn($idListview, "Column 4", 100)
_GUICtrlListView_AddColumn($idListview, "Column 5", 100)
_GUICtrlListView_AddColumn($idListview, "Column 6", 100)

; Add items;
;column selecter
_SetPic($indx)

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

Func _SetPic($indx)
_GUICtrlListView_SetImageList($idListview, $hImage, 1)
$maxind = _StringExplode($indx[UBound($indx) - 1], ":")
  _ArrayDisplay($maxind)
  For $int = 0 To $maxind[0]
     $sclmn = _ArraySearch($indx, ":" &$int &"-", 0, 0, 0, 1, 1)
     If Not @error Then
      $imageidcl = _StringBetween($indx[$sclmn],"",":")[0]
      _GUICtrlListView_AddItem($idListview, "Row 1: Col 1", $imageidcl)
      For $kint = 0 To $maxind[1]
            $srow = _ArraySearch($indx,$int &"-" &$kint, 0, 0, 0, 1, 1)
            If Not @error Then
            $imageidrw = _StringBetween($indx[$srow],"",":")[0]
            _GUICtrlListView_AddSubItem($idListview, $int, "Row 1: Col 2", $kint,$imageidrw)
            MsgBox(Default,Default,"INT:"&$int &" --KINT" &$kint &" --SROW" &$srow &"--SCLMN" &$sclmn)
            EndIf
      Next
   EndIf
  Next
EndFunc

Func _LoadPic(ByRef $hImage)
   _GDIPlus_Startup()
   Local $ary[0]
   Local $aFileList = _FileListToArray(@ScriptDir & "\Resources\", "*.jpg", Default, True)
   Local $sDrive = "", $sDir = "", $sFileName = "", $sExtension = ""
   Local $GDIpBmpLarge, $GDIpBmpResized, $GDIbmp, $maxcol = 0, $maxrow = 0

   For $i = 1 To $aFileList[0]
    _PathSplit($aFileList[$i], $sDrive, $sDir, $sFileName, $sExtension)
    $indices = _StringExplode($sFileName, "-")
    If Not @error And UBound($indices) - 1 > 0 Then
      $GDIpBmpLarge = _GDIPlus_ImageLoadFromFile($aFileList[$i])     ;GDI+ image!
      $GDIpBmpResized = _GDIPlus_ImageResize($GDIpBmpLarge, 100, 100)     ;GDI+ image
      $GDIbmp = _GDIPlus_BitmapCreateHBITMAPFromBitmap($GDIpBmpResized)     ;GDI image!
      _GUIImageList_Add($hImage, $GDIbmp)

      _ArrayAdd($ary, $i - 1 & ":" & $sFileName)
     If $maxcol < $indices[0] Then $maxcol = $indices[0]
     If $maxrow < $indices[1] Then $maxrow = $indices[1]

     _GDIPlus_BitmapDispose($GDIpBmpLarge)
     _GDIPlus_BitmapDispose($GDIpBmpResized)
     _WinAPI_DeleteObject($GDIbmp)
      EndIf
    Next
    _ArrayAdd($ary, $maxcol & ":" & $maxrow)
    Return $ary
EndFunc   ;==>_LoadPic

 

No matter whatever the challenge maybe control on the outcome its on you its always have been.

MY UDF: Transpond UDF (Sent vriables to Programs) , Utter UDF (Speech Recognition)

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...