各位大侠,请问怎么在list control 中比如有四条记录,分别用不同的颜色显示,可以吗? 对于LVS_REPORT风格的列表框,重载DrawItem显然很容易解决. 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 这是我的一个程序:头文件:class CGSListBox : public CListBox{// Constructionpublic: CGSListBox();// Attributespublic:// Operationspublic:// Overrides // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CGSListBox) public: virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct); virtual void MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct); //}}AFX_VIRTUAL// Implementationpublic: void DrawGSItem(int nIndex, CDC* pDC, CRect& rect, int nType = 0); int AddGSData(GSObjData* pGSData); virtual ~CGSListBox(); // Generated message map functionsprotected: //{{AFX_MSG(CGSListBox) // NOTE - the ClassWizard will add and remove member functions here. //}}AFX_MSG DECLARE_MESSAGE_MAP()};实现:CGSListBox::CGSListBox(){}CGSListBox::~CGSListBox(){}BEGIN_MESSAGE_MAP(CGSListBox, CListBox) //{{AFX_MSG_MAP(CGSListBox) // NOTE - the ClassWizard will add and remove mapping macros here. //}}AFX_MSG_MAPEND_MESSAGE_MAP()void CGSListBox::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) { CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC); CRect rect = lpDrawItemStruct->rcItem; int nIndex = lpDrawItemStruct->itemID; // Set the background mode to TRANSPARENT. int nOldMode = pDC->SetBkMode(TRANSPARENT); BOOL bSel = FALSE; if ((lpDrawItemStruct->itemState & ODS_SELECTED)) // Item has been selected - hilite frame. bSel = TRUE; if (!(lpDrawItemStruct->itemState & ODS_SELECTED)) // Item has been unselected. bSel = FALSE; CBrush br(RGB(50, 50, 220)); pDC->FillRect(rect, &br); DrawGSItem(nIndex, pDC, rect); if((lpDrawItemStruct->itemState & ODS_SELECTED)) pDC->InvertRect(rect);// if ((lpDrawItemStruct->itemState & ODS_FOCUS))// pDC->DrawFocusRect(rect); // Set the background to old mode. pDC->SetBkMode(nOldMode);}void CGSListBox::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct) { lpMeasureItemStruct->itemHeight = 44;}int CGSListBox::AddGSData(GSObjData *pGSObjData){ int nItem = AddString(_T("无")); if(nItem != LB_ERR) { SetItemData(nItem, (DWORD)pGSObjData); return nItem; } return 0;}void CGSListBox::DrawGSItem(int nIndex, CDC *pDC, CRect &rect, int nType){ if(nIndex < 0 || nIndex >= GetCount()) return ; GSObjData* pGSObjData = (GSObjData*)GetItemData(nIndex); ASSERT(pGSObjData); int nLeft = 4; int nLength = 46; CString strIndex, strCode, strName, strStart, strEnd, strPer, strTotal; strIndex.Format("%03d", nIndex + 1); strCode = pGSObjData->m_strGSCode; strName = pGSObjData->m_strGSName; strStart = pGSObjData->m_strGSStartData; strEnd = pGSObjData->m_strGSEndData; strPer = pGSObjData->m_strGSPercentData; strTotal = pGSObjData->m_strGSTotalData; CFont newFont; newFont.CreateFont(22, 0, 0, 0, 700 , 0, 0, 0, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, PROOF_QUALITY, VARIABLE_PITCH | FF_DONTCARE, "Fixedsys"); CFont* oldFont = pDC->SelectObject(&newFont); pDC->SetTextColor(RGB(255, 255, 255)); pDC->DrawText(strIndex, CRect(rect.left + nLeft, rect.top, rect.left + nLength, rect.bottom), DT_LEFT | DT_VCENTER | DT_SINGLELINE); nLeft = 50; nLength = 80; pDC->SetTextColor(RGB(255, 255, 0)); pDC->DrawText(strCode, CRect(rect.left + nLeft, rect.top, rect.left + nLeft + nLength, rect.bottom), DT_CENTER | DT_VCENTER | DT_SINGLELINE); nLeft = 130; nLength = 90; pDC->SetTextColor(RGB(255, 255, 180)); pDC->DrawText(strName, CRect(rect.left + nLeft, rect.top, rect.left + nLeft + nLength, rect.bottom), DT_CENTER | DT_VCENTER | DT_SINGLELINE); nLeft = 220; nLength = 80; if(pGSObjData->m_bIsStop) { pDC->SetTextColor(RGB(255, 255, 255)); } else { if(GSObjData::StringToDouble(strEnd) < GSObjData::StringToDouble(strStart)) pDC->SetTextColor(RGB(255, 0, 0)); else if(GSObjData::StringToDouble(strEnd) > GSObjData::StringToDouble(strStart)) pDC->SetTextColor(RGB(0, 255, 0)); else pDC->SetTextColor(RGB(255, 255, 255)); } pDC->DrawText(strEnd, CRect(rect.left + nLeft, rect.top, rect.left + nLeft + nLength, rect.bottom), DT_RIGHT | DT_VCENTER | DT_SINGLELINE); nLeft = 300; nLength = 80; pDC->DrawText(strStart, CRect(rect.left + nLeft, rect.top, rect.left + nLeft + nLength, rect.bottom), DT_RIGHT | DT_VCENTER | DT_SINGLELINE); nLeft = 380; nLength = 80; pDC->DrawText(strPer, CRect(rect.left + nLeft, rect.top, rect.left + nLeft + nLength, rect.bottom), DT_RIGHT | DT_VCENTER | DT_SINGLELINE); nLeft = 460; nLength = 120; pDC->DrawText(strTotal, CRect(rect.left + nLeft, rect.top, rect.left + nLeft + nLength, rect.bottom), DT_RIGHT | DT_VCENTER | DT_SINGLELINE); rect.bottom -= 2; CPen newPen1(PS_SOLID, 1, RGB(128, 128, 128)); CPen newPen2(PS_SOLID, 1, RGB(192, 192, 192)); pDC->SelectObject(&newPen2); pDC->MoveTo(rect.left, rect.bottom); pDC->LineTo(rect.right, rect.bottom); pDC->SelectObject(&newPen1); rect.bottom += 1; pDC->MoveTo(rect.left, rect.bottom); pDC->LineTo(rect.right, rect.bottom);}CListCtrl要有owner draw风格:Variable可以 vc 中TiXmlElement类读取不到'&'这个符号咋办,大侠们求救啊 MFC 控制台输出 如何将正确获取unsigned char*的长度以及将其正确转为CString? CTreeView控件 初始化 请问用最大二维熵二值化图象中得到的阀值向量(S,T)后,如何对每个向素分类啊。 如何将CString变量通过自定义消息发送出去? 失恋,散分,无须多言 我又来发发贴:详情如下? DZYMZ 用人用过 IAMStreamConfig吗? 为什么我不能设置视频的ColorSpace为RGB呢? 只有YUY2.请高手指教! 200分 豁出去了?? 一个简单问题,会给分的
头文件:
class CGSListBox : public CListBox
{
// Construction
public:
CGSListBox();// Attributes
public:// Operations
public:// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CGSListBox)
public:
virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
virtual void MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct);
//}}AFX_VIRTUAL// Implementation
public:
void DrawGSItem(int nIndex, CDC* pDC, CRect& rect, int nType = 0);
int AddGSData(GSObjData* pGSData);
virtual ~CGSListBox(); // Generated message map functions
protected:
//{{AFX_MSG(CGSListBox)
// NOTE - the ClassWizard will add and remove member functions here.
//}}AFX_MSG DECLARE_MESSAGE_MAP()
};实现:
CGSListBox::CGSListBox()
{
}CGSListBox::~CGSListBox()
{
}
BEGIN_MESSAGE_MAP(CGSListBox, CListBox)
//{{AFX_MSG_MAP(CGSListBox)
// NOTE - the ClassWizard will add and remove mapping macros here.
//}}AFX_MSG_MAP
END_MESSAGE_MAP()void CGSListBox::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
CRect rect = lpDrawItemStruct->rcItem;
int nIndex = lpDrawItemStruct->itemID;
// Set the background mode to TRANSPARENT.
int nOldMode = pDC->SetBkMode(TRANSPARENT);
BOOL bSel = FALSE;
if ((lpDrawItemStruct->itemState & ODS_SELECTED))
// Item has been selected - hilite frame.
bSel = TRUE;
if (!(lpDrawItemStruct->itemState & ODS_SELECTED))
// Item has been unselected.
bSel = FALSE;
CBrush br(RGB(50, 50, 220));
pDC->FillRect(rect, &br);
DrawGSItem(nIndex, pDC, rect);
if((lpDrawItemStruct->itemState & ODS_SELECTED))
pDC->InvertRect(rect);
// if ((lpDrawItemStruct->itemState & ODS_FOCUS))
// pDC->DrawFocusRect(rect);
// Set the background to old mode.
pDC->SetBkMode(nOldMode);
}void CGSListBox::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct)
{
lpMeasureItemStruct->itemHeight = 44;
}int CGSListBox::AddGSData(GSObjData *pGSObjData)
{
int nItem = AddString(_T("无"));
if(nItem != LB_ERR)
{
SetItemData(nItem, (DWORD)pGSObjData);
return nItem;
}
return 0;
}void CGSListBox::DrawGSItem(int nIndex, CDC *pDC, CRect &rect, int nType)
{
if(nIndex < 0 || nIndex >= GetCount())
return ;
GSObjData* pGSObjData = (GSObjData*)GetItemData(nIndex);
ASSERT(pGSObjData);
int nLeft = 4;
int nLength = 46;
CString strIndex, strCode, strName, strStart, strEnd, strPer, strTotal; strIndex.Format("%03d", nIndex + 1);
strCode = pGSObjData->m_strGSCode;
strName = pGSObjData->m_strGSName;
strStart = pGSObjData->m_strGSStartData;
strEnd = pGSObjData->m_strGSEndData;
strPer = pGSObjData->m_strGSPercentData;
strTotal = pGSObjData->m_strGSTotalData; CFont newFont;
newFont.CreateFont(22, 0, 0, 0, 700 , 0, 0, 0,
ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
PROOF_QUALITY, VARIABLE_PITCH | FF_DONTCARE, "Fixedsys");
CFont* oldFont = pDC->SelectObject(&newFont); pDC->SetTextColor(RGB(255, 255, 255));
pDC->DrawText(strIndex,
CRect(rect.left + nLeft, rect.top, rect.left + nLength, rect.bottom),
DT_LEFT | DT_VCENTER | DT_SINGLELINE);
nLeft = 50;
nLength = 80;
pDC->SetTextColor(RGB(255, 255, 0));
pDC->DrawText(strCode,
CRect(rect.left + nLeft, rect.top, rect.left + nLeft + nLength, rect.bottom),
DT_CENTER | DT_VCENTER | DT_SINGLELINE);
nLeft = 130;
nLength = 90;
pDC->SetTextColor(RGB(255, 255, 180));
pDC->DrawText(strName,
CRect(rect.left + nLeft, rect.top, rect.left + nLeft + nLength, rect.bottom),
DT_CENTER | DT_VCENTER | DT_SINGLELINE);
nLeft = 220;
nLength = 80;
if(pGSObjData->m_bIsStop)
{
pDC->SetTextColor(RGB(255, 255, 255));
}
else
{
if(GSObjData::StringToDouble(strEnd) < GSObjData::StringToDouble(strStart))
pDC->SetTextColor(RGB(255, 0, 0));
else if(GSObjData::StringToDouble(strEnd) > GSObjData::StringToDouble(strStart))
pDC->SetTextColor(RGB(0, 255, 0));
else
pDC->SetTextColor(RGB(255, 255, 255));
}
pDC->DrawText(strEnd,
CRect(rect.left + nLeft, rect.top, rect.left + nLeft + nLength, rect.bottom),
DT_RIGHT | DT_VCENTER | DT_SINGLELINE);
nLeft = 300;
nLength = 80;
pDC->DrawText(strStart,
CRect(rect.left + nLeft, rect.top, rect.left + nLeft + nLength, rect.bottom),
DT_RIGHT | DT_VCENTER | DT_SINGLELINE);
nLeft = 380;
nLength = 80;
pDC->DrawText(strPer,
CRect(rect.left + nLeft, rect.top, rect.left + nLeft + nLength, rect.bottom),
DT_RIGHT | DT_VCENTER | DT_SINGLELINE);
nLeft = 460;
nLength = 120;
pDC->DrawText(strTotal,
CRect(rect.left + nLeft, rect.top, rect.left + nLeft + nLength, rect.bottom),
DT_RIGHT | DT_VCENTER | DT_SINGLELINE);
rect.bottom -= 2;
CPen newPen1(PS_SOLID, 1, RGB(128, 128, 128));
CPen newPen2(PS_SOLID, 1, RGB(192, 192, 192));
pDC->SelectObject(&newPen2);
pDC->MoveTo(rect.left, rect.bottom);
pDC->LineTo(rect.right, rect.bottom);
pDC->SelectObject(&newPen1);
rect.bottom += 1;
pDC->MoveTo(rect.left, rect.bottom);
pDC->LineTo(rect.right, rect.bottom);
}CListCtrl要有owner draw风格:Variable可以