Jump to content
Sign in to follow this  

Color ListView row built with UDF

Recommended Posts


Hello all,

I have built a GUI using UDFs that displays a ListView with select services and specific information. The ListView has 4 elements in each row and has a Checkbox which may be either checked or unchecked based on information at that moment. I've been asked to also color the rows, based on certain information ('red' means this, 'green' means that, etc..) to allow better visual indication to the user.

I've built this ListView with all _GUICtrlListView.... functions which allowed me (as best I could determine) to dynamically create 4 columns, with checkboxes, with 'pre-sized' widths, and is sortable.

I'm trying find a way to preserve all the current functionallity and checkboxes, and also be able to color individual rows (the entire row, not just 1 column of a row, not just an image bitmap, etc.)

I've tried to rewrite the code below with standard 'GUICtrlCreateListView' type functions and it seems I loose some of the functionallity/ flexibility.

I'm getting the impressions after reading Melba23's post that this may not be so simple with UDFs:

I've looked at BugFix's great UDF, but this only appears to do a 'cell', not an entire row.

#include <Array.au3>
#include <String.au3>
#include <GUIConstantsEx.au3>
#include <GuiListView.au3>
#include <ListViewConstants.au3>
#include <WindowsConstants.au3>
#include <ColorConstants.au3>
#include <Constants.au3>

Global $bTop, $bSize, $hGUI, $hLabel1, $hLabel1font, $hLabel2, $hLabel2font, $hListView
Global $Button_1, $Button_2, $Button_3, $Button_4, $Button_5
$bTop = 320                 ; - Button constants.
$bSize = 100

Global $hGUI = GUICreate("Modify Services", 570, 350, -1, -1, -1, $WS_EX_TOPMOST)
Global $hLabel1 = GUICtrlCreateLabel("Select Services to 'Enable' or 'Disable' and click 'Apply'.", 10, 6, 540)
Global $hLabel1font = GUICtrlSetFont(-1, 9, 600)
Global $hLabel2 = GUICtrlCreateLabel("(The 'Reload' button with refresh the list of Services in their current state.)", 10, 22, 540)
Global $hLabel2font = GUICtrlSetFont(-1, 9, 400)
Global $hListView = _GUICtrlListView_Create($hGUI, "", 10, 40, 550, 275, $LVS_REPORT, $WS_EX_CLIENTEDGE)
_GUICtrlListView_SetExtendedListViewStyle($hListView, $LVS_EX_CHECKBOXES)
_GUICtrlListView_InsertColumn($hListView, 0, "Service", 200)
_GUICtrlListView_InsertColumn($hListView, 1, "Current State", 100)
_GUICtrlListView_InsertColumn($hListView, 2, "Optimized State", 100)
_GUICtrlListView_InsertColumn($hListView, 3, "Dependent Services", 200)

; - Buttons
$Button_4 = GUICtrlCreateButton("Quit", 480, $bTop, 80)
    GUICtrlSetState(-1, $GUI_DEFBUTTON)
$Button_5 = GUICtrlCreateButton("Reload", 320, $bTop, 80)
$Button_1 = GUICtrlCreateButton("Apply", 400, $bTop, 80)
$Button_2 = GUICtrlCreateButton("Enable All", 10, $bTop, $bSize)
$Button_3 = GUICtrlCreateButton("Disable All", 110, $bTop, $bSize)

; ------------------------------------------------------------------------------------------------
; - $runArray (2d) is populated here with service names and information.
; > This supplies 4 of the pieces of information displayed in the ListView.<
Global $runArray[1][8], $z = 0
; - Stuff happens to populate $runArray...
; ------------------------------------------------------------------------------------------------

; - Populate the ListView and Check chechboxes based on the value of $runArray[$z][7]
For $z = 0 To UBound($runArray) - 1
    _GUICtrlListView_AddItem($hListView, $runArray[$z][0], -1, $z)
    _GUICtrlListView_AddSubItem($hListView, $z, $runArray[$z][1], 1)
    _GUICtrlListView_AddSubItem($hListView, $z, $runArray[$z][2], 2)
    _GUICtrlListView_AddSubItem($hListView, $z, $runArray[$z][3], 3)

    If $runArray[$z][7] = True Then
        _GUICtrlListView_SetItemChecked($hListView, $z, $runArray[$z][7])       ; - Sets items to 'Checked' or 'Unchecked'.

Any thoughts on if coloring a row is do-able, or if there is another way to get at this?


Share this post

