如何在CListCtr中设置某一行的文本颜色

dingyongqiang117 2005-04-28 02:01:38
如何在CListCtr中设置某一行的文本颜色
...全文
90 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
gwinner 2005-04-28
  • 打赏
  • 举报
回复
自已处理NM_CUSTOMDRAW事件。

在NM_CUSTOMDRAW响应函数中对特定的行设置你想要的颜色、字体等。

给你一个Tree的例子,List的实现方法类似。

//Add the follows line after BEGIN_MESSAGE_MAP and before END_MESSAGE_MAP()
ON_NOTIFY(NM_CUSTOMDRAW, IDC_TREE_ADDR, OnCustomdrawTree)

//Add the follow line into .h file before DECLARE_MESSAGE_MAP() to define the function
afx_msg void OnCustomdrawTree(NMHDR*, LRESULT*);

void CBrowseAddressPage::OnCustomdrawTree ( NMHDR* pNMHDR, LRESULT* pResult )
{
// This is the beginning of an item's paint cycle.
LVITEM rItem;
int nItem = static_cast<int>( pLVCD->nmcd.dwItemSpec );
CDC* pDC = CDC::FromHandle ( pLVCD->nmcd.hdc );
COLORREF crBkgnd;
BOOL bListHasFocus;
CRect rcItem;
CRect rcText;
CString sText;
UINT uFormat;

bListHasFocus = ( m_list.GetSafeHwnd() == ::GetFocus() );
// Get the image index and selected/focused state of the
// item being drawn.
ZeroMemory ( &rItem, sizeof(LVITEM) );
rItem.mask = LVIF_IMAGE | LVIF_STATE;
rItem.iItem = nItem;
rItem.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
m_list.GetItem ( &rItem );

// Get the rect that holds the item's icon.
m_list.GetItemRect ( nItem, &rcItem, LVIR_ICON );

// Draw the icon.
uFormat = ILD_TRANSPARENT;

if ( ( rItem.state & LVIS_SELECTED ) && bListHasFocus )
uFormat |= ILD_FOCUS;

m_imglist.Draw ( pDC, rItem.iImage, rcItem.TopLeft(), uFormat );

// Get the rect that bounds the text label.
m_list.GetItemRect ( nItem, rcItem, LVIR_LABEL );


// Draw the background of the list item. Colors are selected
// according to the item's state.

if ( rItem.state & LVIS_SELECTED )
{
if ( bListHasFocus )
{
crBkgnd = GetSysColor ( COLOR_HIGHLIGHT );
pDC->SetTextColor ( GetSysColor ( COLOR_HIGHLIGHTTEXT ));
}
else
{
crBkgnd = GetSysColor ( COLOR_BTNFACE );
pDC->SetTextColor ( GetSysColor ( COLOR_BTNTEXT ));
}
}
else
{
crBkgnd = GetSysColor ( COLOR_WINDOW );
pDC->SetTextColor ( GetSysColor ( COLOR_BTNTEXT ));
}

// Draw the background & prep the DC for the text drawing. Note
// that the entire item RECT is filled in, so this emulates the full-
// row selection style of normal lists.
pDC->FillSolidRect ( rcItem, crBkgnd );
pDC->SetBkMode ( TRANSPARENT );


// Tweak the rect a bit for nicer-looking text alignment.
rcText = rcItem;
rcText.left += 3;
rcText.top++;

// Draw the text.
sText = m_list.GetItemText ( nItem, 0 );

pDC->DrawText ( sText, rcText, DT_VCENTER | DT_SINGLELINE );

// Draw a focus rect around the item if necessary.
if ( bListHasFocus && ( rItem.state & LVIS_FOCUSED ))
{
pDC->DrawFocusRect ( rcItem );
}
*pResult = CDRF_SKIPDEFAULT; // We've painted everything.
}
xundeng 2005-04-28
  • 打赏
  • 举报
回复
那要自绘

15,979

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 界面
社区管理员
  • 界面
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