Jump to content

treeview set selected item color


Recommended Posts

hi,

with the code below you can change the color of the selected item in a listview.

In the example below in red.

Is the same possible with a treeview?

 

 

#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <File.au3>
#include <String.au3>
#include <Array.au3>
#include <GuiListView.au3>

Global $iCurentSelectet = 0, $iCurentSelectetPrev = 0

$hGui = GUICreate("1", 370, 464)

Global $h_ListView = GUICtrlCreateListView("", 4, 32, 362, 414)

_GUICtrlListView_AddColumn($h_ListView, "Items", 362)

GUICtrlCreateListViewItem("TEST", $h_ListView)
GUICtrlCreateListViewItem("TEST2", $h_ListView)


GUIRegisterMsg($WM_NOTIFY, "_WM_NOTIFY")
GUISetState(@SW_SHOW, $hGui)

While 1
    Sleep(10)
    $msg = GUIGetMsg()
    If $msg = $GUI_EVENT_CLOSE Then Exit
WEnd

Func _WM_NOTIFY($hwnd, $iMsg, $iwParam, $ilParam)
    #forceref $hwnd, $iMsg, $iwParam
    Local Static $iHot = -1, $iHotPrev = -1
    Local $hWndFrom, $iCode, $tNMHDR, $tInfo

    $hWndListView = $h_ListView
    If Not IsHWnd($hWndListView) Then $hWndListView = GUICtrlGetHandle($h_ListView)

    $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iCode = DllStructGetData($tNMHDR, "Code")
    Switch $hWndFrom
        Case $hWndListView
            Switch $iCode
                Case $LVN_ITEMCHANGING ;disable blue on hover
                    Return 1
                Case $NM_CLICK ; Sent by a list-view control when the user clicks an item with the left mouse button
                    $tInfo = DllStructCreate($tagNMLISTVIEW, $ilParam)
                    $iCurentSelectet = DllStructGetData($tInfo, "Item")
                    _GUICtrlListView_RedrawItems($hWndListView, $iCurentSelectet, $iCurentSelectet)
                    _GUICtrlListView_RedrawItems($hWndListView, $iCurentSelectetPrev, $iCurentSelectetPrev)
                    $iCurentSelectetPrev = $iCurentSelectet
                Case $NM_CUSTOMDRAW
                    Local $tNMLVCUSTOMDRAW = DllStructCreate($tagNMLVCUSTOMDRAW, $ilParam)
                    Local $dwDrawStage = DllStructGetData($tNMLVCUSTOMDRAW, "dwDrawStage")
                    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
                            Return $CDRF_NOTIFYSUBITEMDRAW          ; Notify the parent window of any SUBITEM-related drawing operations
                        Case BitOR($CDDS_ITEMPREPAINT, $CDDS_SUBITEM) ; Before painting a subitem
                            Local $dwItemSpec = DllStructGetData($tNMLVCUSTOMDRAW, "dwItemSpec") ; Item index
                            If $dwItemSpec = $iCurentSelectet Then
                                DllStructSetData($tNMLVCUSTOMDRAW, "ClrText", _RGB2BGR(0xFFFFFF))
                                DllStructSetData($tNMLVCUSTOMDRAW, "ClrTextBk", _RGB2BGR(0xFF2400))
                            Else ; Other rows
                                DllStructSetData($tNMLVCUSTOMDRAW, "ClrText", _RGB2BGR(0xFFFFFF))
                                DllStructSetData($tNMLVCUSTOMDRAW, "ClrTextBk", _RGB2BGR(0x919191)) ;_RGB2BGR(0x191919))
                            EndIf
                            Return $CDRF_NEWFONT                    ; $CDRF_NEWFONT must be returned after changing font or colors
                    EndSwitch
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>_WM_NOTIFY


Func _RGB2BGR($iColor)
    Local $sH = Hex($iColor, 6)
    Return '0x' & StringRight($sH, 2) & StringMid($sH, 3, 2) & StringLeft($sH, 2)
EndFunc   ;==>_RGB2BGR

 

Edited by bladem2003
Link to comment
Share on other sites

Link to comment
Share on other sites

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