VC改变CListCtrl 表格中文字颜色,和背景颜色。
(1)首先需要自定义一个类,派生自CListCtrl。如下图:

(2)然后在派生类的头文件中声明一个成员函数,如下图:

(3)在源文件中实现该成员方法,如图:

(4)在源文件中做消息映射,如图:

这时候,当CListCtrl控件在绘制的时候,就会有NM_CUSTOMDRAW消息被我们的函数截获。
我们就在实现函数中筛选出CListCtrl控件应该设置内容(文字颜色,文字背景颜色)的时机,对绘制的内容做相应的修改即可。
// ColorListCtrl.cpp : implementation file
// #include "stdafx.h"
#include "test.h"
#include "ColorListCtrl.h" #ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif /////////////////////////////////////////////////////////////////////////////
// CColorListCtrl CColorListCtrl::CColorListCtrl()
{
m_iRow = -; //这里我们定义了三个成员变量,分别用于表示单元格的“行号”,“列号”,“字体” 在构造函数中初始化他们。
m_iCol = -;
m_Font = NULL;
} CColorListCtrl::~CColorListCtrl()
{
if ( m_Font ) delete m_Font; //删除字体 析构函数中销毁字体所指对象。
} BEGIN_MESSAGE_MAP(CColorListCtrl, CListCtrl)
//{{AFX_MSG_MAP(CColorListCtrl)
// NOTE - the ClassWizard will add and remove mapping macros here.
ON_NOTIFY_REFLECT(NM_CUSTOMDRAW,OnNMCustomdraw) //这里就是消息映射了。
//}}AFX_MSG_MAP
END_MESSAGE_MAP() /////////////////////////////////////////////////////////////////////////////
// CColorListCtrl message handlers void CColorListCtrl::PreSubclassWindow()
{
// TODO: Add your specialized code here and/or call the base class
CListCtrl::PreSubclassWindow();
} void CColorListCtrl::OnNMCustomdraw(NMHDR *pNMHDR, LRESULT *pResult) //获取消息后的实现代码。
{
NMLVCUSTOMDRAW* pLVCD = reinterpret_cast<NMLVCUSTOMDRAW*>( pNMHDR ); //首先声明一个NMLVCUSTOMDRAW结构体的指针pLVCD,关联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 ) //仅当pLVCD结构体中nmcd成员的dwDrawStage状态为CDDS_ITEMPREPAINT | CDDS_SUBITEM时
{ //我们就可以判断“行”和“列”,从而来设置文字颜色和文字背景颜色了。
// This is the prepaint stage for a subitem. Here's where we set the
// item's text and background colors. Our return value will tell
// Windows to draw the subitem itself, but it will use the new colors
// we set here.
if ( m_iCol == pLVCD->iSubItem && m_iRow == pLVCD->nmcd.dwItemSpec)
{
pLVCD->clrTextBk = m_Color;
pLVCD->clrText = m_TextColor; //SetFont(m_Font, false);
}
else
{
pLVCD->clrTextBk = ;//如果不是选择的“行”和“列”就设置成系统默认的那种颜色。
pLVCD->clrText = ;
}
//SetFont(m_Font, false);
// Store the colors back in the NMLVCUSTOMDRAW struct.
// Tell Windows to paint the control itself.
*pResult = CDRF_DODEFAULT;
}
} void CColorListCtrl::SetColor(int iRow, int iCol, COLORREF color)
{
m_iRow = iRow;
m_iCol = iCol;
m_Color = color;
/*m_vctRow.push_back(iRow);
m_vctCol.push_back(iCol);
m_vctColorValue.push_back(color);*/
} void CColorListCtrl::SetSubItemFont(LOGFONT font, COLORREF color, long lsize)
{
if ( m_Font ) delete m_Font; //删除旧字体
m_Font = new CFont;
m_Font->CreateFontIndirect(&font); m_TextColor = color;
m_TextSize = lsize;
}
最后,要使用的时候(及要改变列表框中文字颜色或文字背景颜色的时候),我们只需要调用这个派生类的对象的这两个Public方法,把颜色传入。如下:
void CTestDlg::OnApply()
{
// TODO: Add your control notification handler code here
CString strRow;
CString strCol;
GetDlgItem(IDC_EDIT1)->GetWindowText(strRow);
GetDlgItem(IDC_EDIT2)->GetWindowText(strCol);
int iRow = atoi(strRow);
int iCol = atoi(strCol);
// m_List.SetColor(iRow, iCol, m_color);
//m_List.SetItemColor(iRow, iCol, m_color);
m_List.SetColor(iRow, iCol, m_color);
m_List.SetSubItemFont(m_Font, m_TextColor, m_TextSize);
Invalidate(TRUE);
}
为了让我们的修改生效,必须调用Invalidate(TRUE);
////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////
下面代码实现列表框的选择项的颜色修改。。。。。。另一个消息相应函数。
void CXListCtrl::OnNMCustomdraw(NMHDR *pNMHDR, LRESULT *pResult)
{
/*修改内容:
改用switch case语句
case语句中增加了CDDS_ITEMPREPAINT和CDDS_ITEMPOSTPAINT两个状态判断
同时在改变颜色时增加了一个函数SetItemState
*/
static BOOL s_bThisItemSelect = FALSE;
NMLVCUSTOMDRAW* pLVCD = reinterpret_cast<LPNMLVCUSTOMDRAW>(pNMHDR);
int nItemIndex=pLVCD->nmcd.dwItemSpec; *pResult = CDRF_DODEFAULT;
switch (pLVCD->nmcd.dwDrawStage)
{
case CDDS_PREPAINT:
*pResult = CDRF_NOTIFYITEMDRAW;
break;
case CDDS_ITEMPREPAINT:
{
UINT ordin = pLVCD->nmcd.lItemlParam;
s_bThisItemSelect = FALSE;
//选中行颜色改变
if (GetItemState(nItemIndex,LVIS_SELECTED) == LVIS_SELECTED/*pLVCD->nmcd.uItemState & CDIS_SELECTED*/)
{
s_bThisItemSelect = TRUE;
//增加了下面这个SetItemState函数
SetItemState(pLVCD->nmcd.dwItemSpec, , LVIS_SELECTED);
pLVCD->clrText = RGB(,,)/*m_SelectItemTextColor*/;
pLVCD->clrTextBk =RGB(,,)/*m_SelectItemBkColor*/;
} *pResult = CDRF_NOTIFYPOSTPAINT;
}
break;
//加了一个状体判断
case CDDS_ITEMPOSTPAINT:
if (s_bThisItemSelect)
SetItemState(pLVCD->nmcd.dwItemSpec, 0xFF, LVIS_SELECTED);
break;
} }
VC改变CListCtrl 表格中文字颜色,和背景颜色。的更多相关文章
- 神奇的 CSS,让文字智能适配背景颜色
最近几天,有好几个同学都问了同样一个问题. 页面上有一段文本,能否实现这段文本在不同背景色下展示不同的颜色?也就是俗称的智能变色.像是下面这样: 文本在黑色底色上表现为白色,在白色底色上表现为黑色.看 ...
- [转]如何在Windows 10中更改文件夹背景颜色
ini文件.我们甚至可以使用相同的技术将图片设置为文件夹背景. 已有工具可以更改Windows 7中Windows资源管理器背景的颜色,并将图像设置为Windows 7中的文件夹背景,但这些工具与Wi ...
- []如何在Windows 10中更改文件夹背景颜色
ini文件.我们甚至可以使用相同的技术将图片设置为文件夹背景. 已有工具可以更改Windows 7中Windows资源管理器背景的颜色,并将图像设置为Windows 7中的文件夹背景,但这些工具与Wi ...
- 使用 JavaScript 中的 document 对象的属性,根据下拉框中选择的属性,更改页面中的字体颜色和背景颜色
查看本章节 查看作业目录 需求说明: 使用 JavaScript 中的 document 对象的属性,根据下拉框中选择的属性,更改页面中的字体颜色和背景颜色 实现思路: 在页面的 <body&g ...
- MFC中修改静态文本框中文字的字体、颜色
假设有一个静态文本框控件,其ID为:IDC_STATIC_XSDJ,且关联一个control类的CStatic类型的变量m_static_xsdj. 设置字体时自然要用到CFont类,下面介绍两种方法 ...
- 设置datagridview中button按钮的背景颜色
问题:DataGridViewButtonColumn()在datagridview中创建按钮列,如何设置按钮的背景颜色(不是单元格的背景颜色). 回答:可以在dataGridView1_CellPa ...
- RadioGroup 的 RadioButton 选择改变字体颜色和背景颜色
RadioGroup <RadioGroup android:id="@+id/client_charge_radiogroup" android:layout_width= ...
- 改变静态文本框和PictureControl的背景颜色
/************************************************************************/ /* 改变静态文本框和选择框的背景颜色 */ /* ...
- Android RadioGroup的RadioButton 选择改变字体颜色和背景颜色
RadioGroup <RadioGroup android:id="@+id/client_charge_radiogroup" android:layout_width= ...
随机推荐
- 遍历frame中的表单:
遍历frame中的表单: public void table1() { // 查找frame List<WebElement> iframes = driver.findElements( ...
- 转载免安装版mysql的配置
解压到自定义目录,我这里演示的是D:\wamp\mysql\ 复制根目录下的my-default.ini,改名为my.ini,my.ini用下面内容替换 #以下是复制内容,这行可不复制 [clie ...
- 【转】mysql force Index 强制索引
其他强制操作,优先操作如下: mysql常用的hint 对于经常使用oracle的朋友可能知道,oracle的hint功能种类很多,对于优化sql语句提供了很多方法.同样,在mysql里,也有类似的h ...
- 反向代理负载均衡-----nginx
一:集群 1.1:集群的概念 集群是一组相互独立的.通过高速网络互联的计算机,他们构成了一个组,并以单一系统的模式加以管理.一个客户与集群相互作用时,集群像是一个独立的服务器.集群配置是用于提高 ...
- [LeetCode] Search in Rotated Array II
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
- asp.net mvc4+EF 下使用UEditor
一.从官方网站下载UEditor,http://ueditor.baidu.com/website/download.html, 我下载的是1.53.net版本
- 出现脚本错误或者未正确调用 Page()
pages/xxxx/xxxx.js 出现脚本错误或者未正确调用 Page() 自己创建的小程序出现上面报错,可能是因为 xxxx.js是一个空文件,所以才会出现未正确调用: 如果是空文件的话,解决办 ...
- BZOJ 1799 同类分布(数位DP)
给出a,b,求出[a,b]中各位数字之和能整除原数的数的个数.1<=a<=b<=1e18. 注意到各位数字之和最大是153.考虑枚举这个东西.那么需要统计的是[0,a-1]和[0,b ...
- BZOJ3637 Query on a tree VI(树链剖分+线段树)
考虑对于每一个点维护子树内与其连通的点的信息.为了换色需要,记录每个点黑白两种情况下子树内连通块的大小. 查询时,找到深度最浅的同色祖先即可,这可以比较简单的树剖+线段树乱搞一下(似乎就是qtree3 ...
- 我为什么鼓励工程师写blog
文/JoeyChen 工程师该怎样才能突破自己的能力瓶颈?写 blog! 工程师该怎样精进自己在职涯上所需要的能力?写 blog! 工程师该怎样才能保持学习与成长的动能?写 blog! 工程师该怎样才 ...