⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 colorl~1.cpp

📁 visual c++ 时尚编程百例 全部源代码
💻 CPP
字号:
// ColorListBox.cpp : implementation file
//

#include "stdafx.h"
#include "ColorListDemo.h"
#include "ColorListBox.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CColorListBox

CColorListBox::CColorListBox()
{
}

CColorListBox::~CColorListBox()
{
}


BEGIN_MESSAGE_MAP(CColorListBox, CListBox)
	//{{AFX_MSG_MAP(CColorListBox)
		// NOTE - the ClassWizard will add and remove mapping macros here.
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CColorListBox message handlers
void CColorListBox::DrawItem(LPDRAWITEMSTRUCT lpdis) 
{
	if (lpdis->itemID < 0)
		return; 

	COLORREF cvText;
	COLORREF cvBack;
	CString itemString;

	if ((lpdis->itemState & ODS_SELECTED) &&	// if item has been selected
		(lpdis->itemAction & (ODA_SELECT | ODA_DRAWENTIRE)))
			DrawFocusRect(lpdis->hDC, &lpdis->rcItem); 
	

	if (!(lpdis->itemState & ODS_SELECTED) &&	// if item has been deselected
		(lpdis->itemAction & ODA_SELECT))
			DrawFocusRect(lpdis->hDC, &lpdis->rcItem); 
	

	if(lpdis->itemData)		// if color information is present
			cvText = SetTextColor(lpdis->hDC, lpdis->itemData);
		else 	// if no color information, use default system colors
			cvText = SetTextColor(lpdis->hDC, GetSysColor((lpdis->itemState & ODS_SELECTED)
		? COLOR_HIGHLIGHTTEXT : COLOR_WINDOWTEXT)); 
	
	// always use system colors for background
	cvBack = SetBkColor(lpdis->hDC, GetSysColor((lpdis->itemState & ODS_SELECTED)
		? COLOR_HIGHLIGHT : COLOR_WINDOW)); 

	// get and display item text
	GetText(lpdis->itemID, itemString );
	DrawText(lpdis->hDC, itemString, -1, &lpdis->rcItem, DT_LEFT | DT_SINGLELINE);

	// restore DC colors
	SetTextColor(lpdis->hDC, cvText); 
	SetBkColor(lpdis->hDC, cvBack); 
}


//***********************************************
// original AddString() method
//
// purpose: Add a string to the listbox
//
// parameters: 
//		lpszItem: pointer to item text
//
// remarks:
//	    provided because CListBox::AddString is 
//		NOT virtual
//
// return:	item index
//***********************************************
int CColorListBox::AddString( LPCTSTR lpszItem)
{
	return ((CListBox*)this)->AddString(lpszItem);
}


//***********************************************
// new AddString() method
//
// purpose: Add a string to the listbox
//
// parameters: 
//		lpszItem: pointer to item text
//			 rgb: text color as a COLORREF
//
// return:	item index
//***********************************************
int CColorListBox::AddString( LPCTSTR lpszItem,COLORREF rgb )
{
	int item = AddString(lpszItem);
	if(item >=0)
		SetItemData(item,rgb);
	return item;
}


//***********************************************
// new InsertString() method
//
// purpose: Insert a string to the listbox
//
// parameters: 
//		  nIndex: index of inserted item
//		lpszItem: pointer to item text
//			 rgb: text color as a COLORREF
//
// return:	item index
//***********************************************
int CColorListBox::InsertString( int nIndex, LPCTSTR lpszItem, COLORREF rgb)
{
	int item = ((CListBox*)this)->InsertString(nIndex,lpszItem);
	if(item >=0)
		SetItemData(item,rgb);
	return item;

}




⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -