maniootek

Combine ListViewItem with multi images and bg color

16 posts in this topic

I can't figure out how make a ListViewItem with bgcolor and multi images. Here's example:

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

Example()

Func Example()
    Local $hImage, $idListview

    ; Create GUI
    GUICreate("ListView Add SubItem", 400, 300)
    $idListview = GUICtrlCreateListView("", 2, 2, 394, 268, -1, BitOR($LVS_EX_FULLROWSELECT, $LVS_EX_SUBITEMIMAGES))
    GUISetState(@SW_SHOW)

    ; Load images
    $hImage = _GUIImageList_Create()
    _GUIImageList_Add($hImage, _GUICtrlListView_CreateSolidBitMap(GUICtrlGetHandle($idListview), 0xFF0000, 16, 16))
    _GUIImageList_Add($hImage, _GUICtrlListView_CreateSolidBitMap(GUICtrlGetHandle($idListview), 0x00FF00, 16, 16))
    _GUIImageList_Add($hImage, _GUICtrlListView_CreateSolidBitMap(GUICtrlGetHandle($idListview), 0x0000FF, 16, 16))
    _GUICtrlListView_SetImageList($idListview, $hImage, 1)

    ; Add columns
    _GUICtrlListView_InsertColumn($idListview, 0, "Column 1", 200)
    _GUICtrlListView_InsertColumn($idListview, 1, "Column 2", 100)
    _GUICtrlListView_InsertColumn($idListview, 2, "Column 3", 100)

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

    $handle = GUICtrlCreateListViewItem("here i am able to add bg color", $idListview) ;but I can't set image
    GUICtrlSetBkColor($handle, 0xFF0000)
    _GUICtrlListView_AddItem($idListview, "Here i don't know how", 0)

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

 

Share this post


Link to post
Share on other sites



_GUICtrlListView_SetItemImage expects the ItemIndex of the LV-Item: 

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

Example()

Func Example()
    Local $hImage, $idListview

    ; Create GUI
    $hGui=GUICreate("ListView Add SubItem", 400, 300)
    $idListview = GUICtrlCreateListView("", 2, 2, 394, 268, -1, BitOR($LVS_EX_FULLROWSELECT, $LVS_EX_SUBITEMIMAGES))
    GUISetState(@SW_SHOW)

    ; Load images
    $hImage = _GUIImageList_Create()
    _GUIImageList_Add($hImage, _GUICtrlListView_CreateSolidBitMap(GUICtrlGetHandle($idListview), 0xFF0000, 16, 16))
    _GUIImageList_Add($hImage, _GUICtrlListView_CreateSolidBitMap(GUICtrlGetHandle($idListview), 0x00FF00, 16, 16))
    _GUIImageList_Add($hImage, _GUICtrlListView_CreateSolidBitMap(GUICtrlGetHandle($idListview), 0x0000FF, 16, 16))
    _GUICtrlListView_SetImageList($idListview, $hImage, 1)

    ; Add columns
    _GUICtrlListView_InsertColumn($idListview, 0, "Column 1", 200)
    _GUICtrlListView_InsertColumn($idListview, 1, "Column 2", 100)
    _GUICtrlListView_InsertColumn($idListview, 2, "Column 3", 100)

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

    $idLVItem = GUICtrlCreateListViewItem("here i am able to add|bg color|Image", $idListview) ;but I can't set image
    GUICtrlSetBkColor($idLVItem, 0xFF0000)
    ;$hLVItem=ControlGetHandle($hGui,'',$idLVItem)
    _GUICtrlListView_SetItemImage($idListview,3,1)
    _GUICtrlListView_SetItemImage($idListview,3,1,1)
    _GUICtrlListView_SetItemImage($idListview,3,2,2)

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

 

Share this post


Link to post
Share on other sites

thank you, didn't know / couldn't find that function.

Share this post


Link to post
Share on other sites

