Jump to content

Listview Selected Row


marko001
 Share

Recommended Posts

Hi to all,

Hi to @Melba23 (waiting for your massive LV update with checkboxes fixed...)

I can't understand why I can get the status of a row. Mainly I would like to ask a confirmation to user before deleting the row, using _GUIListViewEx_Delete()

I use _GUIListViewEx_MsgRegister() before the While - Wend and $vRet = _GUIListViewEx_EventMonitor(0) at the end of EndSwitch sentences.

I retrieve n. of rows trough a Local $aItemList = _FileListToArray($ItemsDir) (then I clean it to my needs and set the $LvRows)

I have  a "Delete" button (similar to the one designed my @Melba23 into his demo .au3) that won't call immediately _GUIListViewEx_Delete() but does:

For $i = 0 To $LVRows
            ConsoleWrite("Item " & $i & " State: " & _GUICtrlListView_GetItemState($LV_Items, $I,  $LVIS_FOCUSED) & @CRLF)
            If _GUICtrlListView_GetItemFocused($LV_Items, $i) = True Then
                MsgBox(0, "", "Selected row we are going to delete: " & $i)
               ;_GUIListViewEx_Delete()
            EndIf
        Next

but I never can find the "clicked" <-- focused line.

What am I doing wrong?

Thanks all, guys

 

Link to comment
Share on other sites

It works as expected, problem is somewhere in your (not posted) code.

 

#include <GUIConstantsEx.au3>
#Include <GuiListView.au3>

GUICreate("My GUI", 400, 300)
$lv = GUICtrlCreateListView("col1|col2|col3  ", 10, 10, 200, 150)
GUICtrlCreateListViewItem("item1|col12|col13", $lv)
GUICtrlCreateListViewItem("item2|col22|col23", $lv)
GUICtrlCreateListViewItem("item3|col32|col33", $lv)
$btn = GUICtrlCreateButton("Selected item", 75, 170, 70, 20)
GUISetState(@SW_SHOW)

While 1
    $msg = GUIGetMsg()
    If $msg = $GUI_EVENT_CLOSE Then ExitLoop
    If $msg = $btn Then MsgBox(0, 'Selected item', _GUICtrlListView_GetNextItem($lv))
WEnd

 

Link to comment
Share on other sites

  • Moderators

marko001,

If you are using my GUIListViewEx UDF - which seems to be the case - then you need to use the _GUIListViewEx_GetLastSelItem to find out which item is currently selected in the ListView. This is because the UDF bypasses a lot of the internal Windows highlighting (because of the possibility of colouring elements) and so the standard calls do not work.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

I have a sample that I used a lot of time. 

 

No credit to me, someone shared it to me. And I can't remember from where. 

 

You probably gonna have to modify, may it can fit to your needs ? Sry if not.

 

$WMNotifyTest = 1

