Jump to content

Listview Item / Subitem Background Colour

Recommended Posts


Never factored that I have multiple Listviews in my gui.  This tries to apply the colours to listviews which are different and subsequently crashes.

Tried to first check if the correct tab was active in WM_NOTIFY, but the WM_NOTIFY function appears to be actioned before the tab appears and I can get the text from it.


Never mind, answer >here.

Just need ti implement it in my code.

Edited by JohnOne

Share this post

Link to post
Share on other sites

Sorry, I know this is old thread but as I am trying out @LarsJ code so asking here.


Is there any other way to color ListView specific cell if logic is true. Without using DllStruct.
I don’t understand DllStructucture logic properly & in my case Array size is no to big [30][15].
So can it be done using GUICtrlSetBkColor or _GUICtrlListView_CreateSolidBitMap?

Edited by jugador

Share this post

Link to post
Share on other sites

The custom draw technique is the great, right, flexible and fast way to apply colors in a listview. Read about DllStruct in the help file and just ask more questions if you have any doubts.

Share this post

Link to post
Share on other sites


ok I get it how to color listview but
No I didnt get it how DllStruct work 😂.

I use @mikell code to understand how it work.

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

Global $myitem = -1, $mysubitem = -1

$Form1 = GUICreate("Form1", 350, 250, -1, -1)
$ListView1 = GUICtrlCreateListView("Header|Header|Header", 0, 0, 350, 200)

$btn = GuiCtrlCreateButton('button', 20, 210, 50, 25)

$iRows = 3
$iCols = 3
Global $aColors[$iRows][$iCols]

For $i = 0 To $iRows - 1
    For $j = 0 To $iCols - 1
        If $i = 1 Then
            If $j = 1 Then
                $aColors[$i][$j] = RGB2BGR(0x00aa00)  ; green
                $aColors[$i][$j] = RGB2BGR(0xffaaff)  ; pink
            $aColors[$i][$j] = RGB2BGR(0xFFFFFF)  ; white


While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
        Case $btn
            $aColors[1][1] = RGB2BGR(0xFF0000)  ; red
            _GUICtrlListView_SetItemText($ListView1, 1, "Red", 1)

Func WM_NOTIFY($hWnd, $Msg, $wParam, $lParam)
     Local $tNMHDR, $hWndFrom, $iCode
     $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
     $hWndFrom = DllStructGetData($tNMHDR, "hWndFrom")
     $iCode = DllStructGetData($tNMHDR, "Code")
     $hListView = GuiCtrlGetHandle($ListView1)

    Switch $hWndFrom
         Case $hListView
             Switch $iCode
               Case $NM_CUSTOMDRAW
                     Local $tCustDraw = DllStructCreate($tagNMLVCUSTOMDRAW, $lParam)
                     Local $iDrawStage = DllStructGetData($tCustDraw, "dwDrawStage")
                     If $iDrawStage = $CDDS_PREPAINT Then Return $CDRF_NOTIFYITEMDRAW
                     If $iDrawStage = $CDDS_ITEMPREPAINT Then Return $CDRF_NOTIFYSUBITEMDRAW
                     Local $iSubItem = DllStructGetData($tCustDraw, "iSubItem")
                     Local $iItem = DllStructGetData($tCustDraw, "dwItemSpec")
                    DllStructSetData($tCustDraw, "clrTextBk", $aColors[$iItem][$iSubItem]) ; background color
                    Return $CDRF_NEWFONT
 EndFunc   ;==>WM_NOTIFY

Func RGB2BGR($iColor)
     Return BitAND(BitShift(String(Binary($iColor)), 8), 0xFFFFFF)
EndFunc   ;==>RGB2BGR()


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

  • Create New...