Outlook风格的使用不同的颜色单列显示新邮件数

下载演示程序        http://www.foolab.com/download/cooltree_r.zip
下载源代码           http://www.foolab.com/download/cooltree.zip
 
动机(Motivation)
 
当我利用业余时间开发完成纷飞(Outplay...),类似Outlook的客户端的邮件程序后,
我终于轻松地跳了起来,但是就在我的脚刚着地时,我突然看到我面前使用的Outlook,
单列使用不同的颜色显示新邮件,于是我决定,我也该让我的Outplay如此...
 
于是,我立刻查找了codeguru里的关于CTreeView的一些文章,我找到了一些合适的信
息,大该花了我30分钟左右的时间,我让我的纷飞(Outplay...)也出现了类似的效果。
让我慢慢的告诉你,你也会发现它是如此的简单和方便,仅仅是在你的CTreeView中添加
几行的代码
 
你应该怎么做呢?(Follow me)
 
首先假定你的CTreeView派生类CTreeViewEx,然后你需要在TreeViewEx.h中
添加如下行:
 afx_msg void OnCustomDraw(NMHDR* pNMHDR, LRESULT* pResult);
 
然后你需要在你的消息映射中添加
ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnCustomDraw)
在OnCusteomDraw中添加如下代码:
void CLeftView::OnCustomDraw(LPNMHDR pNmhdr, LRESULT* pResult)
{
 static CRect rcItem;
 static int  nItemState;
 
 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;
 
   // reposuition the viewport so the TreeCtrl DefWindowProc doesn't draw to viewport 0/0
   //::SetViewportOrgEx(pCustomDraw->nmcd.hdc, /*m_nOffset*/0, 0, NULL);
   break;
 
  case CDDS_ITEMPREPAINT:
   // set the background and foregroundcolor of the item to the background, so you don't see the default drawing of the text
   //get item state(focus,selected..), because it will be drawn of us
   nItemState = pCustomDraw->nmcd.uItemState;
 
   pCustomDraw->nmcd.uItemState &= ~CDIS_FOCUS;
   pCustomDraw->clrText = m_colHilightText;
   // remember the drawing rectangle of the item so we can draw it ourselves
   m_pTree->GetItemRect((HTREEITEM) pCustomDraw->nmcd.dwItemSpec, &rcItem, TRUE);
   *pResult = CDRF_NOTIFYPOSTPAINT;
   break;
 
  case CDDS_ITEMPOSTPAINT:
   DrawTreeItem(nItemState, rcItem, pCustomDraw->nmcd.hdc, (HTREEITEM) pCustomDraw->nmcd.dwItemSpec);
   break;
  default:
   *pResult = CDRF_DODEFAULT;
 }
}
 
最后你需要做的只是在DrawTreeItem中自绘你想要的得效果就是了如下:
void CLeftView::DrawTreeItem(int nItemState, CRect rcItem, HDC hdc, HTREEITEM hItem)
{
 // if the item has got the focus, we have to draw sorouinding rectangle and fill a rect blue
 COLORREF colText = m_colText;
 
 if(nItemState & CDIS_FOCUS)
 {
  ::FillRect(hdc, &rcItem, (HBRUSH)m_BackBrush.m_hObject);
  // Add new focus rect code...
  ::DrawFocusRect( hdc, &rcItem);
  colText = m_colHilightText; //=:GetSysColor(COLOR_HIGHLIGHTTEXT);
 }
 else if(nItemState & CDIS_SELECTED)
 {
  ::FillRect(hdc, &rcItem, (HBRUSH)m_GrayBrush.m_hObject);
 }
 else
 {
  TRACE("CLEAR HIGH/n");
   // add this to clear leftover highlight bar
  ::FillRect(hdc, &rcItem, (HBRUSH)m_BackBrushNormal.m_hObject);
 }
 
  // always write text without background
 ::SetBkMode(hdc, TRANSPARENT);
 ::SetTextColor(hdc,colText);
 CString str = m_pTree->GetItemText(hItem);
 ::DrawText(hdc, str, -1, &rcItem, DT_VCENTER | DT_SINGLELINE | DT_WORD_ELLIPSIS);
 
// you may modify them according to your need
 int nNew = 20;
 CString strNew = _T("");
 strNew.Format(_T("(%d)"),nNew);
 ::SetTextColor(hdc,RGB(0,0,255));
 RECT rc = rcItem;
 rc.left = rcItem.right+2;
 rc.right = rc.left + 100;
 ::FillRect(hdc, &rc, (HBRUSH)m_BackBrushNormal.m_hObject);
 ::DrawText(hdc,strNew, -1, &rc, DT_VCENTER | DT_SINGLELINE | DT_WORD_ELLIPSIS);
}
 
需要说明的是如果你支持拖放而且你需要做一些改变。
            愿你快乐 
            Lazen(Email:Outplay@foolab.com) 2002.12.29           
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值