// ColorListBox.cpp : implementation file
//-------------------------------------------------------------------
//
// CColorListBox class -
// A CListBox-derived class with optional colored items.
//
// Version: 1.0 01/10/1998 Copyright � Patrice Godard
//
// Version: 2.0 09/17/1999 Copyright � Paul M. Meidinger
//
//-------------------------------------------------------------------
#include "stdafx.h"
#include "ColorListBox.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CColorListBox
//-------------------------------------------------------------------
//
CColorListBox::CColorListBox()
//
// Return Value: None.
//
// Parameters : None.
//
// Remarks : Standard constructor.
//
{
} // CColorListBox
//-------------------------------------------------------------------
//
CColorListBox::~CColorListBox()
//
// Return Value: None.
//
// Parameters : None.
//
// Remarks : Destructor.
//
{
} // ~CColorListBox()
BEGIN_MESSAGE_MAP(CColorListBox, CListBox)
//{{AFX_MSG_MAP(CColorListBox)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CColorListBox message handlers
//-------------------------------------------------------------------
//
void CColorListBox::DrawItem(LPDRAWITEMSTRUCT lpDIS)
//
// Return Value: None.
//
// Parameters : lpDIS - A long pointer to a DRAWITEMSTRUCT structure
// that contains information about the type of drawing required.
//
// Remarks : Called by the framework when a visual aspect of
// an owner-draw list box changes.
//
{
if ((int)lpDIS->itemID < 0)
return;
CDC* pDC = CDC::FromHandle(lpDIS->hDC);
COLORREF crText;
CString sText;
COLORREF crNorm = (COLORREF)lpDIS->itemData; // Color information is in item data.
COLORREF crHilite = RGB(255-GetRValue(crNorm), 255-GetGValue(crNorm), 255-GetBValue(crNorm));
// If item has been selected, draw the highlight rectangle using the item's color.
if ((lpDIS->itemState & ODS_SELECTED) &&
(lpDIS->itemAction & (ODA_SELECT | ODA_DRAWENTIRE)))
{
CBrush brush(crNorm);
pDC->FillRect(&lpDIS->rcItem, &brush);
}
// If item has been deselected, draw the rectangle using the window color.
if (!(lpDIS->itemState & ODS_SELECTED) && (lpDIS->itemAction & ODA_SELECT))
{
CBrush brush(::GetSysColor(COLOR_WINDOW));
pDC->FillRect(&lpDIS->rcItem, &brush);
}
// If item has focus, draw the focus rect.
if ((lpDIS->itemAction & ODA_FOCUS) && (lpDIS->itemState & ODS_FOCUS))
pDC->DrawFocusRect(&lpDIS->rcItem);
// If item does not have focus, redraw (erase) the focus rect.
if ((lpDIS->itemAction & ODA_FOCUS) && !(lpDIS->itemState & ODS_FOCUS))
pDC->DrawFocusRect(&lpDIS->rcItem);
// Set the background mode to TRANSPARENT to draw the text.
int nBkMode = pDC->SetBkMode(TRANSPARENT);
// If the item's color information is set, use the highlight color
// gray text color, or normal color for the text.
if (lpDIS->itemData)
{
if (lpDIS->itemState & ODS_SELECTED)
crText = pDC->SetTextColor(crHilite);
else if (lpDIS->itemState & ODS_DISABLED)
crText = pDC->SetTextColor(::GetSysColor(COLOR_GRAYTEXT));
else
crText = pDC->SetTextColor(crNorm);
}
// Else the item's color information is not set, so use the
// system colors for the text.
else
{
if (lpDIS->itemState & ODS_SELECTED)
crText = pDC->SetTextColor(::GetSysColor(COLOR_HIGHLIGHTTEXT));
else if (lpDIS->itemState & ODS_DISABLED)
crText = pDC->SetTextColor(::GetSysColor(COLOR_GRAYTEXT));
else
crText = pDC->SetTextColor(::GetSysColor(COLOR_WINDOWTEXT));
}
// Get and display item text.
GetText(lpDIS->itemID, sText);
CRect rect = lpDIS->rcItem;
// Setup the text format.
UINT nFormat = DT_LEFT | DT_SINGLELINE | DT_VCENTER;
if (GetStyle() & LBS_USETABSTOPS)
nFormat |= DT_EXPANDTABS;
// Calculate the rectangle size before drawing the text.
pDC->DrawText(sText, -1, &rect, nFormat | DT_CALCRECT);
pDC->DrawText(sText, -1, &rect, nFormat);
pDC->SetTextColor(crText);
pDC->SetBkMode(nBkMode);
} // DrawItem
//-------------------------------------------------------------------
//
void CColorListBox::MeasureItem(LPMEASUREITEMSTRUCT lpMIS)
//
// Return Value: None.
//
// Parameters : lpMIS - A long pointer to a
// MEASUREITEMSTRUCT structure.
//
// Remarks : Called by the framework when a list box with
// an owner-draw style is created.
//
{
// ### Is the default list box item height the same as
// the menu check height???
lpMIS->itemHeight = ::GetSystemMetrics(SM_CYMENUCHECK);
} // MeasureItem
//-------------------------------------------------------------------
//
int CColorListBox::AddString(LPCTSTR lpszItem)
//
// Return Value: The zero-based index to the string in the list box.
// The return value is LB_ERR if an error occurs; the
// return value is LB_ERRSPACE if insufficient space
// is available to store the new string.
//
// Parameters : lpszItem - Points to the null-terminated
// string that is to be added.
//
// Remarks : Call this member function to add a string to a list
// box. Provided because CListBox::AddString is NOT
// a virtual function.
//
{
return ((CListBox*)this)->AddString(lpszItem);
} // AddString
//-------------------------------------------------------------------
//
int CColorListBox::AddString(LPCTSTR lpszItem, COLORREF rgb)
//
// Return Value: The zero-based index to the string in the list box.
// The return value is LB_ERR if an error occurs; the
// return value is LB_ERRSPACE if insufficient space
// is available to store the new string.
//
// Parameters : lpszItem - Points to the null-terminated
// string that is to be added.
// rgb - Specifies the color to be associated with the item.
//
// Remarks : Call this member function to add a string to a list
// box with a custom color.
//
{
int nItem = AddString(lpszItem);
if (nItem >= 0)
SetItemData(nItem, rgb);
return nItem;
} // AddString
//-------------------------------------------------------------------
//
int CColorListBox::InsertString(int nIndex, LPCTSTR lpszItem)
//
// Return Value: The zero-based index of the position at which the
// string was inserted. The return value is LB_ERR if
// an error occurs; the return value is LB_ERRSPACE if
// insufficient space is available to store the new string.
//
// Parameters : nIndex - Specifies the zero-based index of the position
// to insert the string. If this parameter is �1, the string
// is added to the end of the list.
// lpszItem - Points to the null-terminated string that
// is to be inserted.
//
// Remarks : Inserts a string into the list box. Provided because
// CListBox::InsertString is NOT a virtual function.
//
{
return ((CListBox*)this)->InsertString(nIndex, lpszItem);
} // InsertString
//-------------------------------------------------------------------
//
int CColorListBox::InsertString(int nIndex, LPCTSTR lpszItem, COLORREF rgb)
//
// Return Value: The zero-based index of the position at which the
// string was inserted. The return value is LB_ERR if
// an error occurs; the return value is LB_ERRSPACE if
// insufficient space is available to store the new string.
//
// Parameters : nIndex - Specifies the zero-based index of the position
// to insert the string. If this parameter is �1, the string
// is added to the end of the list.
// lpszItem - Points to the null-terminated string that
// is to be inserted.
// rgb - Specifies the color to be associated with the item.
//
没有合适的资源?快使用搜索试试~ 我知道了~
可设置文字颜色的listbox