Func WM_NOTIFY($hWnd, $iMsg, $wParam, $lParam)
        
         If $WMNotifyTest = 1 Then
            ConsoleWrite ("; WMNotify () Is Working!!"&@CRLF)
                If $WMNotifyTest = 1 Then
                    $WMNotifyTest += 1
                EndIf
         EndIf
    #forceref $hWnd, $iMsg, $wParam
    Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndListView, $tInfo
    ; Local $tBuffer
    $hWndListView = $Console
    If Not IsHWnd($Console) Then $hWndListView = GUICtrlGetHandle($Console)

    $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
    $iCode = DllStructGetData($tNMHDR, "Code")
    Switch $hWndFrom
        Case $hWndListView
            Switch $iCode
                Case $NM_CLICK ; Sent by a list-view control when the user clicks an item with the left mouse button
                    $tInfo = DllStructCreate($tagNMITEMACTIVATE, $lParam)
                    _DebugPrint("$NM_CLICK" & @CRLF & _
                            $TextFav = _GUICtrlListView_GetItemText($Console, DllStructGetData($tInfo, "Index")) & _
                            "--> hWndFrom:" & @TAB & $hWndFrom & @CRLF & _
                            "-->IDFrom:" & @TAB & $iIDFrom & @CRLF & _
                            "-->Code:" & @TAB & $iCode & @CRLF & _
                            "-->Index:" & @TAB & DllStructGetData($tInfo, "Index") & @CRLF & _
                            "-->SubItem:" & @TAB & DllStructGetData($tInfo, "SubItem") & @CRLF & _
                            "-->NewState:" & @TAB & DllStructGetData($tInfo, "NewState") & @CRLF & _
                            "-->OldState:" & @TAB & DllStructGetData($tInfo, "OldState") & @CRLF & _
                            "-->Changed:" & @TAB & DllStructGetData($tInfo, "Changed") & @CRLF & _
                            "-->ActionX:" & @TAB & DllStructGetData($tInfo, "ActionX") & @CRLF & _
                            "-->ActionY:" & @TAB & DllStructGetData($tInfo, "ActionY") & @CRLF & _
                            "-->lParam:" & @TAB & DllStructGetData($tInfo, "lParam") & @CRLF & _
                            "-->KeyFlags:" & @TAB & DllStructGetData($tInfo, "KeyFlags"))
                    ; No return value
                Case $NM_DBLCLK ; Sent by a list-view control when the user double-clicks an item with the left mouse button
                    $tInfo = DllStructCreate($tagNMITEMACTIVATE, $lParam)
                    ClipPut(_GUICtrlListView_GetItemText($Console, DllStructGetData($tInfo, "Index")))
                    _DebugPrint("$NM_DBLCLK" & @CRLF & "--> hWndFrom:" & @TAB & $hWndFrom & @CRLF & _
                            "-->IDFrom:" & @TAB & $iIDFrom & @CRLF & _
                            "-->Code:" & @TAB & $iCode & @CRLF & _
                            "-->Index:" & @TAB & DllStructGetData($tInfo, "Index") & @CRLF & _
                            "-->SubItem:" & @TAB & DllStructGetData($tInfo, "SubItem") & @CRLF & _
                            "-->NewState:" & @TAB & DllStructGetData($tInfo, "NewState") & @CRLF & _
                            "-->OldState:" & @TAB & DllStructGetData($tInfo, "OldState") & @CRLF & _
                            "-->Changed:" & @TAB & DllStructGetData($tInfo, "Changed") & @CRLF & _
                            "-->ActionX:" & @TAB & DllStructGetData($tInfo, "ActionX") & @CRLF & _
                            "-->ActionY:" & @TAB & DllStructGetData($tInfo, "ActionY") & @CRLF & _
                            "-->lParam:" & @TAB & DllStructGetData($tInfo, "lParam") & @CRLF & _
                            "-->KeyFlags:" & @TAB & DllStructGetData($tInfo, "KeyFlags"))
                    ; No return value
                Case $NM_RDBLCLK ; Sent by a list-view control when the user double-clicks an item with the right mouse button
                    $tInfo = DllStructCreate($tagNMITEMACTIVATE, $lParam)
                    _GUICtrlListView_DeleteItem($Console, DllStructGetData($tInfo, "Index"))

                    _DebugPrint("$NM_RDBLCLK" & @CRLF & "--> hWndFrom:" & @TAB & $hWndFrom & @CRLF & _
                            "-->IDFrom:" & @TAB & $iIDFrom & @CRLF & _
                            "-->Code:" & @TAB & $iCode & @CRLF & _
                            "-->Index:" & @TAB & DllStructGetData($tInfo, "Index") & @CRLF & _
                            "-->SubItem:" & @TAB & DllStructGetData($tInfo, "SubItem") & @CRLF & _
                            "-->NewState:" & @TAB & DllStructGetData($tInfo, "NewState") & @CRLF & _
                            "-->OldState:" & @TAB & DllStructGetData($tInfo, "OldState") & @CRLF & _
                            "-->Changed:" & @TAB & DllStructGetData($tInfo, "Changed") & @CRLF & _
                            "-->ActionX:" & @TAB & DllStructGetData($tInfo, "ActionX") & @CRLF & _
                            "-->ActionY:" & @TAB & DllStructGetData($tInfo, "ActionY") & @CRLF & _
                            "-->lParam:" & @TAB & DllStructGetData($tInfo, "lParam") & @CRLF & _
                            "-->KeyFlags:" & @TAB & DllStructGetData($tInfo, "KeyFlags"))
                    ; No return value
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY

 

