Jump to content

is it possible to change the background color of a single line in guictrlcreatelist ?


Recommended Posts

if 1=1 then line color = green

if 1=2 then line color = red

i cant search for this information since the search doesnt allow the word color

you need an ownerdrawn listbox for that

search the forum for ownerdrawn constants like ODT_LISTBOX etc. for other examples

A roughly modified unoptimized Siao/Gary Frost example (not a UDF, needs error checking etc.) - adjust to your requirements

showing alternate row colours, custom per row text/back colours, custom select colour

other embellishments are custom fonts per item, DrawThemeText or DrawShadowText API instead of DrawText etc.

for custom coloured rows by item - set colours in WM_DRAWITEM msg handler with an array of items and colours

ListBox Set Selection Color Default is dark Blue

How to set icons for ListBox? Is it possible?

tested in XP, made change to ptr in tMEASUREITEMS and other funcs for ulong_ptr

moded for unicode and possibly? x64 (untested)

#AutoIt3Wrapper_AU3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6
 #include 
 #include 
 #include 
 #include 
 ;#Include 

 Opt("GUIOnEventMode", 1)
 Opt('MustDeclareVars', 1)


 Global Const $ODT_LISTBOX = 2
 Global Const $ODA_DRAWENTIRE = 0x1
 Global Const $ODA_FOCUS = 0x4
 Global Const $ODA_SELECT = 0x2
 Global Const $ODS_SELECTED = 0x1
 ;Global Const $DT_RIGHT = 0x2
 ;Global Const $DT_LEFT = 0x0

 Global $iDllGDI = DllOpen("gdi32.dll")
 Global $iDllUSER32 = DllOpen("user32.dll")

 Global $iListBkColor = 0x0000CC
 Global $iListSelColor = 0xFFFFFF

 Global $hGui = GUICreate("Ownerdrawn Listbox: text/background colour per row", 450, 200)
 GUISetOnEvent($GUI_EVENT_CLOSE, "SysEvents")

 GUIRegisterMsg($WM_MEASUREITEM, "MsgEvents")
 GUIRegisterMsg($WM_DRAWITEM, "MsgEvents")

 Global $cLBox1 = GUICtrlCreateList("", 10, 10, 430, 180, BitOR($LBS_SORT, $WS_BORDER, $WS_VSCROLL, $LBS_OWNERDRAWFIXED, $LBS_HASSTRINGS))
 GUICtrlSetData(-1, "1 An Ownerdrawn Listbox|1b|1c|1d|1e|1f|1g|1h|1i|1j|1k|1l|1m|1n|1o|1p|1q")
 GUICtrlSetBkColor(-1, $iListBkColor)
 GUICtrlSetFont(-1, 9, 400, -1, "Arial")

 ;set 3 arbitrary items text/bk colours
 Global $aCol[3][3] = [[2, 0xFFFFFF, 0xFF0000],[5, 0x000000, 0x00FF00],[7, 0x000000, 0xABCDEF]]

 GUISetState(@SW_SHOW)

 While 1
     Sleep(1000)
 WEnd

 Func SysEvents()
     Switch @GUI_CtrlId
         Case $GUI_EVENT_CLOSE
             DllClose($iDllGDI)
             DllClose($iDllUSER32)
             Exit
     EndSwitch
 EndFunc   ;==>SysEvents

 Func MsgEvents($hWnd, $Msg, $wParam, $lParam)
     #forceref $hWnd, $wParam
     Switch $Msg
         Case $WM_MEASUREITEM
             ;read MSDN http://msdn.microsoft.com - WM_MEASUREITEM and MEASUREITEMSTRUCT Structure
             If $wParam <> $cLBox1 Then Return $GUI_RUNDEFMSG ;run default msg if control is not $cLBox1
             Local $tMEASUREITEMS = DllStructCreate("uint cType;uint cID;uint itmID" & _
                     ";uint itmW;uint itmH;ulong_ptr itmData", $lParam)
             ;check that specified control is actually a listbox
             If DllStructGetData($tMEASUREITEMS, "cType") <> $ODT_LISTBOX Then Return $GUI_RUNDEFMSG
             DllStructSetData($tMEASUREITEMS, "itmH", 16);row height
             Return 1
         Case $WM_DRAWITEM
             ;read MSDN http://msdn.microsoft.com - WM_DRAWITEM and DRAWITEMSTRUCT Structure
             If $wParam <> $cLBox1 Then Return $GUI_RUNDEFMSG ;run default msg if control is not $cLBox1
             Local $tDRAWITEMS = DllStructCreate("uint cType;" & _
                     "uint cID;" & _
                     "uint itmID;" & _
                     "uint itmAction;" & _
                     "uint itmState;" & _
                     "hwnd hItm;" & _
                     "handle hDC;" & _
                     "dword itmRect[4];" & _
                     "ulong_ptr itmData", _
                     $lParam)

             ;check that specified control is actually a listbox
             If DllStructGetData($tDRAWITEMS, "cType") <> $ODT_LISTBOX Then Return $GUI_RUNDEFMSG
             ;Local $cID = DllStructGetData($tDRAWITEMS, "cID")
             Local $itmID = DllStructGetData($tDRAWITEMS, "itmID")
             Local $itmAction = DllStructGetData($tDRAWITEMS, "itmAction")
             Local $itmState = DllStructGetData($tDRAWITEMS, "itmState")
             Local $hItm = DllStructGetData($tDRAWITEMS, "hItm")
             Local $hDC = DllStructGetData($tDRAWITEMS, "hDC")
             Switch $itmAction
                 Case $ODA_DRAWENTIRE, $ODA_SELECT
                     Local $iBrushColor
                     Local $tRECT = DllStructCreate("int;int;int;int", DllStructGetPtr($tDRAWITEMS, "itmRect"))
                     ;select bkground color
                     If $itmState = $ODS_SELECTED Then
                         $iBrushColor = RGB2BGR($iListSelColor) ;selected background colour
                         ;selected text colour
                         DllCall($iDllGDI, "INT", "SetTextColor", "handle", $hDC, "dword", 0x0000FF)
                     Else
                         If Mod($itmID, 2) = 0 Then ;even/odd rows
                             $iBrushColor = RGB2BGR($iListBkColor);text background colour
                             ;text colour
                             DllCall($iDllGDI, "INT", "SetTextColor", "handle", $hDC, "dword", 0xFFFFFF)
                         Else
                             $iBrushColor = RGB2BGR(0xC0C0C0);text background colour
                             ;text colour
                             DllCall($iDllGDI, "INT", "SetTextColor", "handle", $hDC, "dword", 0x000000)
                         EndIf

                         For $i = 0 To UBound($aCol) - 1 Step 1
                             If $itmID = $aCol[$i][0] Then
                                 $iBrushColor = RGB2BGR($aCol[$i][2]);text background colour
                                 ;text colour
                                 DllCall($iDllGDI, "INT", "SetTextColor", "handle", $hDC, "dword", $aCol[$i][1])
                             EndIf
                         Next
                     EndIf

                     ;draw item bkground
                     Local $aBrush = DllCall($iDllGDI, "handle", "CreateSolidBrush", "int", $iBrushColor)
                     Local $aBrushOld = DllCall($iDllGDI, "handle", "SelectObject", "handle", $hDC, "handle", $aBrush[0])
                     DllCall($iDllUSER32, "int", "FillRect", "handle", $hDC, "long_ptr", DllStructGetPtr($tRECT), "handle", $aBrush[0])
                     DllCall($iDllGDI, "handle", "SelectObject", "handle", $hDC, "handle", $aBrushOld[0])
                     DllCall($iDllGDI, "int", "DeleteObject", "handle", $aBrush[0])
                     DllCall($iDllGDI, "int", "SetBkMode", "handle", $hDC, "int", 1)
                     ;get the text to be drawn

                     Local $aStringLen = DllCall($iDllUSER32, "int", "SendMessageW", "hwnd", $hItm, "uint", $LB_GETTEXTLEN, "wParam", $itmID, "lParam", 0)
                     If @error Then Return $GUI_RUNDEFMSG
                     $aStringLen = $aStringLen[0]
                     Local $tBuffer = DllStructCreate("wchar["&$aStringLen+1&"]") ;removed that evil 4096
                     DllCall($iDllUSER32, "int", "SendMessageW", "hwnd", $hItm, "uint", $LB_GETTEXT, "wParam", $itmID, "lParam", DllStructGetPtr($tBuffer))
                     If @error Then Return $GUI_RUNDEFMSG
                     Local $itmText = DllStructGetData($tBuffer, 1)
                     ;text indent
                     DllStructSetData($tRECT, 1, DllStructGetData($tRECT, 1) + 2)
                     DllStructSetData($tRECT, 3, DllStructGetData($tRECT, 3) - 2)
                     ;draw item text - see MSDN for formatting options - DrawText Function
                     ;"If uFormat includes DT_MODIFYSTRING, the function could add up to four additional characters
                     ;to this string. The buffer containing the string should be large enough to accommodate these
                     ;extra characters.
                     DllCall($iDllUSER32, "int", "DrawTextW", _
                             "handle", $hDC, _
                             "wstr", $itmText, "int", $aStringLen, _
                             "long_ptr", DllStructGetPtr($tRECT), _
                             "uint", $DT_LEFT);uFormat
                 Case $ODA_FOCUS
                 ;enable for focus rectangle
                 ;Return $GUI_RUNDEFMSG
             EndSwitch
             Return 1
     EndSwitch
 EndFunc   ;==>MsgEvents

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

