views:

317

answers:

2

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

A: 

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.

Nikola Smiljanić
Thanks, will look at that.
Max
A: 

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.

Stefan