ITPub博客

首页 > IT基础架构 > 网络安全 > Outlook风格的使用不同的颜色单列显示新邮件数 (转)

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

原创 网络安全 作者:amyz 时间:2007-10-11 13:07:16 0 删除 编辑
Outlook风格的使用不同的颜色单列显示新邮件数 (转)[@more@]
下载演示程序    DOWNLOAD/cooltree_r.zip">http://www.foolab.com/download/cooltree_r.zip
下载源代码 http://www.foolab.com/download/cooltree.zip
下载纷飞(Outplay...)http://www.foolab.com/download/outplay_1.03.zip
 
.NET/develop/article/images/16253_outplay.GIF" align=baseline border=0>
动机(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; SCOlor">//=:GetSysColor(COLOR_HIGHLIGHTTEXT);
 }
 else if(nItemState & CDIS_SELECTED)
 {
 ::FillRect(hdc, &rcItem, (HBRUSH)m_GrayBrush.m_hObject);
 }
 else
 {
 TRACE("CLEAR HIGHn");
 // 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(Mailto:email:Outplay@foolab.com">Email:Outplay@foolab.com) 2002.12.29 

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10752019/viewspace-975827/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论