|
|
带颜色文字的列表框 |
|
|
作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站 |
一个能显示带颜色文字的列表框类,类名是CColorListBox,它的基类是MFC类中的CListBox,它和类CListBox类不一样,它允许能在列表框中显示有颜色的文字,在该类中的addstring()和insertstring()就干这个的,比如你可以用下面的方法:
m_clistbox.addstring("hey it's red!",RGB(255,0,0)); m_clistbox.insertstring(index,"hi i'm green",RGB(0,255,0));
它的使用是很简单的,你只需要用资源编辑器就够了。
首先,在对话框中添加一个列表框,你得把对话框的属性该一下ower draw:选fixed。当然你的列表框中还应该有字符串(要不然怎么有带颜色的文字呢?)
然后,还要把有关于这个类的头文件放到你的工程中去。还有下面的语句也是要注意的,
下面是一部分代码:
#include "colorlistbox.h" // better be in stdafx.h #include "mydialog.h"
......
bool cmydialog::oninitdialog() { cdialog::oninitdialog();
// sublclass listbox so that our ccolorlistbox receives windows events m_clistbox.subclassdlgitem(idc_clistbox, this); // idc_clistbox is the listbox's resource id
..... } 下面是头文件和那个CPP文件,全部代码如下:
//以下是头文件 #if !defined(AFX_COLORLISTBOX_H__5529A6B1_584A_11D2_A41A_006097BD277B__INCLUDED_) #define AFX_COLORLISTBOX_H__5529A6B1_584A_11D2_A41A_006097BD277B__INCLUDED_
#if _MSC_VER >= 1000 #pragma once #endif // _MSC_VER >= 1000
//************************************************************* // ColorListBox.h : header file // // MFC ListBox with optional color // // Version: 1.0 01/10/1998 (c)Patrice Godard // //**************************************************************
///////////////////////////////////////////////////////////////////////////// // CColorListBox window
class CColorListBox : public CListBox { // Construction public: CColorListBox();
// Attributes public:
// Operations public: int AddString( LPCTSTR lpszItem); int AddString( LPCTSTR lpszItem, COLORREF rgb); int InsertString( int nIndex, LPCTSTR lpszItem, COLORREF rgb); // Overrides // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CColorListBox) public: virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct); //}}AFX_VIRTUAL
// Implementation public: virtual ~CColorListBox();
// Generated message map functions protected: //{{AFX_MSG(CColorListBox) //}}AFX_MSG
DECLARE_MESSAGE_MAP() };
/////////////////////////////////////////////////////////////////////////////
//{{AFX_INSERT_LOCATION}} // Microsoft Developer Studio will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_COLORLISTBOX_H__5529A6B1_584A_11D2_A41A_006097BD277B__INCLUDED_)
//以下是实现文件 //************************************************************* // ColorListBox.cpp : implementation file // // MFC ListBox with optional color // // Version: 1.0 01/10/1998 (c)Patrice Godard // //**************************************************************
#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) //}}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;
}
译完于2001年9月12号。我希望这能起到抛砖引玉的作用,有技术的读者能把它给扩展了。


|
|
相关文章:相关软件: |
|