CListCtrl 设置文字背景色的时候,怎么只在有定的地方出现背景色?
在CListCtrl控件里,用SetTextBkColor()设置背文字景色的时候,是一整行都设置成了背景色,我现在是想只在有字的地方出现背景色,没有字的地方还是原来的颜...
在CListCtrl控件里,用SetTextBkColor()设置背文字景色的时候,是一整行都设置成了背景色,我现在是想只在有字的地方出现背景色,没有字的地方还是原来的颜色,如白色。就像CListBox一样。这个怎么实现了,请大家帮帮忙呀!
展开
2个回答
展开全部
设置列表视图控件奇偶行颜色
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); 子色
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); 子色
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
CUSTOMDRAW消息
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询