急急急!!!改变CListCtrl某行的颜色代码报错

xiaobudiann0918 2014-04-04 12:42:50
想改变改变CListCtrl某行的颜色,使用了网上的代码(http://blog.csdn.net/ribut9225/article/details/6720639)
具体代码是
// aDlg.h : header file
//

#include <afxtempl.h>

#if !defined(AFX_ADLG_H__18E3D009_226C_435D_B9DA_B18D41A77DE3__INCLUDED_)
#define AFX_ADLG_H__18E3D009_226C_435D_B9DA_B18D41A77DE3__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

/////////////////////////////////////////////////////////////////////////////
// CADlg dialog

/////////////////////////////////////////////////////////////////////////////
// CLineColorListCtrl window

class CMyList : public CListCtrl
{

public:
CMyList(void);
DECLARE_MESSAGE_MAP()

public:
~CMyList();
afx_msg void OnNMCustomdraw(NMHDR *pNMHDR, LRESULT *pResult);
void SetItemColor(DWORD iItem, COLORREF color);

CMap<DWORD , DWORD& , COLORREF , COLORREF&> MapItemColor;

};

class CADlg : public CDialog
{
// Construction
public:
CADlg(CWnd* pParent = NULL); // standard constructor

// Dialog Data
//{{AFX_DATA(CADlg)
enum { IDD = IDD_A_DIALOG };
CListCtrl m_listctrl1;
//}}AFX_DATA

// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CADlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL

// Implementation
protected:
HICON m_hIcon;

// Generated message map functions
//{{AFX_MSG(CADlg)
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_ADLG_H__18E3D009_226C_435D_B9DA_B18D41A77DE3__INCLUDED_)


// aDlg.cpp : implementation file
//

#include "stdafx.h"
#include "a.h"
#include "aDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

CMyList::CMyList(void)
{
}

CMyList::~CMyList(void)
{
}

BEGIN_MESSAGE_MAP(CMyList, CListCtrl)

//{{AFX_MSG_MAP(CMyList)
// NOTE - the ClassWizard will add and remove mapping macros here.
//}}AFX_MSG_MAP
ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnNMCustomdraw)//必须要加的语句
END_MESSAGE_MAP()

void CMyList::OnNMCustomdraw(NMHDR *pNMHDR, LRESULT *pResult)
{
// TODO: Add your control notification handler code here
*pResult = CDRF_DODEFAULT;
NMLVCUSTOMDRAW * lplvdr=(NMLVCUSTOMDRAW*)pNMHDR;
NMCUSTOMDRAW &nmcd = lplvdr->nmcd;

switch(lplvdr->nmcd.dwDrawStage)
{
case CDDS_PREPAINT:

*pResult = CDRF_NOTIFYITEMDRAW;
break;
case CDDS_ITEMPREPAINT:
{
COLORREF ItemColor;
if( MapItemColor.Lookup(nmcd.dwItemSpec, ItemColor))
{
lplvdr->clrText = ItemColor;
*pResult = CDRF_DODEFAULT;
}
}
break;
}
}

void CMyList::SetItemColor(DWORD iItem, COLORREF color)
{
// m_iCurrentItem = iItem;
// m_CurrentColor = color;

MapItemColor.SetAt(iItem, color);
this->RedrawItems(iItem, iItem);
UpdateWindow();

}

class CAboutDlg : public CDialog
{
public:
CAboutDlg();

// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA

// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL

// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CADlg dialog

CADlg::CADlg(CWnd* pParent /*=NULL*/)
: CDialog(CADlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CADlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CADlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CADlg)
DDX_Control(pDX, IDC_LIST1, m_listctrl1);
//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CADlg, CDialog)
//{{AFX_MSG_MAP(CADlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CADlg message handlers

BOOL CADlg::OnInitDialog()
{
CDialog::OnInitDialog();

// Add "About..." menu item to system menu.

// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);

CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}

// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon

// TODO: Add extra initialization here

COLORREF red = RGB(255,0,0);
m_listctrl1.SetItemColor(1,red);

return TRUE; // return TRUE unless you set the focus to a control
}

其中,m_listctrl1为CListCtrl控件关联的变量,编译时报错:
:\code\a\aDlg.cpp(174) : error C2039: 'SetItemColor' : is not a member of 'CListCtrl'
c:\program files (x86)\microsoft visual studio\vc98\mfc\include\afxcmn.h(177) : see declaration of 'CListCtrl'
Error executing cl.exe.

请问如何解决这个问题?谢谢!!!
...全文
200 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
晴天33 2014-05-05
  • 打赏
  • 举报
回复
这个问题我也遇到了,其实就是控件绑定的变量错了。
「已注销」 2014-04-05
  • 打赏
  • 举报
回复
vecSift是全局变量,存放的是特殊颜色的行号
「已注销」 2014-04-05
  • 打赏
  • 举报
回复
之前用过一个继承改写CListCtrl类: ColoredListCtrl
#if !defined(AFX_COLOREDLISTCTRL_H__86FEBE8E_F6FA_429F_B740_76F1769C81C6__INCLUDED_)
#define AFX_COLOREDLISTCTRL_H__86FEBE8E_F6FA_429F_B740_76F1769C81C6__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// ColoredListCtrl.h : header file
//

/////////////////////////////////////////////////////////////////////////////
// CColoredListCtrl window


