如何使得CTreeCtrl 失去焦点后,选中项的颜色仍然保持不变,怎么实现?高亮


BEGIN_MESSAGE_MAP(CMyTreeviewOut, CTreeView)
//{{AFX_MSG_MAP(CMyTreeviewOut)
  // NOTE - the ClassWizard will add and remove mapping macros here.
ON_NOTIFY_REFLECT(NM_RCLICK, OnRclick)
ON_COMMAND(TREEVIEW_ADD_CHILD_ITEM, OnAddChildItem)
ON_COMMAND(TREEVIEW_DEL_ITEM, OnDelItem)
ON_COMMAND(TREEVIEW_RENAME_ITEM, OnRenameItem)
ON_NOTIFY_REFLECT(TVN_BEGINLABELEDIT, OnBeginlabeledit)
ON_NOTIFY_REFLECT(TVN_ENDLABELEDIT, OnEndlabeledit)
ON_WM_DESTROY()
ON_NOTIFY_REFLECT(NM_CLICK, OnClick)
ON_NOTIFY_REFLECT(NM_DBLCLK, OnDblclk)
    ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnCustomDraw )
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

void CMyTreeviewOut::OnCustomDraw(NMHDR *pNMHDR, LRESULT *pResult)            
{            
LPNMHDR pNmhdr = (LPNMHDR)pNMHDR;          
// To make sure that the notification is coming
// from the right control (GGH)
// CLeftView *pView = GetLeftPane();

if(this->GetTreeCtrl().m_hWnd != pNmhdr->hwndFrom)
  return;

LPNMTVCUSTOMDRAW pCustomDraw = (LPNMTVCUSTOMDRAW)pNMHDR;
switch (pCustomDraw->nmcd.dwDrawStage)
{
    case CDDS_PREPAINT:
     // Need to process this case and set pResult to CDRF_NOTIFYITEMDRAW,
     // otherwise parent will never receive CDDS_ITEMPREPAINT notification. (GGH)
     *pResult = CDRF_NOTIFYITEMDRAW;
     return;
    
    case CDDS_ITEMPREPAINT:
     if(this->IsWindowEnabled()==1)
     {
     if ((pCustomDraw->nmcd.uItemState & (CDIS_FOCUS))==0
      &&(pCustomDraw->nmcd.uItemState & (CDIS_SELECTED))==CDIS_SELECTED) // selected
     {
      pCustomDraw->clrTextBk=RGB(255, 255, 255);
      pCustomDraw->clrText = RGB(0, 0, 0);
     }
     *pResult = CDRF_NOTIFYPOSTPAINT;
     return;
     }
     else{
     *pResult = CDRF_DODEFAULT ;
     return;
     }
    case CDDS_ITEMPOSTPAINT:
     if(this->IsWindowEnabled()==1)
     {
     if ((pCustomDraw->nmcd.uItemState & (CDIS_FOCUS))==0
      &&(pCustomDraw->nmcd.uItemState & (CDIS_SELECTED))==CDIS_SELECTED) // selected
     {
      CRect   rcText;  
      HTREEITEM hItem=(HTREEITEM) pCustomDraw->nmcd.dwItemSpec;
      this->GetTreeCtrl().GetItemRect(hItem,   &rcText,   true);
      CPen penBlue(PS_SOLID ,1,RGB(0, 0, 255));
      CDC* pDC=CDC::FromHandle(pCustomDraw->nmcd.hdc);
      CBrush* pBrush=CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH));
      CBrush* pOldBrush = pDC->SelectObject(pBrush);
      CPen* pOldPen = pDC->SelectObject(&penBlue);
      pDC->Rectangle(&rcText);
      pDC->SelectObject(pOldBrush);
      pDC->SelectObject(pOldPen);
     }
     *pResult = CDRF_SKIPDEFAULT;
     return;
     }
     else{
     *pResult = CDRF_DODEFAULT ;
     return;
     }
    
}

}  

猜你喜欢

转载自blog.csdn.net/tom_xuzg/article/details/38348741