List box with color text

xiaoxiao2021-03-06  64

A list box class that displays color text, the class name is CColorListbox, and its base class is the CListbox in the MFC class. It is different from the class CListbox class, which allows you to display color text in the list box, in this AddString () and insertstring in the class do this, such as you can use the following method:

M_clistbox.addstring ("Hey IT's Red!", RGB (255, 0));

M_clistbox.insertstring (INDEX, "Hi I'm Green", RGB (0, 255, 0));

It is very simple, you only need to use the resource editor.

First, add a list box in the dialog box, you have to put the property of the dialog box at Ower Draw: Select Fixed. Of course, you should have a string in your list box (otherwise how to have a colorful text?)

Then, put the header files related to this class in your project. Also pay attention to the following statement,

Below is a part of the code:

#include "colorlistbox.h" // better be in stdafx.h

#include "mydialog.h"

......

Bool CMYDIALOG :: OnNitdialog ()

{

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

.....

}

Here is the header file and that CPP file, all code is as follows:

// The following is the header file

#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 overce

#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 (LPCTSTSTR 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 Immediate Line.

#ENDIF /! Defined (AFX_COLORLISTBOX_H__5529A6B1_584A_11D2_A41A_006097BD277B__INCluded_)

// The following is the implementation of the file

// ******************************************************** ************

// colorListbox.cpp: importation 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 Uses in 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_singlex);

// 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 inflecting 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;

}

Translated from September 12, 2001. I hope this will play a role in tiles, and there is a technology reader to extend it.

转载请注明原文地址:https://www.9cbs.com/read-87112.html

New Post(0)