MFC自绘ListBox可根据条件定义不同行字体颜色

来源:互联网 发布:程序员分类 编辑:程序博客网 时间:2024/05/29 12:36

MFC自绘ListBox可根据条件定义不同行字体颜色

头文件

#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 windowtypedef 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{// Constructionpublic:    CMulitLineListBox();// Attributespublic:    void AppendString(LPCSTR lpszText, COLORREF fgColor, COLORREF bgColor);    //添加String 函数,可以自定义颜色// Operationspublic:// Overrides    // ClassWizard generated virtual function overrides    //{{AFX_VIRTUAL(CMulitLineListBox)    public:    virtual void MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct);    virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);  //重载自绘//  virtual BOOL Create(DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID);    //}}AFX_VIRTUAL// Implementationpublic:    virtual ~CMulitLineListBox();    // Generated message map functionsprotected:    //{{AFX_MSG(CMulitLineListBox)    afx_msg void OnDestroy();//  afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);    //}}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_)

原函数

// MulitLineListBox.cpp : implementation file//#include "stdafx.h"#include "CList.h"#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE[] = __FILE__;#endif/////////////////////////////////////////////////////////////////////////////// CMulitLineListBoxCMulitLineListBox::CMulitLineListBox(){}CMulitLineListBox::~CMulitLineListBox(){}BEGIN_MESSAGE_MAP(CMulitLineListBox, CListBox)    //{{AFX_MSG_MAP(CMulitLineListBox)    ON_WM_DESTROY()    //}}AFX_MSG_MAPEND_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////// CMulitLineListBox message handlersvoid 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);}// // int CMulitLineListBox::OnCreate(LPCREATESTRUCT lpCreateStruct) // {//     if (CListBox::OnCreate(lpCreateStruct) == -1)//        return -1;//        //      if ((GetStyle() & (LBS_OWNERDRAWFIXED | LBS_HASSTRINGS))//        == (LBS_OWNERDRAWFIXED | LBS_HASSTRINGS))//    SetItemHeight(-1, 12);//    //     return 0;// }// // BOOL CMulitLineListBox::Create(DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID) // {//     dwStyle |= LBS_NOINTEGRALHEIGHT | LBS_OWNERDRAWFIXED | LBS_HASSTRINGS;//  //  return CListBox::Create(dwStyle, rect, pParentWnd, nID);// }// 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);    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 }
0 0
原创粉丝点击