The purpose of this sample is to do things when you double-left-click or double-right-click or left-click on list view items. 

Edited by caramen

My video tutorials : ( In construction )  || My Discord : https://discord.gg/S9AnwHw

How to Ask Help ||  UIAutomation From Junkew || WebDriver From Danp2 || And Water's UDFs in the Quote

Spoiler

 Water's UDFs:
Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Link to comment
Share on other sites

  • Moderators

marko001,

Quote

Bingo

Glad I could help!

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

@Melba23 @Zedna question is still open... anyway.

This is my LV filling Function:

Func _fillLV()
    If DirGetSize($Files) = 0 Then ;
        _send("No files, can't load archive", $red)
        Return
    EndIf
    $aItemList = _FileListToArray($Files)
    If Not @error Then
        _ArrayDelete($aItemList, 0)
        Local $thumb = _ArraySearch($aItemList, "Thumbs.db")
        If Not @error Then _ArrayDelete($aItemList, $thumb)
        $hImage = _GUIImageList_Create(50, 50)
        _GDIPlus_Startup()
        ;add images
        For $i = 0 To UBound($aItemList) - 1
            $GDIpBmpLarge = _GDIPlus_ImageLoadFromFile($Files & "\" & $aItemList[$i]) ;GDI+ image!
            $GDIpBmpResized = _GDIPlus_ImageResize($GDIpBmpLarge, 50, 50) ;GDI+ image
            $GDIbmp = _GDIPlus_BitmapCreateHBITMAPFromBitmap($GDIpBmpResized) ;GDI image!
            _GUIImageList_Add($hImage, $GDIbmp)
        Next
        ;add file name
        For $i = 0 To UBound($aItemList) - 1
            _GUICtrlListView_AddItem($LV_Items, StringTrimRight($aItemList[$i], 4), $i)
        Next
        _GUICtrlListView_SetImageList($LV_Items, $hImage, 1)
        _GDIPlus_BitmapDispose($GDIpBmpLarge)
        _GDIPlus_BitmapDispose($GDIpBmpResized)
        _WinAPI_DeleteObject($GDIbmp)
        _GDIPlus_Shutdown()

        _send("Archive loaded", $blue)
        $LVRows = UBound($aItemList) - 1
    Else
        _send("Error: " & @error, $red)
    EndIf
EndFunc   ;==>_fillLV

So I have images (.jpg) and text (filename without extension)

My delete function is:

Func _deleteItem()
    Local $aSelected
    Local $sFile2Delete
    Local $sSelected = _GUIListViewEx_GetLastSelItem($LV_Items) ; col|row|cel
    If $sSelected = "" Then
        _send("No Item selected or no item available", $red)
        Return
    Else
        $aSelected = StringSplit($sSelected, "|", 2)
        $sFile2Delete = $aItemList[$aSelected[1]]
        $answ = MsgBox(36, "File delete", "Are you sure to delete " & StringUpper(StringTrimRight($sFile2Delete, 4)) & "?")
        If $answ = 7 Then ; NO
            _send("Nothing deleted", $blue)
        Else
;~          _GUIImageList_Destroy($hImage)
            FileDelete($Files & "\" & $sFile2Delete)
            _send("Removed selected file", $blue)
            _GUICtrlListView_DeleteAllItems($LV_Trash)
            _fillLVTrashItems() ; to redraw it
        EndIf
    EndIf
EndFunc   ;==>_deleteItem

I use this workaround because the _GUIListViewEx_Delete() virtually delete the rows but not phisically the file.

But I can't delete it because file is hooked, don't know if by GDIImage  or LV but also in Explorer I can't phisically delete it: "File is used by AutoIt".

Hints?

Thanks,

Edited by marko001
Link to comment
Share on other sites

Hi marko001 :)

As you noticed, image files are always locked after you use _GDIPlus_ImageLoadFromFile(), that's why they should be released asap via _GDIPlus_ImageDispose() and each opened image should be disposed (in your case, within the For... Next loop)

It's not because you re-use the same variable $GDIpBmpLarge that things will be different. Consider the following code (badly scripted on purpose)

; Load 2 images
$hImage = _GDIPlus_ImageLoadFromFile(@ScriptDir & "\Image1.jpg")
...
$hImage = _GDIPlus_ImageLoadFromFile(@ScriptDir & "\Image2.jpg")
...

; Clean up resources
_GDIPlus_ImageDispose($hImage)

Sleep(20000) ; 20s

During these 20s, you will be able to delete Image2.jpg (from Explorer) because _GDIPlus_ImageDispose() released it ($hImage corresponds to the last opened image) . But you won't be able to delete Image1.jpg because it will be locked (tested)

I guess that if you transfer those 3 lines at the end of your For... Next loop, everything should work fine :

_GDIPlus_BitmapDispose($GDIpBmpLarge)
_GDIPlus_BitmapDispose($GDIpBmpResized)
_WinAPI_DeleteObject($GDIbmp)
Edited by pixelsearch
typo
Link to comment
Share on other sites

@pixelsearch didn't work:

For $i = 0 To UBound($aItemList) - 1
            $GDIpBmpLarge = _GDIPlus_ImageLoadFromFile($Files & "\" & $aItemList[$i]) ;GDI+ image!
            $GDIpBmpResized = _GDIPlus_ImageResize($GDIpBmpLarge, 50, 50) ;GDI+ image
            $GDIbmp = _GDIPlus_BitmapCreateHBITMAPFromBitmap($GDIpBmpResized) ;GDI image!
            _GUIImageList_Add($hImage, $GDIbmp)
        Next
        _GDIPlus_BitmapDispose($GDIpBmpLarge)
        _GDIPlus_BitmapDispose($GDIpBmpResized)
        _WinAPI_DeleteObject($GDIbmp)

files are still hooked and I can't delete them by AutoIt neither Explorer

Link to comment
Share on other sites

Hi Marko001,
When I wrote "(in your case, within the For... Next loop)" and "at the end of your For... Next loop" I meant :

For $i = 0 To UBound($aItemList) - 1
    $GDIpBmpLarge = _GDIPlus_ImageLoadFromFile($Files & "\" & $aItemList[$i]) ;GDI+ image!
    $GDIpBmpResized = _GDIPlus_ImageResize($GDIpBmpLarge, 50, 50) ;GDI+ image
    $GDIbmp = _GDIPlus_BitmapCreateHBITMAPFromBitmap($GDIpBmpResized) ;GDI image!
    _GUIImageList_Add($hImage, $GDIbmp)

    _GDIPlus_BitmapDispose($GDIpBmpLarge)
    _GDIPlus_BitmapDispose($GDIpBmpResized)
    _WinAPI_DeleteObject($GDIbmp)
Next

Could you please try it that way and let us know ?

Link to comment
Share on other sites

@pixelsearch wasn't sure about inserting that code inside the loop but YES it works now.

As in my code I can now ask my customer confirmation before deleting the file. 

If he accepts the file is deleted BUT listview is not updated.

I use 

_GUIListViewEx_Close($LV_Items)
            _GUICtrlListView_DeleteAllItems($LV_Items)

then recall _fillLV()

Func _fillLV()
    If DirGetSize($Files) = 0 Then ;
        _send("No files, can't load archive", $red)
        Return
    EndIf
    $aItemList = _FileListToArray($Files)
    If Not @error Then
        _ArrayDelete($aItemList, 0)
        Local $thumb = _ArraySearch($aItemList, "Thumbs.db")
        If Not @error Then _ArrayDelete($aItemList, $thumb)
        $hImage = _GUIImageList_Create(50, 50)
        _GDIPlus_Startup()
        ;add images
        For $i = 0 To UBound($aItemList) - 1
            $GDIpBmpLarge = _GDIPlus_ImageLoadFromFile($Files & "\" & $aItemList[$i]) ;GDI+ image!
            $GDIpBmpResized = _GDIPlus_ImageResize($GDIpBmpLarge, 50, 50) ;GDI+ image
            $GDIbmp = _GDIPlus_BitmapCreateHBITMAPFromBitmap($GDIpBmpResized) ;GDI image!
            _GUIImageList_Add($hImage, $GDIbmp)
        Next
        ;add file name
        For $i = 0 To UBound($aItemList) - 1
            _GUICtrlListView_AddItem($LV_Items, StringTrimRight($aItemList[$i], 4), $i)
        Next
        _GUICtrlListView_SetImageList($LV_Items, $hImage, 1)
        _GDIPlus_BitmapDispose($GDIpBmpLarge)
        _GDIPlus_BitmapDispose($GDIpBmpResized)
        _WinAPI_DeleteObject($GDIbmp)
        _GDIPlus_Shutdown()

        _send("Archive loaded", $blue)
        $LVRows = UBound($aItemList) - 1
    Else
        _send("Error: " & @error, $red)
    EndIf
EndFunc   ;==>_fillLV

but LV is not updated

Link to comment
Share on other sites

@Melba23 and this should be the last, I almost fixed everything:

Problem is: I can't reload correctly LV. IT just clear images but doesn't reload.

Here is the code:

Basically, I ask confirmation for deletion of a line (1 line = 1 file). If Yes I first delete the file then reload completely the LV in order to get new thumbnails and filenames

Func _deleteItem()
    Local $aSelected
    Local $sFile2Delete
    Local $sSelected = _GUIListViewEx_GetLastSelItem($LV_Files) ; col|row|cel
    If $sSelected = "" Then
        _send("No file selected or no file available", $red)
        Return
    Else
        $aSelected = StringSplit($sSelected, "|", 2)
        $sFile2Delete = $aItemList[$aSelected[1]]
        $answ = MsgBox(36, "File removal", "Are you sure to remove " & StringUpper(StringTrimRight($sFile2Delete, 4)) & "?")
        If $answ = 7 Then ; NO
            _send("Nothing deleted", $blue)
        Else
            FileDelete($FileDir & "\" & $sFile2Delete)
            _ClearLV()
            _fillLV()
            $LV_Files = _GUIListViewEx_Init($LV_Files, "", 0, 0xFF0000, True, 1 + 4 + 8)
            _send("Removed selected line from Item List and deleted file", $blue)
        EndIf
    EndIf
EndFunc   ;==>_deleteItem

Here the Func to Clear LV:

Func _ClearLV()
    $answ = _GUICtrlListView_DeleteAllItems($LV_Files)
    ; msgbox(0,"",$answ)
    $answ = _GUIListViewEx_Close($LV_Files)
    ; msgbox(0,"",$answ)
    ; Destroy and recreate ImageList
    _GUIImageList_Destroy($hImage)
    $hImage = _GUIImageList_Create(50, 50)
EndFunc

When I call _deleteItem() the file is physically deleted (thanks @pixelsearch) then I see no changes: If I use mousewheel I see images disappearead (but I DO have to move bar or they stand) but filenames remains and are not reloaded 

And here is the function to fill LV, _fillLV()

Func _fillLV()
    If DirGetSize($FileDir) = 0 Then ;
        _send("No files, can't load archive", $red)
        Return
    EndIf
    $aItemList = _FileListToArray($FileDir)
    If Not @error Then
        _ArrayDelete($aItemList, 0)
        Local $thumb = _ArraySearch($aItemList, "Thumbs.db")
        If Not @error Then _ArrayDelete($aItemList, $thumb)
        $hImage = _GUIImageList_Create(50, 50)
        _GDIPlus_Startup()
        ;add images
        For $i = 0 To UBound($aItemList) - 1
            $GDIpBmpLarge = _GDIPlus_ImageLoadFromFile($FileDir & "\" & $aItemList[$i]) ;GDI+ image!
            $GDIpBmpResized = _GDIPlus_ImageResize($GDIpBmpLarge, 50, 50) ;GDI+ image
            $GDIbmp = _GDIPlus_BitmapCreateHBITMAPFromBitmap($GDIpBmpResized) ;GDI image!
            _GUIImageList_Add($hImage, $GDIbmp)
            _GDIPlus_BitmapDispose($GDIpBmpLarge)
            _GDIPlus_BitmapDispose($GDIpBmpResized)
            _WinAPI_DeleteObject($GDIbmp)
        Next
        ;add file name
        For $i = 0 To UBound($aItemList) - 1
            _GUICtrlListView_AddItem($LV_Files, StringTrimRight($aItemList[$i], 4), $i)
        Next
        _GUICtrlListView_SetImageList($LV_Files, $hImage, 1)
        _GDIPlus_Shutdown()

        _send("Files loaded", $blue)
        $LVRows = UBound($aItemList) - 1
    Else
        _send("Error: " & @error, $red)
    EndIf
EndFunc   ;==>_fillLV

Thanks!

Edited by marko001
Link to comment
Share on other sites

Hi Marko001
Why not sharing the whole script ?

Then it would be much easier for Melba23 to help you (when he has some free time) especially it's related to
GUIListViewEx. Also anyone who is able to help could give you a hand. But that requires to work on the whole script.

It's not necessary to share any image as the helper(s) can easily find 4-5 pics on their own computer and place them in the $FileDir directory

In case the script is too big to be pasted, just attach the .au3 to your post.

Edited by pixelsearch
Link to comment
Share on other sites

@pixelsearch @Melba23 here is the code of the specific part , problem still remains.

Just put some .jpg files into the folder and run it.

File is deleted (consolewrite) but doesn't refresh.

When (manually with Explorer) a file is added doesn't refresh

#include <MsgBoxConstants.au3>
#include <AutoItConstants.au3>
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <ComboConstants.au3>
#include <File.au3>
#include <String.au3>
#include <GuiRichEdit.au3>
#include <ExtMsgBox.au3>
#include <Misc.au3>
#include <Constants.au3>
#include <GDIPlus.au3>
#include <GUIListViewEx.au3>

Global $Filedir = @ScriptDir & "\FileItems"
Global $LVRows = 0
Global $aItemList, $hImage

#Region ### START Koda GUI section ### File CONFIG
$FRM_File = GUICreate("File Config", 410, 289, 674, 168)
$LV_Files = GUICtrlCreateListView("", 8, 32, 299, 214, $GUI_SS_DEFAULT_LISTVIEW, BitOR($LVS_EX_FULLROWSELECT, $LVS_EX_GRIDLINES, $LVS_EX_SUBITEMIMAGES))
_GUICtrlListView_AddColumn($LV_Files, "File Items", 250)
_GUICtrlListView_SetTextBkColor($LV_Files, 0xDDFFDD)
$B_FileInsert = GUICtrlCreateButton("Add File", 320, 32, 75, 25)
$B_FileDelete = GUICtrlCreateButton("Delete File", 320, 64, 75, 25)
$LBL_FileItems = GUICtrlCreateLabel("Files & Thumbnails", 8, 8, 150, 17)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

If DirGetSize($Filedir) < 0 Then DirCreate($Filedir)
_FillLV()

$LV_Files = _GUIListViewEx_Init($LV_Files, "", 0, 0xFF0000, True, 1 + 4 + 8)
_GUIListViewEx_MsgRegister()

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $B_FileDelete
            _deleteItem()
        Case $B_FileInsert
            _InsertItem()
    EndSwitch
    $vRet = _GUIListViewEx_EventMonitor(0) ; Use combos to change EditMode
    If @error Then
        MsgBox($MB_SYSTEMMODAL, "Error", "Event error: " & @error)
    EndIf
WEnd

Func _FillLV()
    If DirGetSize($Filedir) = 0 Then ;
        ConsoleWrite("No items to File in inventory, add them before enabling it" & @CRLF)
        Return
    EndIf
    $aItemList = _FileListToArray($Filedir)
    If Not @error Then
        _ArrayDelete($aItemList, 0)
        Local $thumb = _ArraySearch($aItemList, "Thumbs.db")
        If Not @error Then _ArrayDelete($aItemList, $thumb)
        $hImage = _GUIImageList_Create(50, 50)
        _GDIPlus_Startup()
        ;add images
        For $i = 0 To UBound($aItemList) - 1
            $GDIpBmpLarge = _GDIPlus_ImageLoadFromFile($Filedir & "\" & $aItemList[$i]) ;GDI+ image!
            $GDIpBmpResized = _GDIPlus_ImageResize($GDIpBmpLarge, 50, 50) ;GDI+ image
            $GDIbmp = _GDIPlus_BitmapCreateHBITMAPFromBitmap($GDIpBmpResized) ;GDI image!
            _GUIImageList_Add($hImage, $GDIbmp)
            _GDIPlus_BitmapDispose($GDIpBmpLarge)
            _GDIPlus_BitmapDispose($GDIpBmpResized)
            _WinAPI_DeleteObject($GDIbmp)
        Next
        ;add file name
        For $i = 0 To UBound($aItemList) - 1
            _GUICtrlListView_AddItem($LV_Files, StringTrimRight($aItemList[$i], 4), $i)
        Next
        _GUICtrlListView_SetImageList($LV_Files, $hImage, 1)
        _GDIPlus_Shutdown()

        ConsoleWrite("File items loaded" & @CRLF)
        $LVRows = UBound($aItemList) - 1
    Else
        ConsoleWrite("Error: " & @error & @CRLF)
    EndIf
EndFunc   ;==>_FillLV

Func _deleteItem()
    Local $aSelected
    Local $sFile2Delete
    Local $sSelected = _GUIListViewEx_GetLastSelItem($LV_Files) ; col|row|cel
    If $sSelected = "" Then
        ConsoleWrite("No Item selected or no item available" & @CRLF)
        Return
    Else
        $aSelected = StringSplit($sSelected, "|", 2)
        If @error Then Return
        $sFile2Delete = $aItemList[$aSelected[1]]
        $answ = MsgBox(36, "Item removal", "Are you sure to remove " & StringUpper(StringTrimRight($sFile2Delete, 4)) & "?")
        If $answ = 7 Then ; NO
            ConsoleWrite("Nothing deleted" & @CRLF)
        Else
            FileDelete($Filedir & "\" & $sFile2Delete)
            ConsoleWrite("Removed selected line from Item List" & @CRLF)
            _ClearLV()
            _FillLV()
            $LV_Files = _GUIListViewEx_Init($LV_Files, "", 0, 0xFF0000, True, 1 + 4 + 8)

        EndIf
    EndIf
EndFunc   ;==>_deleteItem

Func _InsertItem()
    MsgBox(0, "", "For test purpose, just add any .jpg to folder and go on. If it works it should reload LV with file name and image")
    _ClearLV()
    _GUIImageList_Destroy($hImage)
    $hImage = _GUIImageList_Create(50, 50)
    _FillLV()
    $LV_Files = _GUIListViewEx_Init($LV_Files, "", 0, 0xFF0000, True, 1 + 4 + 8)
    _GUIListViewEx_MsgRegister()
EndFunc   ;==>_InsertItem

Func _ClearLV()
    $answ = _GUICtrlListView_DeleteAllItems($LV_Files)
    $answ = _GUIListViewEx_Close($LV_Files)
    ; Destroy and recreate ImageList
    _GUIImageList_Destroy($hImage)
    $hImage = _GUIImageList_Create(50, 50)
EndFunc   ;==>_ClearLV

 

Link to comment
Share on other sites

The listview refresh works if you solve this problem in your variables :

; $LV_Files is the control ID of the listview : correct
$LV_Files = GUICtrlCreateListView(...)

; $LV_Files is now the "Index number of ListView for use in other GUIListViewEx functions" : incorrect
$LV_Files = _GUIListViewEx_Init($LV_Files, "", 0, 0xFF0000, True, 1 + 4 + 8)

You cannot use the same variable for both because the ListView id is wrong now (it was = 3, now = 1) . To solve this, just create another variable, named for example $LV_FilesEx, changing 5 lines in the script :

$LV_FilesEx = _GUIListViewEx_Init($LV_Files, "", 0, 0xFF0000, True, 1 + 4 + 8) ; change THREE times
...
Local $sSelected = _GUIListViewEx_GetLastSelItem($LV_FilesEx) ; col|row|cel
...
$answ = _GUIListViewEx_Close($LV_FilesEx)

Then you'll have a correct refresh after any click on "delete file" or "add file"

Edited by pixelsearch
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...