class CColoredListCtrl : public CListCtrl
{
// Construction
public:
	CColoredListCtrl();

// Attributes
public:
	COLORREF m_colRow1;
	COLORREF m_colRow2;
	COLORREF m_colRowSelected;


// Operations
public:

// Overrides
	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CColoredListCtrl)
	//}}AFX_VIRTUAL

// Implementation
public:
	virtual ~CColoredListCtrl();
	afx_msg void CColoredListCtrl::OnCustomDraw(NMHDR* pNMHDR, LRESULT* pResult);

	// Generated message map functions
protected:
	//{{AFX_MSG(CColoredListCtrl)
	afx_msg BOOL OnEraseBkgnd(CDC* pDC);	
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

/////////////////////////////////////////////////////////////////////////////

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_COLOREDLISTCTRL_H__86FEBE8E_F6FA_429F_B740_76F1769C81C6__INCLUDED_)
ColoredListCtrl.cpp:
// ColoredListCtrl.cpp : implementation file
//

#include "stdafx.h"
#include "ColoredListCtrl.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CColoredListCtrl

CColoredListCtrl::CColoredListCtrl()
{
	// 奇偶行颜色值,
	//m_colRow1 = RGB(223, 236, 245);
	m_colRow2 = RGB(250, 250, 250);

	// 奇偶颜色设置为相同
	m_colRow1 = m_colRow2; 
	m_colRowSelected =RGB(252, 205, 74);
}

CColoredListCtrl::~CColoredListCtrl()
{
}

BEGIN_MESSAGE_MAP(CColoredListCtrl, CListCtrl)
	//{{AFX_MSG_MAP(CColoredListCtrl)
	ON_WM_ERASEBKGND()
	ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnCustomDraw)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CColoredListCtrl message handlers

void CColoredListCtrl::OnCustomDraw(NMHDR* pNMHDR, LRESULT* pResult)
{
  *pResult = 0;

  LPNMLVCUSTOMDRAW  lplvcd = (LPNMLVCUSTOMDRAW)pNMHDR;
  int iRow = lplvcd->nmcd.dwItemSpec;

  switch(lplvcd->nmcd.dwDrawStage)
  {
    case CDDS_PREPAINT :
    {
      *pResult = CDRF_NOTIFYITEMDRAW;
      return;
    }

    // Modify item text and or background
    case CDDS_ITEMPREPAINT:
		{
			lplvcd->clrText = RGB(0,0,0);
			// If you want the sub items the same as the item,
			// set *pResult to CDRF_NEWFONT
			*pResult = CDRF_NOTIFYSUBITEMDRAW;
			return;
		}

    // Modify sub item text and/or background
    case CDDS_SUBITEM | CDDS_PREPAINT | CDDS_ITEM:
    {
		// 奇偶行颜色设置		
        if(iRow % 2){
         lplvcd->clrTextBk = m_colRow2;
        }
        else{
          lplvcd->clrTextBk = m_colRow1;
        }
		if( !vecSift.empty() ){
						
			for (vector<int>::iterator iter = vecSift.begin(); 
			iter != vecSift.end(); ++iter )
			{
				if (iRow == *iter)
				{
					lplvcd->clrTextBk = m_colRowSelected; //选中行背景色	
				}
			}	
 		}
		
        *pResult = CDRF_DODEFAULT;

        return;
    }
  }
   
}

BOOL CColoredListCtrl::OnEraseBkgnd(CDC* pDC) 
{
	// TODO: Add your message handler code here and/or call default
	
  CRect rect;
  CColoredListCtrl::GetClientRect(rect);


  POINT mypoint;  
  
  CBrush brush0(m_colRow1);
  CBrush brush1(m_colRow2);


 
 int chunk_height=GetCountPerPage();
 pDC->FillRect(&rect,&brush1);

 for (int i=0;i<=chunk_height;i++)
 {
	GetItemPosition(i,&mypoint);
	rect.top=mypoint.y ;
	GetItemPosition(i+1,&mypoint);
	rect.bottom =mypoint.y;
	pDC->FillRect(&rect,i %2 ? &brush1 : &brush0);


 }

  brush0.DeleteObject();
  brush1.DeleteObject();

  return FALSE;
}
yiyefangzhou24 2014-04-05
  • 打赏
  • 举报
回复
重写OnCtlColor
HBRUSH CxxDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
    HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
    // TODO:  在此更改 DC 的任何属性
    switch (pWnd->GetDlgCtrlID())
    {
    case IDC_LIST_xx:
        pDC->SetBkColor(RGB(255,100,100));
        pDC->SetTextColor(RGB(255,0,0));
        return(HBRUSH)GetStockObject(NULL_BRUSH);
    case IDC_STATIC_xx:
        pDC->SetTextColor(RGB(125,125,255));
        return(HBRUSH)GetStockObject(NULL_BRUSH);
    }
     
    return hbr;
}
zbo_stepping 2014-04-04
  • 打赏
  • 举报
回复
多态用反了。
zbo_stepping 2014-04-04
  • 打赏
  • 举报
回复
。。。m_listctrl1是CListCtrl控件关联的变量 又不是CMyList的 你去调CMyList的成员函数!
xiaobudiann0918 2014-04-04
  • 打赏
  • 举报
回复
因为直接调用CMyList内的成员函数SetItemColor的话,控件的句柄传不过去,很纠结,求助啊!!!
xiaobudiann0918 2014-04-04
  • 打赏
  • 举报
回复
对,m_listctrl1的确是CListCtrl控件关联的变量,不是CMyList的。但是我现在想调用CMyList内的成员函数SetItemColor,不知道如何调用啊。。。

65,209

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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