65,209
社区成员
发帖
与我相关
我的任务
分享#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;
}
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;
}
多态用反了。