<实现不同行字体颜色分配>
<此为项目文件中引用,因此根据要求appendString函数设置了'fail'消息的显示颜色,其他操作大致相同>
头文件:
#pragma once #include "resource.h" #include <afxwin.h> class CMyListBox : public CListBox { public: CMyListBox(); virtual ~CMyListBox(); public: void AppendString(LPCTSTR lpszText); void AppendString(LPCTSTR lpszText, COLORREF fgColor, COLORREF bgColor = RGB(255, 255, 255)); void AddColorType(); public: virtual void MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct); virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct); protected: afx_msg void OnDestroy(); DECLARE_MESSAGE_MAP() };
cpp文件:
#include "stdafx.h" #include "MyListBox.h" CMyListBox::CMyListBox() {} CMyListBox::~CMyListBox() {} BEGIN_MESSAGE_MAP(CMyListBox, CListBox) ON_WM_DESTROY() END_MESSAGE_MAP() void CMyListBox::AppendString(LPCTSTR lpszText) { COLORREF fgColor; fgColor = RGB(0, 0, 0); CString strText(""); strText.Format(_T("%s"),lpszText); strText.Format(_T("%s"), lpszText); if( strText.Find(_T("Fail")) != -1 || strText.Find(_T("fail")) != -1 || strText.Find(_T("FAIL")) != -1 || strText.Find(_T("Err")) != -1 || strText.Find(_T("err")) != -1 ) { fgColor = RGB(255, 0, 0); } else if(strText.Find(_T("Warn")) != -1 || strText.Find(_T("WARN")) != -1 ) { fgColor = RGB(255, 255, 0); } int item = AddString(lpszText); if(item >= 0) { SetItemData(item, fgColor); } } void CMyListBox::AppendString(LPCTSTR lpszText, COLORREF fgColor, COLORREF bgColor) { CString strText(""); COLORREF textColor; strText.Format(_T("%s"), lpszText); int item = AddString(lpszText); textColor = fgColor; if(item >= 0) { SetItemData(item, textColor); } } void CMyListBox::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct) { ASSERT(lpMeasureItemStruct->CtlType == ODT_LISTBOX); CString strText(""); /*ASSERT(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 CMyListBox::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) { ASSERT(lpDrawItemStruct->CtlType == ODT_LISTBOX); if(lpDrawItemStruct->itemID < 0) { return; } COLORREF cvText; COLORREF cvBack; CString itemString; //if item has been selected if((lpDrawItemStruct->itemState & ODS_SELECTED) && (lpDrawItemStruct->itemAction & (ODA_SELECT | ODA_DRAWENTIRE))) { DrawFocusRect(lpDrawItemStruct->hDC, &lpDrawItemStruct->rcItem); } //if item has been selected if(!(lpDrawItemStruct->itemState & ODS_SELECTED) && (lpDrawItemStruct->itemAction & ODA_SELECT)) { DrawFocusRect(lpDrawItemStruct->hDC, &lpDrawItemStruct->rcItem); } if(lpDrawItemStruct->itemData) { cvText = SetTextColor(lpDrawItemStruct->hDC, lpDrawItemStruct->itemData); } else { // if no color information, use default system colors cvText = SetTextColor(lpDrawItemStruct->hDC, GetSysColor((lpDrawItemStruct->itemState & ODS_SELECTED) ? COLOR_HIGHLIGHTTEXT : COLOR_WINDOWTEXT)); } //always use system colors for background cvBack = SetBkColor(lpDrawItemStruct->hDC, GetSysColor((lpDrawItemStruct->itemState & ODS_SELECTED) ? COLOR_HIGHLIGHT : COLOR_WINDOW)); //get and display item text GetText(lpDrawItemStruct->itemID, itemString); DrawText(lpDrawItemStruct->hDC, itemString, -1, &lpDrawItemStruct->rcItem, DT_LEFT | DT_SINGLELINE); //restore DC colors SetTextColor(lpDrawItemStruct->hDC, cvText); SetBkColor(lpDrawItemStruct->hDC, cvBack); } void CMyListBox::OnDestroy() { CListBox::OnDestroy(); }
浙公网安备 33010602011771号