Outlook风格的使用不同的颜色单列显示新邮件数 (转)[@more@]
下载演示
程序
DOWNLOAD/cooltree_r.zip" rel="nofollow">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;
{
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;
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;
// ::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;
// 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;
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(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 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_h object);
// 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);
}
{
::FillRect(hdc, &rcItem, (HBRUSH)m_BackBrush.m_h object);
// 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);
::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);
}
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);
}
需要说明的是如果你支持拖放而且你需要做一些改变。
<%}%>
愿你快乐
上一篇:
使用C#制作的更换桌面背景程序 (转)
下一篇:
揭开正则表达式语法的神秘面纱 (转)
请登录后发表评论
登录
全部评论
<%=items[i].createtime%>
<%=items[i].content%>
<%if(items[i].items.items.length) { %>
<%for(var j=0;j
<%}%> <%if(items[i].items.total > 5) { %>
<%}%>
<%=items[i].items.items[j].createtime%>
<%=items[i].items.items[j].username%> 回复 <%=items[i].items.items[j].tousername%>: <%=items[i].items.items[j].content%>
还有<%=items[i].items.total-5%>条评论
) data-count=1 data-flag=true>点击查看
<%}%>
转载于:http://blog.itpub.net/10752019/viewspace-975827/