I see fascists...

Link to comment
Share on other sites

  • Moderators

supadodger,

Searching (yes, that box at top right!) for "+list +colour +line" brought up this topic as the 5th line. :(

It is only a work-around, but might be of help to you. :graduated:

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

this forum is broken due to an Invsion IPBoard bug

preview a post and then it won't add the reply. I get "You must enter a post."

it's an Invision IPBoard bug

http://community.invisionpower.com/tracker/issue-25678-rte-message-you-must-enter-a-post/

#AutoIt3Wrapper_AU3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6


#include <GUIConstantsEx.au3>
#include <ListBoxConstants.au3>
#include <WindowsConstants.au3>
#include <GUIConstants.au3>
;#Include <GuiListBox.au3>

Opt("GUIOnEventMode", 1)
Opt('MustDeclareVars', 1)


Global Const $ODT_LISTBOX = 2
Global Const $ODA_DRAWENTIRE = 0x1
Global Const $ODA_FOCUS = 0x4
Global Const $ODA_SELECT = 0x2
Global Const $ODS_SELECTED = 0x1
;Global Const $DT_RIGHT = 0x2
;Global Const $DT_LEFT = 0x0

Global $iDllGDI = DllOpen("gdi32.dll")
Global $iDllUSER32 = DllOpen("user32.dll")

Global $iListBkColor = 0x0000CC
Global $iListSelColor = 0xFFFFFF

Global $hGui = GUICreate("Ownerdrawn Listbox: text/background colour per row", 450, 200)
GUISetOnEvent($GUI_EVENT_CLOSE, "SysEvents")

GUIRegisterMsg($WM_MEASUREITEM, "MsgEvents")
GUIRegisterMsg($WM_DRAWITEM, "MsgEvents")

Global $cLBox1 = GUICtrlCreateList("", 10, 10, 430, 180, BitOR($LBS_SORT, $WS_BORDER, $WS_VSCROLL, $LBS_OWNERDRAWFIXED, $LBS_HASSTRINGS))
GUICtrlSetData(-1, "1 An Ownerdrawn Listbox|1b|1c|1d|1e|1f|1g|1h|1i|1j|1k|1l|1m|1n|1o|1p|1q")
GUICtrlSetBkColor(-1, $iListBkColor)
GUICtrlSetFont(-1, 9, 400, -1, "Arial")

;set 3 arbitrary items text/bk colours
Global $aCol[3][3] = [[2, 0xFFFFFF, 0xFF0000],[5, 0x000000, 0x00FF00],[7, 0x000000, 0xABCDEF]]

GUISetState(@SW_SHOW)

While 1
    Sleep(1000)
WEnd

Func SysEvents()
    Switch @GUI_CtrlId
        Case $GUI_EVENT_CLOSE
            DllClose($iDllGDI)
            DllClose($iDllUSER32)
            Exit
    EndSwitch
EndFunc   ;==>SysEvents

Func MsgEvents($hWnd, $Msg, $wParam, $lParam)
    #forceref $hWnd, $wParam
    Switch $Msg
        Case $WM_MEASUREITEM
            ;read MSDN http://msdn.microsoft.com - WM_MEASUREITEM and MEASUREITEMSTRUCT Structure
            If $wParam <> $cLBox1 Then Return $GUI_RUNDEFMSG ;run default msg if control is not $cLBox1
            Local $tMEASUREITEMS = DllStructCreate("uint cType;uint cID;uint itmID" & _
                    ";uint itmW;uint itmH;ulong_ptr itmData", $lParam)
            ;check that specified control is actually a listbox
            If DllStructGetData($tMEASUREITEMS, "cType") <> $ODT_LISTBOX Then Return $GUI_RUNDEFMSG
            DllStructSetData($tMEASUREITEMS, "itmH", 16);row height
            Return 1
        Case $WM_DRAWITEM
            ;read MSDN http://msdn.microsoft.com - WM_DRAWITEM and DRAWITEMSTRUCT Structure
            If $wParam <> $cLBox1 Then Return $GUI_RUNDEFMSG ;run default msg if control is not $cLBox1
            Local $tDRAWITEMS = DllStructCreate("uint cType;" & _
                    "uint cID;" & _
                    "uint itmID;" & _
                    "uint itmAction;" & _
                    "uint itmState;" & _
                    "hwnd hItm;" & _
                    "handle hDC;" & _
                    "dword itmRect[4];" & _
                    "ulong_ptr itmData", _
                    $lParam)

            ;check that specified control is actually a listbox
            If DllStructGetData($tDRAWITEMS, "cType") <> $ODT_LISTBOX Then Return $GUI_RUNDEFMSG
            ;Local $cID = DllStructGetData($tDRAWITEMS, "cID")
            Local $itmID = DllStructGetData($tDRAWITEMS, "itmID")
            Local $itmAction = DllStructGetData($tDRAWITEMS, "itmAction")
            Local $itmState = DllStructGetData($tDRAWITEMS, "itmState")
            Local $hItm = DllStructGetData($tDRAWITEMS, "hItm")
            Local $hDC = DllStructGetData($tDRAWITEMS, "hDC")
            Switch $itmAction
                Case $ODA_DRAWENTIRE, $ODA_SELECT
                    Local $iBrushColor
                    Local $tRECT = DllStructCreate("int;int;int;int", DllStructGetPtr($tDRAWITEMS, "itmRect"))
                    ;select bkground color
                    If $itmState = $ODS_SELECTED Then
                        $iBrushColor = RGB2BGR($iListSelColor) ;selected background colour
                        ;selected text colour
                        DllCall($iDllGDI, "INT", "SetTextColor", "handle", $hDC, "dword", 0x0000FF)
                    Else
                        If Mod($itmID, 2) = 0 Then ;even/odd rows
                            $iBrushColor = RGB2BGR($iListBkColor);text background colour
                            ;text colour
                            DllCall($iDllGDI, "INT", "SetTextColor", "handle", $hDC, "dword", 0xFFFFFF)
                        Else
                            $iBrushColor = RGB2BGR(0xC0C0C0);text background colour
                            ;text colour
                            DllCall($iDllGDI, "INT", "SetTextColor", "handle", $hDC, "dword", 0x000000)
                        EndIf

                        For $i = 0 To UBound($aCol) - 1 Step 1
                            If $itmID = $aCol[$i][0] Then
                                $iBrushColor = RGB2BGR($aCol[$i][2]);text background colour
                                ;text colour
                                DllCall($iDllGDI, "INT", "SetTextColor", "handle", $hDC, "dword", $aCol[$i][1])
                            EndIf
                        Next
                    EndIf

                    ;draw item bkground
                    Local $aBrush = DllCall($iDllGDI, "handle", "CreateSolidBrush", "int", $iBrushColor)
                    Local $aBrushOld = DllCall($iDllGDI, "handle", "SelectObject", "handle", $hDC, "handle", $aBrush[0])
                    DllCall($iDllUSER32, "int", "FillRect", "handle", $hDC, "long_ptr", DllStructGetPtr($tRECT), "handle", $aBrush[0])
                    DllCall($iDllGDI, "handle", "SelectObject", "handle", $hDC, "handle", $aBrushOld[0])
                    DllCall($iDllGDI, "int", "DeleteObject", "handle", $aBrush[0])
                    DllCall($iDllGDI, "int", "SetBkMode", "handle", $hDC, "int", 1)
                    ;get the text to be drawn

                    Local $aStringLen = DllCall($iDllUSER32, "int", "SendMessageW", "hwnd", $hItm, "uint", $LB_GETTEXTLEN, "wParam", $itmID, "lParam", 0)
                    If @error Then Return $GUI_RUNDEFMSG
                    $aStringLen = $aStringLen[0]
                    Local $tBuffer = DllStructCreate("wchar["&$aStringLen+1&"]") ;removed that evil 4096
                    DllCall($iDllUSER32, "int", "SendMessageW", "hwnd", $hItm, "uint", $LB_GETTEXT, "wParam", $itmID, "lParam", DllStructGetPtr($tBuffer))
                    If @error Then Return $GUI_RUNDEFMSG
                    Local $itmText = DllStructGetData($tBuffer, 1)
                    ;text indent
                    DllStructSetData($tRECT, 1, DllStructGetData($tRECT, 1) + 2)
                    DllStructSetData($tRECT, 3, DllStructGetData($tRECT, 3) - 2)
                    ;draw item text - see MSDN for formatting options - DrawText Function
                    ;"If uFormat includes DT_MODIFYSTRING, the function could add up to four additional characters
                    ;to this string. The buffer containing the string should be large enough to accommodate these
                    ;extra characters.
                    DllCall($iDllUSER32, "int", "DrawTextW", _
                            "handle", $hDC, _
                            "wstr", $itmText, "int", $aStringLen, _
                            "long_ptr", DllStructGetPtr($tRECT), _
                            "uint", $DT_LEFT);uFormat
                Case $ODA_FOCUS
                ;enable for focus rectangle
                ;Return $GUI_RUNDEFMSG
            EndSwitch
            Return 1
    EndSwitch
EndFunc   ;==>MsgEvents

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

I see fascists...

Link to comment
Share on other sites

  • Moderators

rover,

Nice to see you too - it has been a while. :graduated:

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

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...