因为做的是工具软件,所以经常和不同的控件打交道,之前工具里面有个提示出错信息的提示框,用的人希望用不同的颜色来显示,这样方便查看,因此百度了一下,找到了下面这个基于listbox的类
// MulitLineListBox.cpp : implementation file
//
#include "stdafx.h"
#include "MulitLineListBox.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/
// CMulitLineListBox
CMulitLineListBox::CMulitLineListBox()
{
}
CMulitLineListBox::~CMulitLineListBox()
{
}
BEGIN_MESSAGE_MAP(CMulitLineListBox, CListBox)
//{{AFX_MSG_MAP(CMulitLineListBox)
ON_WM_DESTROY()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/
// CMulitLineListBox message handlers
void CMulitLineListBox::AppendString(LPCSTR lpszText, COLORREF fgColor, COLORREF bgColor)
{
LISTBOX_COLOR* pInfo = new LISTBOX_COLOR;
pInfo->strText.Format(_T("%s"), lpszText);
pInfo->fgColor = fgColor;
pInfo->bgColor = bgColor;
SetItemDataPtr(AddString(pInfo->strText), pInfo);
}
void CMulitLineListBox::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct)
{
// TODO: Add your code to determine the size of specified item
ASSERT(lpMeasureItemStruct->CtlType == ODT_LISTBOX);
CString strText(_T(""));
GetText(lpMeasureItemStruct->itemID, strText);
ASSERT(TRUE != strText.IsEmpty());
CRect rect;
GetItemRect(lpMeasureItemStruct->itemID, &rect);
CDC* pDC = GetDC();
lpMeasureItemStruct->itemHeight = pDC->DrawText(strText, -1, rect, DT_WORDBREAK | DT_CALCRECT);
ReleaseDC(pDC);
}
void CMulitLineListBox::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
// TODO: Add your code to draw the specified item
ASSERT(lpDrawItemStruct->CtlType == ODT_LISTBOX);
if(lpDrawItemStruct->itemID == -1)
return;
LISTBOX_COLOR* pListBox = (LISTBOX_COLOR*)GetItemDataPtr(lpDrawItemStruct->itemID);
ASSERT(NULL != pListBox);
CDC dc;
dc.Attach(lpDrawItemStruct->hDC);
// Save these value to restore them when done drawing.
COLORREF crOldTextColor = dc.GetTextColor();
COLORREF crOldBkColor = dc.GetBkColor();
// If this item is selected, set the background color
// and the text color to appropriate values. Also, erase
// rect by filling it with the background color.
if ((lpDrawItemStruct->itemAction | ODA_SELECT) &&
(lpDrawItemStruct->itemState & ODS_SELECTED))
{
//dc.SetTextColor(pListBox->bgColor);
dc.SetBkColor(pListBox->fgColor);
dc.FillSolidRect(&lpDrawItemStruct->rcItem, pListBox->fgColor);
}
else
{
//dc.SetTextColor(pListBox->fgColor);
dc.SetBkColor(pListBox->bgColor);
dc.FillSolidRect(&lpDrawItemStruct->rcItem, pListBox->bgColor);
}
lpDrawItemStruct->rcItem.left += 5;
// Draw the text.
dc.DrawText(pListBox->strText, pListBox->strText.GetLength(), &lpDrawItemStruct->rcItem, DT_WORDBREAK);
// Reset the background color and the text color back to their
// original values.
dc.SetTextColor(crOldTextColor);
dc.SetBkColor(crOldBkColor);
dc.Detach();
}
void CMulitLineListBox::OnDestroy()
{
int nCount = GetCount();
for(int i=0; i<nCount; i++)
{
LISTBOX_COLOR* pList = (LISTBOX_COLOR*)GetItemDataPtr(i);
delete pList;
pList = NULL;
}
CListBox::OnDestroy();
// TODO: Add your message handler code here
}
#if !defined(AFX_MULITLINELISTBOX_H__D705CB99_9FD0_424E_BD71_027547449AE5__INCLUDED_)
#define AFX_MULITLINELISTBOX_H__D705CB99_9FD0_424E_BD71_027547449AE5__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// MulitLineListBox.h : header file
//
/
// CMulitLineListBox window
typedef struct _LISTBOX_COLOR_
{
CString strText;
COLORREF fgColor;
COLORREF bgColor;
_LISTBOX_COLOR_()
{
strText.Empty();
fgColor = RGB(0, 0, 0);
bgColor = RGB(255, 255, 255);
}
}LISTBOX_COLOR, *PLISTBOX_COLOR;
class CMulitLineListBox : public CListBox
{
// Construction
public:
CMulitLineListBox();
// Attributes
public:
void AppendString(LPCSTR lpszText, COLORREF fgColor, COLORREF bgColor);
// Operations
public:
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CMulitLineListBox)
public:
virtual void MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct);
virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
//}}AFX_VIRTUAL
// Implementation
public:
virtual ~CMulitLineListBox();
// Generated message map functions
protected:
//{{AFX_MSG(CMulitLineListBox)
afx_msg void OnDestroy();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
/
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_MULITLINELISTBOX_H__D705CB99_9FD0_424E_BD71_027547449AE5__INCLUDED_)
使用起来也比较简单
void OnLogMsg(WPARAM wParam, LPARAM lParam)
{
CHAR* MsgBuff = (CHAR*)wParam;
CString strText = "";
COLORREF fgColor;
COLORREF bgColor;
strText.Format("%s",MsgBuff);
fgColor = RGB(20, 100, 224);
if(strText.Find("错误")!=-1)//根据不同的提示信息给出不同的颜色
{
bgColor = RGB(255, 0, 0);
}
else
{
bgColor = RGB(255, 255, 255);
}
m_LogMsgList.AppendString(MsgBuff, fgColor, bgColor);
int count = m_LogMsgList.GetCount();
if (count > 2048)
{
for (int i = 0; i < 1024; i++)
m_LogMsgList.DeleteString(0);
}
count = m_LogMsgList.GetCount();
m_LogMsgList.SetTopIndex(count-1);
m_LogMsgList.SetCurSel(count-1);
}
上面的CMulitLineListBox.cpp和我在网上下载的做了一点小小的修改,因为网上下载的有一个毛病,就是listbox被放在非模态对话框中显示,并且里面没有内容的时候会报错,后来增加了if(lpDrawItemStruct->itemID == -1)
return;这2句,来先对有没有内容进行判断。