gcue

Listview Change Subitem Bk Font Colors

7 posts in this topic

Hello World!

I have found several examples of how to change background and font colors in the forums.  Unfortunately they all use WM_NOTIFY which handles click events.  I am looking for something where I can specify upon creation of a listview item.  So if conditions are met for the item i am adding I change the colors of each subitem (ie: green for condition met and red for opposite)

I've tried BugFix's version 1.3 and lbsl's version 1.4 - but doesnt seem to work with current version of autoit

Any help is greatly appreciated!

 

Share this post


Link to post
Share on other sites



Hello. You can do something like this.

 

#include <GUIConstantsEx.au3>
#include <GuiImageList.au3>
#include <GuiListView.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>
Global $oListView=0
Example()

Func Example()
    Local $hImage

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


   GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")

  GUISetState(@SW_SHOW)

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

    ; Add items
    _GUICtrlListView_AddItem($oListView, "Danyfirex")
    _GUICtrlListView_AddSubItem($oListView, 0, "gcue", 1)
    _GUICtrlListView_AddSubItem($oListView, 0, "Danyfirex", 2)
    _GUICtrlListView_AddItem($oListView, "gcue")
    _GUICtrlListView_AddSubItem($oListView, 1, "Danyfirex", 1)
    _GUICtrlListView_AddItem($oListView, "AutoIt")

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


Func WM_NOTIFY($hWnd, $Msg, $wParam, $lParam)
    Local $tNMHDR, $hWndFrom, $iCode
    $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
    $hWndFrom = DllStructGetData($tNMHDR, "hWndFrom")
    $iCode = DllStructGetData($tNMHDR, "Code")
$g_hListView=GUICtrlGetHandle($oListView)
    Switch $hWndFrom
        Case $g_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")
                    Local $iColor = RGB2BGR(0xffffff)

                        If _GUICtrlListView_GetItemText($g_hListView, $iItem, $iSubItem) = "Danyfirex" Then
                            $iColor = RGB2BGR(0x00ff00)
                        EndIf
                        If _GUICtrlListView_GetItemText($g_hListView, $iItem, $iSubItem) = "Autoit" Then
                            $iColor = RGB2BGR(0xFF8282)
                        EndIf
                        If _GUICtrlListView_GetItemText($g_hListView, $iItem, $iSubItem) = "gcue" Then
                            $iColor = RGB2BGR(0x00ffff)
                        EndIf

                    DllStructSetData($tCustDraw, "clrTextBk", $iColor)
                    Return $CDRF_NEWFONT

            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY

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

Saludos

Share this post


Link to post
Share on other sites

looks great - thank youuuu!

:drool:

Share this post


Link to post
Share on other sites

how do i change the color of specific subitems?

thanks again for your help

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

Opt("GUIOnEventMode", 1)

Global $oListView = 0


Local $hImage

; Create GUI
GUICreate("ListView Add SubItem", 400, 300)
$oListView = GUICtrlCreateListView("Col 1|Col 2|Col 3", 2, 2, 394, 268, -1, BitOR($LVS_EX_FULLROWSELECT, $LVS_EX_SUBITEMIMAGES))


GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")

GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")

GUISetState(@SW_SHOW)


$array = Array()

For $x = 0 To UBound($array) - 1
    GUICtrlCreateListViewItem($array[$x][0] & "|" & $array[$x][1] & "|" & $array[$x][2], $oListView)
Next


While 1
    Sleep(10)
WEnd

Func Array()

    Local $array[5][3]

    $array[0][0] = "img-1"
    $array[0][1] = "3843"
    $array[0][2] = "3843"

    $array[1][0] = "img-2"
    $array[1][1] = "3939"
    $array[1][2] = "3938"

    $array[2][0] = "img-3"
    $array[2][1] = "sdfg"
    $array[2][2] = "sdfgg"

    $array[3][0] = "img-4"
    $array[3][1] = "rwet"
    $array[3][2] = "rwet"

    $array[4][0] = "img-5"
    $array[4][1] = "ert6"
    $array[4][2] = "ert6"

    Return $array

EndFunc   ;==>Array

Func WM_NOTIFY($hWnd, $Msg, $wParam, $lParam)
    Local $tNMHDR, $hWndFrom, $iCode
    $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
    $hWndFrom = DllStructGetData($tNMHDR, "hWndFrom")
    $iCode = DllStructGetData($tNMHDR, "Code")
    $g_hListView = GUICtrlGetHandle($oListView)
    Switch $hWndFrom
        Case $g_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")
                    Local $iColor = RGB2BGR(0xffffff)

                    $col1 = _GUICtrlListView_GetItemText($g_hListView, $iItem, 1)
                    $col2 = _GUICtrlListView_GetItemText($g_hListView, $iItem, 2)

                    If $col1 = $col2 Then
                        $iColor = RGB2BGR(0x00ff00)
                    EndIf

                    DllStructSetData($tCustDraw, "clrTextBk", $iColor)
                    Return $CDRF_NEWFONT
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY

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