One more question. Is it possible to change selecting style? By default it is blue (when it's active) and grey (when it's not active) background. It is also put "small squares" on the image when it's selected - I want to avoid this. Any chance to disable selecting on blue and make it only "marked" (border with dotted line)?

listview.png.0b16466fe46cc581f3e88dbcd97

Share this post


Link to post
Share on other sites

There's this thread that can highlight the items you want even if you use $LVS_EX_FULLROWSELECT as the EX style of the listview. I'm working on a downloader using a listview with a progress bar and planned on dissecting that post to make it select the row but not the progress bitmap. I'm still working on the actual downloading part so I haven't started yet, so if you figure out a the solution let us know :P

Share this post


Link to post
Share on other sites

It's not that hard to remove the selected state of the focused item:

#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiImageList.au3>
#include <GuiListView.au3>

Opt( "MustDeclareVars", 1 )

Global $hListview

Example()

Func Example()
    ; Create GUI
    Local $hGui=GUICreate("ListView Add SubItem", 400, 300)
    Local $idListview = GUICtrlCreateListView("", 2, 2, 394, 268, -1, BitOR($LVS_EX_FULLROWSELECT, $LVS_EX_SUBITEMIMAGES))
    $hListview = GUICtrlGetHandle( $idListview )
    GUISetState(@SW_SHOW)

    ; Load images
    Local $hImage = _GUIImageList_Create()
    _GUIImageList_Add($hImage, _GUICtrlListView_CreateSolidBitMap(GUICtrlGetHandle($idListview), 0xFF0000, 16, 16))
    _GUIImageList_Add($hImage, _GUICtrlListView_CreateSolidBitMap(GUICtrlGetHandle($idListview), 0x00FF00, 16, 16))
    _GUIImageList_Add($hImage, _GUICtrlListView_CreateSolidBitMap(GUICtrlGetHandle($idListview), 0x0000FF, 16, 16))
    _GUICtrlListView_SetImageList($idListview, $hImage, 1)

    ; Add columns
    _GUICtrlListView_InsertColumn($idListview, 0, "Column 1", 200)
    _GUICtrlListView_InsertColumn($idListview, 1, "Column 2", 100)
    _GUICtrlListView_InsertColumn($idListview, 2, "Column 3", 100)

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

    Local $idLVItem = GUICtrlCreateListViewItem("here i am able to add|bg color|Image", $idListview) ;but I can't set image
    GUICtrlSetBkColor($idLVItem, 0xFF0000)
    ;$hLVItem=ControlGetHandle($hGui,'',$idLVItem)
    _GUICtrlListView_SetItemImage($idListview,3,1)
    _GUICtrlListView_SetItemImage($idListview,3,1,1)
    _GUICtrlListView_SetItemImage($idListview,3,2,2)

    ; Register WM_NOTIFY message handler
    GUIRegisterMsg( $WM_NOTIFY, "WM_NOTIFY" )

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

Func WM_NOTIFY($hWnd, $iMsg, $wParam, $lParam)
  #forceref $hWnd, $iMsg, $wParam
  Local $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
  Local $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
  Local $iCode = DllStructGetData($tNMHDR, "Code")
  Switch $hWndFrom
    Case $hListview
      Switch $iCode
        Case $LVN_ITEMCHANGED
          Local $tNMListView = DllStructCreate( $tagNMLISTVIEW, $lParam )
          Local $iNewState = DllStructGetData( $tNMListView, "NewState" )
          If BitAND( $iNewState, $LVIS_FOCUSED ) Then
            Local $iLvItem = DllStructGetData( $tNMListView, "Item" )
            _GUICtrlListView_SetItemSelected( $hListview, $iLvItem, False )
          EndIf
      EndSwitch
  EndSwitch
  Return $GUI_RUNDEFMSG
EndFunc

 

It is possible to get the red background rectangles to fit perfectly with the size of the images but it requires significantly more code (custom draw of each single subitem in the post paint stage and adjusting the size and position of the background rectangles).

