#include <GUIConstantsEx.au3>
#include <StructureConstants.au3>
#include <TreeViewConstants.au3>
#include <WinAPI.au3>
#include <WindowsConstants.au3>
Opt("GUIOnEventMode", 1)

Global $GUI_Gui = GUICreate("TreeView - TestGUI", @DesktopWidth - 300, @DesktopHeight - 100)
GUISetOnEvent(-3, "_GUI_Event_Close")
GUISetBkColor(0xFFFFFF)

Global $TV = GUICtrlCreateTreeView(10, 40, 180, @DesktopHeight - 354, _
			BitOR(Default,$TVS_TRACKSELECT,$TVS_FULLROWSELECT), $WS_EX_CLIENTEDGE)
Global $hTV = GUICtrlGetHandle($TV)

Global $aTVI[6]
For $i = 0 To 5
	$aTVI[$i] = GUICtrlGetHandle(GUICtrlCreateTreeViewItem("Testeintrag #" & $i+1, $TV))
Next
Global $aColor[3][3] ; [$i][0]=hWnd, [$i][1]=txtCol, [$i][2]=bkCol
$aColor[0][0] = $aTVI[1]
$aColor[0][1] = 0xffffff
$aColor[0][2] = 0xffff00

$aColor[1][0] = $aTVI[3]
$aColor[1][1] = 0x000000
$aColor[1][2] = 0x0000ff

$aColor[2][0] = $aTVI[5]
$aColor[2][1] = 0x0000ff
$aColor[2][2] = 0xffff00

Global $currWindow = WinGetHandle($GUI_Gui)
Global $txtColor, $bkColor

GUISetState(@SW_SHOW, $GUI_Gui)
GUIRegisterMsg($WM_NOTIFY, 'WM_NOTIFY_TREEVIEW')
_WinAPI_RedrawWindow($currWindow)


While 1
    Sleep(100)
WEnd

Func _GUI_Event_Close()
    Exit
EndFunc   ;==>_GUI_Event_Close



Func WM_NOTIFY_TREEVIEW($hWnd, $Msg, $wParam, $lParam)
    Local $hWndFrom, $iCode, $tNMHDR, $tItemSpec
    $tNMHDR = DllStructCreate($tagNMHDR, $lParam)

    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
	If $hWndFrom <> $hTV Then Return $GUI_RUNDEFMSG

    $iCode = DllStructGetData($tNMHDR, "Code")
    If $iCode = $NM_CUSTOMDRAW Then
		Local $iDrawStage, $iItem, $tCustDraw = DllStructCreate($tagNMTVCUSTOMDRAW, $lParam)
        $iDrawStage = DllStructGetData($tCustDraw, 'DrawStage')
        If $iDrawStage = $CDDS_ITEMPREPAINT Then
			$tItemSpec = DllStructCreate('dword', DllStructGetPtr($tCustDraw, 'ItemSpec'))
			$iItem = DllStructGetData($tItemSpec, 1)
			For $i = 0 To UBound($aColor) -1
				If $iItem = $aColor[$i][0] Then
					DllStructSetData($tCustDraw, 'ClrText', RGB2BGR($aColor[$i][1]))
					DllStructSetData($tCustDraw, 'ClrTextBk', RGB2BGR($aColor[$i][2]))
					Return $CDRF_NEWFONT
				EndIf
			Next
		EndIf
	EndIf
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY_TREEVIEW

Func RGB2BGR($iColor)
	Local $sH = Hex($iColor,6)
    Return '0x' & StringRight($sH, 2) & StringMid($sH,3,2) & StringLeft($sH, 2)
EndFunc  ;==>RGB2BGR