Func _Exit()
    Exit
EndFunc   ;==>_Exit

 

Share this post


Link to post
Share on other sites

look also here:

and here:

 


Signature beginning:   Wondering who uses AutoIT and what it can be used for ?
* GHAPI UDF - modest begining - comunication with GitHub REST API *
ADO.au3 UDF     POP3.au3 UDF     XML.au3 UDF    How to use IE.au3  UDF with  AutoIt v3.3.14.x  for other useful stuff click the following button

Spoiler

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

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAP * WinSCP UDF * GHAPI UDF - modest begining - comunication with GitHub REST API *

My contribution to others projects or UDF based on  others projects: * _sql.au3 UDF  * POP3.au3 UDF *  RTF Printer - UDF * XML.au3 - BETA * ADO.au3 UDF SMTP Mailer UDF *

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Best coding practices * 

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities * _IEquerySelectorAll() (by uncommon) * 

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming"
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

Signature last update: 2017-06-04

Share this post


Link to post
Share on other sites

For sub items in a row, you would have to do custom drawn, as mentioned above.

For a simple method of just having different color rows, you could check out the latest version of my KindEbook Wishlist program, though be advised, it was created and updated with AutoIt v3.3.0.0, so it may not work in later AutoIt versions as is. There are two methods it uses, one for creating entries on an odd and even basis (pale green or pink), and the second during a Price Query, where a failure colors the whole row Red, and a change colors it Green, and when no change it stays as is (pale green or pink).

 


AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

Share this post


Link to post
Share on other sites

awesome this is how i did it.

thank you all for your help!

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

Opt("GUIOnEventMode", 1)

GUICreate("ListView Add SubItem", 400, 300)
$oListView = GUICtrlCreateListView("Thumbnail|Folder", 2, 2, 394, 268, -1, BitOR($LVS_EX_FULLROWSELECT, $LVS_EX_SUBITEMIMAGES))

GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")

GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")

GUISetState(@SW_SHOW)


$array = Array()

For $x = 0 To UBound($array) - 1
    GUICtrlCreateListViewItem($array[$x][0] & "|" & $array[$x][1] & "|" & $array[$x][2], $oListView)
Next


While 1
    Sleep(10)
WEnd

Func Array()

    Local $array[5][3]

    $array[0][0] = "img-1"
    $array[0][1] = "3843"
    $array[0][2] = "3843"

    $array[1][0] = "img-2"
    $array[1][1] = "3939"
    $array[1][2] = "3938"

    $array[2][0] = "img-3"
    $array[2][1] = "sdfg"
    $array[2][2] = "sdfgg"

    $array[3][0] = "img-4"
    $array[3][1] = "rwet"
    $array[3][2] = "rwet"

    $array[4][0] = "img-5"
    $array[4][1] = "ert6"
    $array[4][2] = "ert6"

    Return $array

EndFunc   ;==>Array

Func WM_NOTIFY($hWnd, $Msg, $wParam, $lParam)
    Local $tNMHDR, $hWndFrom, $iCode
    $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
    $hWndFrom = DllStructGetData($tNMHDR, "hWndFrom")
    $iCode = DllStructGetData($tNMHDR, "Code")
    $g_hListView = GUICtrlGetHandle($oListView)

    Switch $hWndFrom
        Case $g_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 $iItem = DllStructGetData($tCustDraw, "dwItemSpec")
                    Local $iSubItem = DllStructGetData($tCustDraw, "iSubItem")

;~                  Local $iBackColor = RGB2BGR(0xFFFFFF)
;~                  Local $iTextColor = RGB2BGR(0x000000)

                    $col1 = _GUICtrlListView_GetItemText($g_hListView, $iItem, 1)
                    $col2 = _GUICtrlListView_GetItemText($g_hListView, $iItem, 2)

                    Switch $iSubItem
                        Case 1
                            If $col1 = $col2 Then
                                $iBackColor = RGB2BGR(0x04B404)
                            Else
                                $iBackColor = RGB2BGR(0xFF0000)
                            EndIf

                            DllStructSetData($tCustDraw, "clrTextBk", $iBackColor)
;~                          DllStructSetData($tCustDraw, "ClrText", $iTextColor)
                    EndSwitch

                    Return $CDRF_NEWFONT
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
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