Jump to content

[Solved] TreeView: No way to set item text and background color for different TreeView level

Recommended Posts


Hi Community,

I have a problem with the usage of _GUICtrlTreeView_AddChild and setting different item text colors (and different background colors), separately for the TreeView levels.

I use the UDF GUITreeViewEx.au3 of 'Melba23' and I renamed the functions to increase my understanding about the content. I already talked to him about his UDF to respect his rights and intellectual property. I read many forum threads, I tried many ways to get it, but it's seems to be impossible in the way that I use the UDF.

How can I set different item text colors, separately for the TreeView levels?
How can I set different item background colors, separately for the TreeView levels?

I found out that _GUICtrlTreeView_AddChild don't let my set this properties directly. But after loading the TreeView content I can't change the items (children) too. Only the font weight to bold I could set with _GUICtrlTreeView_SetBold but I want to do this in a generic way directly at the creation (loading) of the TreeView.

Of course I tried the different functions to set text color or bkgColor, but it didn't work - maybe I do it wrong.
I really hope you can help me, give me a hint and understand what I want to do.

[Solved] final solution made by @LarsJ (see below)

Thanks for your help - I'm grateful!



As attachment you will find a *.zip which contains 3 files. Two *.au3 files and one *.ini file.
The ini contains the TreeView levels as ini sections. I think you will see what I mean, if you try it :) .



In the animated *.gif you can see that I only got bold text font for the parent items. But the parents and the next level 'Feature' should have different text colors and different background colors which should be dynamically created, not like I do with _setParentTextToBold( 'Section1' ) manually.






Edited by Sven-Seyfert
Link to comment
Share on other sites

Mr. Sven-Seyfert, You do it this way:

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

; compiler informations for AutoIt
#AutoIt3Wrapper_Res_Description=ExecutionPlan (2018-05-11)

; includes ---------------------------------------------------------------------
#include <Array.au3>
#include <ButtonConstants.au3>
#include <GuiConstantsEx.au3>
#include <GuiTreeView.au3>
#include <WindowsConstants.au3>

; opt and just singleton -------------------------------------------------------
Opt( 'MustDeclareVars', 1 )
Global $aInst = ProcessList( 'ExecutionPlan.exe' )
If $aInst[0][0] > 1 Then Exit

; declaration ------------------------------------------------------------------
Global $sFileConfig              = @ScriptDir & '\ExecutionPlan_Config.ini'

Global $aSection1                = IniReadSection( $sFileConfig, 'Section1' )
Global $aSection2                = IniReadSection( $sFileConfig, 'Section2' )
Global $aSection3                = IniReadSection( $sFileConfig, 'Section3' )
Global $aSection4                = IniReadSection( $sFileConfig, 'Section4' )

Global $bCheck                   = True
Global $hItemSelected            = 0
Global $aTreeViewDataArray[1][2] = [[0, 0]]
Global $sTreeViewData

; gui --------------------------------------------------------------------------
Global $hMainGui      = GUICreate( 'ExecutionPlan', 625, 700 )
Global $cTreeView     = GUICtrlCreateTreeView( 0, 0, 625, 630, BitOR( $GUI_SS_DEFAULT_TREEVIEW, $TVS_CHECKBOXES ) )
Global $cBtnAllOrNone = GUICtrlCreateButton( '', 10, 640, 50, 50, $BS_ICON )
Global $cBtnRun       = GUICtrlCreateButton( '', 70, 640, 50, 50, $BS_ICON )
Global $cBtnExit      = GUICtrlCreateButton( '', 565, 640, 50, 50, $BS_ICON )

GUICtrlSetImage( $cBtnAllOrNone, 'shell32.dll', 274, 1 )
GUICtrlSetImage( $cBtnRun,       'shell32.dll', 25, 1 )
GUICtrlSetImage( $cBtnExit,      'shell32.dll', 28, 1 )

GUICtrlSetTip( $cBtnAllOrNone,   'Toggle Checkboxes' )
GUICtrlSetTip( $cBtnRun,         'Execute checked test scenarios' )
GUICtrlSetTip( $cBtnExit,        'Exit program' )

; functions --------------------------------------------------------------------
#include 'ExecutionPlan_Functions.au3'

; Register WM_NOTIFY message handler through subclassing
Global $pWM_NOTIFY = DllCallbackGetPtr( DllCallbackRegister( "WM_NOTIFY", "lresult", "hwnd;uint;wparam;lparam;uint_ptr;dword_ptr" ) )
DllCall( "comctl32.dll", "bool", "SetWindowSubclass", "hwnd", $hMainGui, "ptr", $pWM_NOTIFY, "uint_ptr", 9999, "dword_ptr", GUICtrlGetHandle( $cTreeView ) ) ; $iSubclassId = 9999, $pData = $hTreeView

; processing -------------------------------------------------------------------
_appendTreeViewDataByIniSection( 'Section1|', $aSection1 )
_appendTreeViewDataByIniSection( 'Section2|', $aSection2 )
_appendTreeViewDataByIniSection( 'Section3|', $aSection3 )
_appendTreeViewDataByIniSection( 'Section4|', $aSection4 )

_loadTreeViewData( $cTreeView, StringTrimRight( $sTreeViewData, 1 ) )

_GUICtrlTreeView_Expand( $cTreeView )

