我想把列表的奇数行和偶数行区分开来,但是不知道怎么做
现在想的是奇数行和偶数行的背景颜色不一样,请问该怎么做啊SetBkColor和SetTextColor是对整个控件有效的吧
谢谢
现在想的是奇数行和偶数行的背景颜色不一样,请问该怎么做啊SetBkColor和SetTextColor是对整个控件有效的吧
谢谢
解决方案 »
- 建立空项目开发MFC需要做什么准备?
- (174)求助:error RC2104 : undefined keyword or key name: ID_ColorToGray
- 浏览器工具栏怎样变成真彩色呀
- 如何遍历一个文件的内容,也就是逐条读取txt文件的内,txt文件中是一条记一行的!
- 谁知道怎么编程得到当前登陆用户的用户名和密码?50分相送!!!!!!
- 当一个窗口失去和得到输入焦点时有没有对应什么消息啊?
- 简单问题!opengl如何显示文字或数字
- 快 快 给偶讲讲safetarry 的用法 用中文讲哈:)
- 获得打开文件的大小是用ar的那个命令?
- 如何读取无线网卡信号强度?? 急!!!在线等!!!!
- 我用VC编一个Dll,编译中遇到很多标准库的错误,特来提问,100分献上!!
- 关于NM_CUSTOMDRAW 的控件重绘问题!
给你一个现成的例子http://www.vckbase.com/document/viewdoc/?id=891
把源文件包含到工程里
在定义变量的地方#include “sortlistctrl.h"
给你的控件ID关联一个CListCtrl对象把ClistCtrl用CSortListCtrl替换后可设置单元格的颜色 进行每行的单元格颜色设置可以达到你要的效果
#define AFX_COLORLISTCTRL_H__04C394FD_80E8_44D1_A263_F78C97A8D727__INCLUDED_#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// ColorListCtrl.h : header file
///////////////////////////////////////////////////////////////////////////////
// CColorListCtrl window
#include <afxtempl.h> // CColorListCtrl class CColorListCtrl : public CListCtrl
{
DECLARE_DYNAMIC(CColorListCtrl)
public:
CColorListCtrl();
virtual ~CColorListCtrl();
protected:
DECLARE_MESSAGE_MAP()
protected:
virtual void PreSubclassWindow();
public:
// virtual void DrawItem(LPDRAWITEMSTRUCT /*lpDrawItemStruct*/);
COLORREF SetItemColor(int nRow, int nCol, COLORREF clrItem);
COLORREF SetItemColor(LVITEM* plvItem = NULL, COLORREF clrItem = RGB(0, 0, 0));
private:
CList<COLORREF*, COLORREF*> m_lstItemColor;
public:
afx_msg void OnNMCustomdraw(NMHDR *pNMHDR, LRESULT *pResult);
}; ///////////////////////////////////////////////////////////////////////////////{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif // !defined(AFX_COLORLISTCTRL_H__04C394FD_80E8_44D1_A263_F78C97A8D727__INCLUDED_)// ColorListCtrl.cpp : implementation file
//#include "stdafx.h"
#include "ColorListCtrl.h"#include ".\colorlistctrl.h" #ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif// CColorListCtrl IMPLEMENT_DYNAMIC(CColorListCtrl, CListCtrl)
CColorListCtrl::CColorListCtrl()
{
} CColorListCtrl::~CColorListCtrl()
{
POSITION pos = m_lstItemColor.GetHeadPosition();
while (pos)
{
delete []m_lstItemColor.GetNext(pos);
}
} BEGIN_MESSAGE_MAP(CColorListCtrl, CListCtrl)
ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnNMCustomdraw)
END_MESSAGE_MAP() void CColorListCtrl::PreSubclassWindow()
{
// TODO: 在此添加专用代码和/或调用基类
ModifyStyle(0, /*LVS_OWNERDRAWFIXED | */LVS_REPORT, 0);
CListCtrl::PreSubclassWindow();
} COLORREF CColorListCtrl::SetItemColor(int nRow, int nCol, COLORREF clrItem)
{
int nCols = GetHeaderCtrl()->GetItemCount();
int nRows = GetItemCount();
if (nCol >= nCols || nRow >= nRows)
return COLORREF(-1);
for (int i = m_lstItemColor.GetCount(); i < nRow + 1; i++)
{
COLORREF *clrCol = new COLORREF[nCols];
ZeroMemory(clrCol, sizeof(clrCol));
m_lstItemColor.AddTail(clrCol);
}
for ( i = 0; i < nRows; i++)
{
if (i == nRow)
{
COLORREF *clrCol = m_lstItemColor.GetAt(m_lstItemColor.FindIndex(nRow));
COLORREF clrRet = clrCol[nCol];
clrCol[nCol] = clrItem;
return clrRet;
}
}
return COLORREF(-1);
}
COLORREF CColorListCtrl::SetItemColor(LVITEM* plvItem, COLORREF clrItem)
{
if (!plvItem)
{
return COLORREF(0);
}
int nRow = plvItem->iItem;
int nCol = plvItem->iSubItem;
return SetItemColor(nCol, nRow, clrItem);
} void CColorListCtrl::OnNMCustomdraw(NMHDR *pNMHDR, LRESULT *pResult)
{
LPNMLVCUSTOMDRAW pNMCD = reinterpret_cast<LPNMLVCUSTOMDRAW>(pNMHDR);
// TODO: 在此添加控件通知处理程序代码
*pResult = 0;
int nRows = m_lstItemColor.GetCount();
int nRow = int(pNMCD->nmcd.dwItemSpec);
COLORREF *clrCol = NULL;
if (nRows && nRow < nRows)
{
clrCol = m_lstItemColor.GetAt(m_lstItemColor.FindIndex(nRow));
}
switch(pNMCD->nmcd.dwDrawStage)
{
case CDDS_PREPAINT :
*pResult = CDRF_NOTIFYITEMDRAW;
return;
case CDDS_ITEMPREPAINT:
if (clrCol)
pNMCD->clrText = clrCol[0];
*pResult = CDRF_NOTIFYSUBITEMDRAW;
return;
case CDDS_SUBITEM | CDDS_ITEMPREPAINT:
if (clrCol)
pNMCD->clrText = clrCol[pNMCD->iSubItem];
*pResult = CDRF_NEWFONT;
return;
}
} /////////////////////////////////////////////////////////////////////////////
// CColorListCtrl message handlers
一个是ColorListCtrl.cpp文件
里面有楼主想要的答案