Share this post


Link to post
Share on other sites

Thank you for you code. This is working as I expected. I just don't know why this script stop working when I just add button? You can try to change this line:

    $hListview = GUICtrlGetHandle( $idListview )
    GUISetState(@SW_SHOW)

to this line:

$hListview = GUICtrlGetHandle( $idListview )
    Local $button = GUICtrlCreateButton("Button", 5, 275, 100, 20)
    GUISetState(@SW_SHOW)

Share this post


Link to post
Share on other sites

I don' know what you added exactyly, but adding a button to the GUI is easy:

#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiImageList.au3>
#include <GuiListView.au3>

Opt("MustDeclareVars", 1)

Global $hListview

Example()

Func Example()
    ; Create GUI
    Local $iMsg, $sText, $aItem
    Local $hGui = GUICreate("ListView Add SubItem", 400, 320)
    Local $idListview = GUICtrlCreateListView("", 2, 2, 394, 268, -1, BitOR($LVS_EX_FULLROWSELECT, $LVS_EX_SUBITEMIMAGES))
    $hListview = GUICtrlGetHandle($idListview)
    Local $idButton = GUICtrlCreateButton('Text', 2, 285, 394, 25)
    GUISetState(@SW_SHOW)

    ; Load images
    Local $hImage = _GUIImageList_Create()
    _GUIImageList_Add($hImage, _GUICtrlListView_CreateSolidBitMap(GUICtrlGetHandle($idListview), 0xFF0000, 16, 16))
    _GUIImageList_Add($hImage, _GUICtrlListView_CreateSolidBitMap(GUICtrlGetHandle($idListview), 0x00FF00, 16, 16))
    _GUIImageList_Add($hImage, _GUICtrlListView_CreateSolidBitMap(GUICtrlGetHandle($idListview), 0x0000FF, 16, 16))
    _GUICtrlListView_SetImageList($idListview, $hImage, 1)

    ; Add columns
    _GUICtrlListView_InsertColumn($idListview, 0, "Column 1", 200)
    _GUICtrlListView_InsertColumn($idListview, 1, "Column 2", 100)
    _GUICtrlListView_InsertColumn($idListview, 2, "Column 3", 100)

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

    Local $idLVItem = GUICtrlCreateListViewItem("here i am able to add|bg color|Image", $idListview) ;but I can't set image
    GUICtrlSetBkColor($idLVItem, 0xFF0000)
    ;$hLVItem=ControlGetHandle($hGui,'',$idLVItem)
    _GUICtrlListView_SetItemImage($idListview, 3, 1)
    _GUICtrlListView_SetItemImage($idListview, 3, 1, 1)
    _GUICtrlListView_SetItemImage($idListview, 3, 2, 2)

    ; Register WM_NOTIFY message handler
    GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")

    ; Loop until the user exits.
    While 1
        $iMsg = GUIGetMsg()
        Switch $iMsg
            Case $GUI_EVENT_CLOSE
                ExitLoop
            Case $idButton
                $aItem = _GUICtrlListView_GetItemTextArray($hListview)
                If $aItem[0] > 0 Then $sText = $aItem[1]
                If StringStripWS($sText,8)="" Then $sText = 'No Item selected'&@crlf&@crlf&'Tray again!'
                MsgBox(0, 'LV-Item selected:', $sText)
                GUIRegisterMsg($WM_NOTIFY,'')
        EndSwitch
    WEnd
    GUIDelete()
EndFunc   ;==>Example

