CListCtrl 设置文字背景色的时候,怎么只在有定的地方出现背景色?

在CListCtrl控件里,用SetTextBkColor()设置背文字景色的时候,是一整行都设置成了背景色,我现在是想只在有字的地方出现背景色,没有字的地方还是原来的颜... 在CListCtrl控件里,用SetTextBkColor()设置背文字景色的时候,是一整行都设置成了背景色,我现在是想只在有字的地方出现背景色,没有字的地方还是原来的颜色,如白色。就像CListBox一样。这个怎么实现了,请大家帮帮忙呀! 展开
 我来答
175943462
推荐于2016-09-06
知道答主
回答量:23
采纳率:0%
帮助的人:13.7万
展开全部
设置列表视图控件奇偶行颜色

1添加声明

afx_msg void OnGlobalhook();
//}}AFX_MSG
afx_msg void OnCustomdrawMyList ( NMHDR* pNMHDR, LRESULT* pResult );

2添加消息映射

BEGIN_MESSAGE_MAP(CPage5, CDialog)
//{{AFX_MSG_MAP(CPage5)
ON_NOTIFY(TVN_SELCHANGED, IDC_TREE1, OnSelchangedTree1)
ON_WM_CONTEXTMENU()
ON_COMMAND(ID_GLOBALHOOK, OnGlobalhook)
//}}AFX_MSG_MAP
ON_NOTIFY ( NM_CUSTOMDRAW, IDC_LIST1, OnCusto

void CMyListCtrlView::OnCustomdrawMyList(NMHDR *pNMHDR, LRESULT *pResult)
{
NMLVCUSTOMDRAW* pLVCD = reinterpret_cast<NMLVCUSTOMDRAW*>( pNMHDR );

//设置listctrl的格式,由格子等

pListCtrl.SetExtendedStyle(pListCtrl.GetExtendedStyle() | LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);

if ( CDDS_PREPAINT == pLVCD->nmcd.dwDrawStage )
{
*pResult = CDRF_NOTIFYITEMDRAW;
}
else if ( CDDS_ITEMPREPAINT == pLVCD->nmcd.dwDrawStage )
{

*pResult = CDRF_NOTIFYSUBITEMDRAW;
}
else if ( (CDDS_ITEMPREPAINT | CDDS_SUBITEM) == pLVCD->nmcd.dwDrawStage )
{

COLORREF clrNewTextColor, clrNewBkColor;

int nItem = static_cast<int>( pLVCD->nmcd.dwItemSpec );

// CString strTemp = m_ctListCtrl.GetItemText(nItem,pLVCD->iSubItem);

// if(strTemp == m_strName)
if(nItem%2 ==0) //奇偶行颜色不同,也可以改变某一小单元的颜色,比如是某行某列,
//用strTemp得到这个单元的文本值,然后再做处理

{
clrNewTextColor = RGB(0,0,0); //Set the text to red
clrNewBkColor = RGB(240,240,240); //Set the bkgrnd color to blue
}
else
{

clrNewTextColor = RGB(0,0,0); //Leave the text black
clrNewBkColor = RGB(255,255,255); //leave the bkgrnd color white
}

pLVCD->clrText = clrNewTextColor;
pLVCD->clrTextBk = clrNewBkColor;

// Tell Windows to paint the control itself.
*pResult = CDRF_DODEFAULT;

}
}

设置列表控件某行颜色

1添加声明

afx_msg void OnGlobalhook();
//}}AFX_MSG
afx_msg void OnCustomdrawMyList ( NMHDR* pNMHDR, LRESULT* pResult );

2添加消息映射

BEGIN_MESSAGE_MAP(CPage5, CDialog)
//{{AFX_MSG_MAP(CPage5)
ON_NOTIFY(TVN_SELCHANGED, IDC_TREE1, OnSelchangedTree1)
ON_WM_CONTEXTMENU()
ON_COMMAND(ID_GLOBALHOOK, OnGlobalhook)
//}}AFX_MSG_MAP
ON_NOTIFY ( NM_CUSTOMDRAW, IDC_LIST1, OnCustomdrawMyList )
END_MESSAGE_MAP()

3代码实现

void CPage5::OnCustomdrawMyList ( NMHDR* pNMHDR, LRESULT* pResult )
{
//This code based on Michael Dunn's excellent article on
//list control custom draw at http://www.codeproject.com/listctrl/lvcustomdraw.asp

NMLVCUSTOMDRAW* pLVCD = reinterpret_cast<NMLVCUSTOMDRAW*>( pNMHDR );

// Take the default processing unless we set this to something else below.
*pResult = CDRF_DODEFAULT;

// First thing - check the draw stage. If it's the control's prepaint
// stage, then tell Windows we want messages for every item.
if ( CDDS_PREPAINT == pLVCD->nmcd.dwDrawStage )
{
*pResult = CDRF_NOTIFYITEMDRAW;
}
else if ( CDDS_ITEMPREPAINT == pLVCD->nmcd.dwDrawStage )
{
// This is the notification message for an item. We'll request
// notifications before each subitem's prepaint stage.

*pResult = CDRF_NOTIFYSUBITEMDRAW;
}
else if ( (CDDS_ITEMPREPAINT | CDDS_SUBITEM) == pLVCD->nmcd.dwDrawStage )
{

COLORREF clrNewTextColor, clrNewBkColor;

int nItem = static_cast<int>( pLVCD->nmcd.dwItemSpec );

// CString strTemp = m_HookList.GetItemText(nItem,pLVCD->iSubItem);
CString strTemp = m_HookList.GetItemText(nItem,5);
CString strtemp2 = m_HookList.GetItemText(nItem,4);

if(strTemp == "YES"||strtemp2 == "YES")
{
clrNewTextColor = RGB(255,0,0); //Set the text to red
clrNewBkColor = RGB(255,255,255); //Set the bkgrnd color to blue
}
else
{

clrNewTextColor = RGB(0,0,0); //Leave the text black
clrNewBkColor = RGB(255,255,255); //leave the bkgrnd color white
}

pLVCD->clrText = clrNewTextColor;
pLVCD->clrTextBk = clrNewBkColor;

// Tell Windows to paint the control itself.
*pResult = CDRF_DODEFAULT;

}
}

RGB(182,88,157); 子色
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
ll3096
2009-08-07 · TA获得超过108个赞
知道答主
回答量:96
采纳率:0%
帮助的人:110万
展开全部
CUSTOMDRAW消息
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式