Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing the color of a selected CTreeCtrl item?

(VS2008, MFC, feature-pack)

Using a CTreeCtrl, I need to have the selected item "better" highlighted when the control looses focus.

My Tree is created with the "TVS_SHOWSELALWAYS" option in the resource editor, but the color is not visible enough.

I already have code to change the items colors via the custom draw message (NM_CUSTOMDRAW) like this :

void MyTree::OnCustomDraw(NMHDR* pNMHDR, LRESULT* pResult) 
{
    NMTVCUSTOMDRAW *pcd = (NMTVCUSTOMDRAW   *)pNMHDR;
    switch ( pcd->nmcd.dwDrawStage )
    {
    case CDDS_PREPAINT: 
        *pResult = CDRF_NOTIFYITEMDRAW;     
        break;

    case CDDS_ITEMPREPAINT : 
        {
            HTREEITEM   hItem = (HTREEITEM)pcd->nmcd.dwItemSpec;

            if ( this->IsSelected(hItem ))
            {
                pcd->clrText = GetSysColor(COLOR_HIGHLIGHTTEXT);    
                pcd->clrTextBk = GetSysColor(COLOR_HIGHLIGHT);
            }

            *pResult = CDRF_DODEFAULT;// do not set *pResult = CDRF_SKIPDEFAULT
            break;
        }
    }
}

It's working, but is seems to be overkill for a simple task as this.

I think I must be missing something obvious to do this without having to do this.

Anything simpler ?

Thanks.

like image 239
Max Avatar asked Oct 17 '25 14:10

Max


2 Answers

apart from the custom drawing that you already do, you could also set the state TVIS_DROPHILITED for all selected items. But I'm not sure if that's really what you want, especially if your tree control also is a drop target.

You could also try to set the theme of the tree control to explorer:

SetWindowTheme(hTreeControl, L"Explorer", NULL);

This makes the tree control look exactly as in the windows explorer with the same colors. Maybe those are more to your liking.

like image 65
Stefan Avatar answered Oct 21 '25 09:10

Stefan


You could set your selected item's text to bold using SetItemState and TVIS_BOLD. You wouldn't need custom draw for this and less code is always better.

like image 42
Nikola Smiljanić Avatar answered Oct 21 '25 09:10

Nikola Smiljanić



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!