共2个文件
h:1个
cpp:1个


温馨提示
重写了CListBox类,从网上收集的,很好很强大 使用方法:(适用于vc++ MFC) 把包里的头文件和cpp文件包含进你的工程 1)添加一个listbox control 2)设置此控件属性:Has Strings : true owner draw : fix or variable selection : single 其他的属性随意。 3)为此控件添加变量,变量类型为类CColorListBox 4)接下来用CColorListBox重载的方法AddString等方法就可以在listbox添加各种颜色的项了 在visual studio 2005 使用成功!!!
资源推荐
资源详情
资源评论
















收起资源包目录



共 2 条
- 1


narwal
- 粉丝: 31
- 资源: 10
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


最新资源
- 毕业设计基于51单片机的电子密码锁LCD12864中文显示仿真设计资料(包含源程序及仿真文件).zip
- VCRUNTIME140.dll 问题
- MDK5.41版 人家官网都是免费的,只因要填一些信息,我免费代劳了
- Delphi Canvas clock 可扩展性极强 发动你的大脑快速运转!
- Typora 1.8.10 安装包
- Python程序设计期末作业 - 植物大战僵尸模拟器.zip
- 基于Vue的在线电商平台(毕设&课设&实训&大作业&竞赛&项目)
- 数据库课程作业,仿天猫商场,着重数据库的设计,前端由thymeleaf设计,后端由,java+mybatis+mysql实现.zip
- Qt项目(室内地图,两个交通信号灯控制系统,RFID测温系统).zip(毕设&课设&实训&大作业&竞赛&项目)
- 基于协同过滤算法的在线教育平台(前后端分离)。.zip(毕设&课设&实训&大作业&竞赛&项目)
- 基于SSM+Mvean+IDEA 的校园外卖系统.zip(毕设&课设&实训&大作业&竞赛&项目)
- matlab实现IMU姿态解算原理-加速度计-陀螺仪-物体姿态估计-matlab
- 基于springboot+mybatis-plus框架搭建的外卖平台.zip(毕设&课设&实训&大作业&竞赛&项目)
- 基于springboot的外卖平台.zip(毕设&课设&实训&大作业&竞赛&项目)
- 基于Android的考证报名系统.zip(毕设&课设&实训&大作业&竞赛&项目)
- 基于HTML+CSS+jQuery的商城管理项目.zip(毕设&课设&实训&大作业&竞赛&项目)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈



安全验证
文档复制为VIP权益,开通VIP直接复制

- 1
- 2
前往页