Link to post
Share on other sites

Here a example based on BugFix (http://www.autoit.de/index.php?page=Thread&postID=173432#post173432)

;Original von BugFix http://www.autoit.de/index.php?page=Thread&postID=173432#post173432
#include <GUIConstantsEx.au3>
#include <GuiListView.au3>
#include <array.au3>
#include <ListViewConstants.au3>
#include <StructureConstants.au3>
#include <WindowsConstants.au3>

$aCol_Colors = IniReadSection(@ScriptDir & "\LV_Format.ini", "COLs")
$aROW_Colors = IniReadSection(@ScriptDir & "\LV_Format.ini", "ROWs")
_ArrayDelete($aCol_Colors, 0)
_ArrayDelete($aROW_Colors, 0)

$GUI = GUICreate("Listview Farbig", 1024, 300, 0, 0)
$cListView = GUICtrlCreateListView("", 2, 2, 1020, 294, -1, BitOR($LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT))
$hListView = GUICtrlGetHandle($cListView)
For $i = 1 To 31
    _GUICtrlListView_InsertColumn($hListView, $i - 1, $i, 25)
    _GUICtrlListView_SetColumnWidth($hListView, $i - 1, 32)
For $i = 0 To 21 ; alle Item/SubItem erstellen
    _GUICtrlListView_AddItem($hListView, "", $i)
    For $j = 0 To 30
        _GUICtrlListView_AddSubItem($hListView, $i, "", $j)


While True
    $msg = GUIGetMsg()
    Switch $msg
        Case -3

Func WM_NOTIFY($hWnd, $msg, $wParam, $lParam)
    Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR
    $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
    $iCode = DllStructGetData($tNMHDR, "Code")
    Switch $hWndFrom
        Case $hListView
            Switch $iCode
                Case $NM_CUSTOMDRAW
                    Local $tCustDraw = DllStructCreate($tagNMLVCUSTOMDRAW, $lParam)
                    Local $iDrawStage, $iItem, $iSubitem, $hDC, $tRect
                    $iDrawStage = DllStructGetData($tCustDraw, 'dwDrawStage')
                    Switch $iDrawStage
                        Case $CDDS_ITEMPREPAINT
                            Return $CDRF_NOTIFYSUBITEMDRAW
                        Case BitOR($CDDS_ITEMPREPAINT, $CDDS_SUBITEM)
                            ; Item/SubItem das aktuell gezeichnet werden soll ermitteln
                            $iItem = DllStructGetData($tCustDraw, 'dwItemSpec')
                            $iSubitem = DllStructGetData($tCustDraw, 'iSubItem')
                            Switch $iItem       ; Rows
                                Case -1
                                Case Else
                                    $iIndex = _ArraySearch($aROW_Colors, $iItem, 0, 0, 0, 0, 1, 0)
                                    ConsoleWrite($iIndex & @CRLF)
                                    If $iIndex <> -1 Then
                                        DllStructSetData($tCustDraw, 'clrTextBk', RGB2BGR($aROW_Colors[$iIndex][1]))
                                        DllStructSetData($tCustDraw, 'clrTextBk', RGB2BGR(0xFFFFFF))
                            Switch $iSubitem    ;Cols
                                Case -1
                                Case Else
                                    $iIndex = _ArraySearch($aCol_Colors, $iSubitem, 0, 0, 0, 0, 1, 0)
                                    ConsoleWrite($iIndex & @CRLF)
                                    If $iIndex <> -1 Then DllStructSetData($tCustDraw, 'clrTextBk', RGB2BGR($aCol_Colors[$iIndex][1]))
                            Return $CDRF_NEWFONT
EndFunc   ;==>WM_NOTIFY

Func RGB2BGR($iColor)
    Local $sH = Hex($iColor, 6)
    Return '0x' & StringRight($sH, 2) & StringMid($sH, 3, 2) & StringLeft($sH, 2)
EndFunc   ;==>RGB2BGR
To get different Colors you have to create a Inifile @ScriptDir & "\LV_Format.ini" Here an example-ini: LV_FormatINI.txt (you have to rename it)

mfg autoBert

Share this post

Link to post
Share on other sites

autoBert - This is very helpful! I will need to spend some time discecting and applying it, but it in my initial testing, it appears that this will meet my needs! Sorry for the delay in responding; I was working on another task.

I need to move forward to complete coding - without row coloring - in this version to meet a release date, but I hope to come back to this next week.

Thank you very much,

Kind Regards


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