Func WM_NOTIFY($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg, $wParam
    Local $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
    Local $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    Local $iCode = DllStructGetData($tNMHDR, "Code")
    Switch $hWndFrom
        Case $hListview
            Switch $iCode
                Case $LVN_ITEMCHANGED
                    Local $tNMListView = DllStructCreate($tagNMLISTVIEW, $lParam)
                    Local $iNewState = DllStructGetData($tNMListView, "NewState")
                    If BitAND($iNewState, $LVIS_FOCUSED) Then
                        Local $iLvItem = DllStructGetData($tNMListView, "Item")
                        _GUICtrlListView_SetItemSelected($hListview, $iLvItem, False)
                    EndIf
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY

testing this script will also show you, what you are never able to do when deselect the selected item. Therefore i just deregister the Notify - func if Text button fails to get the text of selected LV-Item. After this the Text button works the LV has it's old behaviour and i don't think there i a easy way to change color of selected item without loosing the comfort to get info's about select item(s).

Share this post


Link to post
Share on other sites

This movie should exaplain my problem. I just added one line to @LarsJ code example (add button to gui) and selecting stop working. When I tried @AutoBert code then selecting does not work either. 

 

Share this post


Link to post
Share on other sites

Have you tried my example? I can't look your video, i have only 64kBit/s so video is broken.

Share this post


Link to post
Share on other sites

When I run your code and click on any ListView Item it does not make any selection. When I click on the button and click OK on MsgBox then selecting start working, but with default style (blue bg color)

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

I have also tried your code from post #2 by adding button to GUI and dotted border of selected item is also disappear. This looks like some bug in Autoit. My autoit version is v3.3.14.2 

AutoIt3Wrapper v.16.306.1237.0 SciTE v.3.6.2.0

Windows 7 64-bit

Edited by maniootek

Share this post


Link to post
Share on other sites
16 minutes ago, maniootek said:

then selecting start working, but with default style (blue bg color)

Yes,

1 hour ago, AutoBert said:

After this the Text button works the LV has it's old behaviour and i don't think there i a easy way to change color of selected item without loosing the comfort to get info's about select item(s).

 

Share this post


Link to post
Share on other sites

I had to study the video carefully before I figured out what's wrong. I can see that you are running the script from SciTE by clicking Go in the Tools menu with the mouse. And that's the problem. If you press F5 on the keyboard to run the script there is no problem.

It's the same in Windows Explorer. If you start the au3-file with a double click with the mouse, you'll see the problem. If you start the script by selecting the file and pressing the Enter key there is no problem.

It has nothing to do with my code. Add the button to the code in post 2 by AutoBert, and you'll see the exact same issue. The dotted focus rectangle is missing when the script is started with the mouse. And it's visible when the script is started with the keyboard. It's easy to see around the icons.

I'm on Win 7 64 bit too, but still on 3.3.10.2. Same issue. Same issue on my old XP.

I've tried to create the button with _GUICtrlButton_Create instead of the native command. Same issue.

I've tried to add the extended listview styles with _GUICtrlListView_SetExtendedListViewStyle (which is the proper way to do it). Same issue.

I don't think there is anything in the code which is directly responsible for the problem. It's just a funny weird little issue. There is probably not much to do about it. But now when you know the problem you can work around it. And that's what you should do.

Share this post


Link to post
Share on other sites
15 minutes ago, LarsJ said:

I can see that you are running the script from SciTE by clicking Go in the Tools menu with the mouse. And that's the problem. If you press F5 on the keyboard to run the script there is no problem.

For both ways I have same problem no matter if I press F5 or if I click "Go" in Tools from SciTE.

Normally I always press F5 when I am working with SciTE to run the script but only for movie purposed I decied to "show" what I am doing by clicking on mouse instead of using shortcuts on keyboard.

Is there any way to report a bug?

Share this post


Link to post
Share on other sites

You can report a bug in the Bug Tracker. Don't expect a quick fix, maybe no solution at all. You can also choose to show the selected row in a more standard way, and not just with the dotted focus rectangle. It seems not to be working, when more controls are added to the GUI (it works consistently for me if I just start the script with the keyboard). The purpose of the dotted rectangle is to show the focused state. The selected state is normally shown with the dark blue background.

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