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
- MFC自绘ListBox可根据条件定义不同行字体颜色
- 根据条件,DBGrid不同行显示不同的颜色
- ExtJS GridPanel根据条件改变字体颜色
- listBox自定义颜色字体
- dev grdicontrol 根据条件改变行颜色,字体颜色等
- 记MFC自绘ListBox闪烁处理
- JComboBox中根据条件改变菜单中字体的颜色
- 改变Listbox中的字体颜色
- 如何使DataGrid(Web)的不同行根据不同的条件有不同的显示格式?
- 自绘式按钮COwnerDrawBtn:可设置文本字体、文本颜色、按钮背景图片
- MFC下自绘ListBox或者ListCtrl中的内容
- sapui5 表格不同行的颜色控制
- 改变ListBox中项的字体颜色。
- 改变ListBox中项的字体颜色。
- 改变ListBox中项的字体颜色。
- devexpress实现单元格根据条件显示不同的样式(颜色、字体、对齐方式,大小等)
- MFC自绘带背景颜色标题栏
- 自绘透明ListBox
- CentOS安装TortoiseSVN svn 客户端及相关命令
- 项目总结 —— 登陆之kaptcha验证码 (1)
- 杀气---不怒自威!
- 透明数据加密(TDE)库的备份和还原
- JQuery Re-Sizable(尺寸改变)
- MFC自绘ListBox可根据条件定义不同行字体颜色
- 使用新的apple id更新Xcode
- ORA-600 [4193]
- crashrpt
- CodeForces 301D(树状数组)
- ApplicationContext和BeanFactory的区别
- Eclipse—为Android工程第三方jar包关联源代码的方法
- 用影响力导图解决问题
- Python开发环境配置与IDE使用