_setParentTextToBold( 'Section1' )
_setParentTextToBold( 'Section2' )
_setParentTextToBold( 'Section3' )
_setParentTextToBold( 'Section4' )

GUISetState( @SW_SHOW, $hMainGui )

GUIRegisterMsg( $WM_NOTIFY, '_treeView_WM_NOTIFY_Handler' )
_createArrayOfCurrentCheckboxState( $cTreeView )

While 1
    Global $eMsg = GUIGetMsg()

    Switch $eMsg
        Case $GUI_EVENT_CLOSE, $cBtnExit
            ; Remove WM_NOTIFY message handler
            DllCall( "comctl32.dll", "bool", "RemoveWindowSubclass", "hwnd", $hMainGui, "ptr", $pWM_NOTIFY, "uint_ptr", 9999 )
            _disposeAndExit( $cTreeView )
        Case $cBtnAllOrNone
            If $bCheck Then
                _toggleCheckboxes( $cTreeView, $bCheck )
                $bCheck = False
                _toggleCheckboxes( $cTreeView, $bCheck )
                $bCheck = True
        Case $cBtnRun
            Global $aData = _getDataAsArray( $cTreeView )
            _ArrayDisplay( $aData )


Func WM_NOTIFY( $hWnd, $iMsg, $wParam, $lParam, $iSubclassId, $hTreeView ) ; $pData = $hTreeView
  If $iMsg <> $WM_NOTIFY Then Return DllCall( "comctl32.dll", "lresult", "DefSubclassProc", "hwnd", $hWnd, "uint", $iMsg, "wparam", $wParam, "lparam", $lParam )[0]

  Local $tStruct  = DllStructCreate( _
    'struct;hwnd hWndFrom;uint_ptr IDFrom;INT Code;endstruct;' & _
    'uint Action;struct;uint OldMask;handle OldhItem;uint OldState;uint OldStateMask;' & _
    'ptr OldText;int OldTextMax;int OldImage;int OldSelectedImage;int OldChildren;lparam OldParam;endstruct;' & _
    'struct;uint NewMask;handle NewhItem;uint NewState;uint NewStateMask;' & _
    'ptr NewText;int NewTextMax;int NewImage;int NewSelectedImage;int NewChildren;lparam NewParam;endstruct;' & _
    'struct;long PointX;long PointY;endstruct', $lParam )

  If DllStructGetData( $tStruct, 'hWndFrom' ) <> HWnd( $hTreeView ) Then _
    Return DllCall( "comctl32.dll", "lresult", "DefSubclassProc", "hwnd", $hWnd, "uint", $iMsg, "wparam", $wParam, "lparam", $lParam )[0]

  Switch DllStructGetData( $tStruct, 'Code' )
      Local $tNMTVCUSTOMDRAW = DllStructCreate($tagNMTVCUSTOMDRAW, $lParam)
      Local $dwDrawStage = DllStructGetData($tNMTVCUSTOMDRAW, "DrawStage")
      Switch $dwDrawStage             ; Holds a value that specifies the drawing stage
        Case $CDDS_PREPAINT           ; Before the paint cycle begins
          Return $CDRF_NOTIFYITEMDRAW ; Notify the parent window of any item-related drawing operations
        Case $CDDS_ITEMPREPAINT       ; Before painting an item
          Local $iLevel = 0, $hItem = DllStructGetData($tNMTVCUSTOMDRAW, "ItemSpec")
          Local $iState = DllStructGetData($tNMTVCUSTOMDRAW, "ItemState")
          If Not $hItem Or BitAnd( $iState, $CDIS_FOCUS ) Then Return $CDRF_NEWFONT
          While $hItem And $iLevel < 3
            $hItem = _SendMessage(HWnd( $hTreeView ), $TVM_GETNEXTITEM, $TVGN_PARENT, $hItem, 0, "wparam", "handle", "handle")
            $iLevel += 1
          Switch $iLevel
            Case 1
              DllStructSetData( $tNMTVCUSTOMDRAW, "ClrText", ColorConvert(0xFFFFFF) )   ; Forecolor of item text
              DllStructSetData( $tNMTVCUSTOMDRAW, "clrTextBk", ColorConvert(0x006400) )  ; Backcolor of item text
            Case 2
              DllStructSetData( $tNMTVCUSTOMDRAW, "ClrText", ColorConvert(0xFFFFFF) )   ; Forecolor of item text
              DllStructSetData( $tNMTVCUSTOMDRAW, "clrTextBk", ColorConvert(0x008B8B) )  ; Backcolor of item text
          Return $CDRF_NEWFONT        ; $CDRF_NEWFONT must be returned after changing font or colors

  Return DllCall( "comctl32.dll", "lresult", "DefSubclassProc", "hwnd", $hWnd, "uint", $iMsg, "wparam", $wParam, "lparam", $lParam )[0]
  #forceref $iSubclassId

;RGB to BGR or BGR to RGB
Func ColorConvert($iColor)
  Return BitOR(BitAND($iColor, 0x00FF00), BitShift(BitAND($iColor, 0x0000FF), -16), BitShift(BitAND($iColor, 0xFF0000), 16))


Link to comment
Share on other sites

Hi @LarsJ,

that's amazing. Honestly I couldn't do that way on my own. It's a bit too much of DLLStructs, which is a complex topic for me.
But I'm really grateful for your help and great solution.

Thanks for your help - I'm grateful!

Edited by Sven-Seyfert
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

  • Create New...