Let the listbox control display a different color for each row

Source: Internet
Author: User

Recently with MFC to do a small project, want to let each row in the listbox according to the custom color to display a different color. Just started to think of MFC too simple, dragged a ListBox control and then bound a variable m_listbox.

In the OnInitDialog () function of the main dialog box, I call the following code

M_listbox.addstring ("This is a little test!") (_t);
M_listbox.setitemdata (0,rgb (0,0,255));
After running, it is found that the default black is still displayed.

Later looked at the relevant information, only to find that Microsoft will these controls the font and color properties are set to the default, the Normal property settings function is useless. To draw the graphics you want in a ListBox or Listctrl control, or to set the attributes you want, you must use a custom drawing.

Interface programming is sometimes a layer of window paper, stabbed to break the good. The following are the specific processes that are implemented:

1. Drag a ListBox control to set the properties. Where the owner draw property is set to Variable,has strings to be checked


2. Customize a class Ccolorlistbox derived from CListBox. Overload the following two virtual functions

virtual void DrawItem (lpDrawItemStruct lpdrawitemstruct);
virtual void MeasureItem (Lpmeasureitemstruct lpmeasureitemstruct);
Then define an interface for us to call, and let each row of the listbox appear according to our custom color according to the incoming string and RGB values.

Public:
	int addstring (LPCTSTR lpszitem, colorref itemcolor = RGB (255,0,0));

Here are the specific definitions of the above 3 functions


////////////////////////////////////////////////////////

/******************************************************	box and save color info. *//////*/********************************************************************/int Ccolorlistbox::addstri 
   
	Ng (LPCTSTR Lpszitem, colorref itemcolor) {//Add the string to the list box int nindex = clistbox::addstring (Lpszitem);

	Save color data if (nindex >= 0) setitemdata (nindex, Itemcolor);
return nindex; }/********************************************************************//*/* Function Name:drawit EM/* Description:called by the framework when a visual aspect of */* an owner-draw list box ch					Anges. *////*/********************************************************************/void Ccolorlistbox::D rawit EM (lpdrawitemstruct LPDRAWITEMSTRUCT) {//losing focus?
		if (Lpdrawitemstruct->itemid = = 1) {DrawFocusRect (LPDRAWITEMSTRUCT->HDC, &lpdrawitemstruct->rcitem);
    Return

	} cdc* PDC = Cdc::fromhandle (LPDRAWITEMSTRUCT->HDC);
	COLORREF Clrold;
	
	CString stext;

	Get color info from item data colorref clrnew = (COLORREF) (lpdrawitemstruct->itemdata);
	Item selected? if ((Lpdrawitemstruct->itemstate & ods_selected) && (Lpdrawitemstruct->itemaction & (Oda_select |
		Oda_drawentire))) {CBrush Brush (:: GetSysColor (Color_highlight));
	Pdc->fillrect (&lpdrawitemstruct->rcitem, &brush);
	}//Item deselected? if (!) (
		Lpdrawitemstruct->itemstate & ods_selected) && (Lpdrawitemstruct->itemaction & Oda_select)) {
		CBrush Brush (:: GetSysColor (Color_window));
	Pdc->fillrect (&lpdrawitemstruct->rcitem, &brush);
	}//item has focus? if ((Lpdrawitemstruct->itemaction & Oda_focus) && (Lpdrawitemstruct->itemstate & Ods_focus)) {Pdc->drawfocusrect (&lpdrawitemstruct->rcitem) 
	;
	}//Lost focus? if (Lpdrawitemstruct->itemaction & Oda_focus) &&! ( 
	Lpdrawitemstruct->itemstate & Ods_focus)) {pdc->drawfocusrect (&lpdrawitemstruct->rcitem);

	}//Set the background mode to transparent int nbkmode = Pdc->setbkmode (transparent); if (Lpdrawitemstruct->itemstate & ods_selected) Clrold = Pdc->settextcolor (:: GetSysColor (COLOR_
	Highlighttext)); else if (Lpdrawitemstruct->itemstate & ods_disabled) Clrold = Pdc->settextcolor (:: GetSysColor (COLOR_GRAYTEX
	T));

	else Clrold = Pdc->settextcolor (clrnew);
	Get item Text GetText (Lpdrawitemstruct->itemid, stext);

	CRect rect = lpdrawitemstruct->rcitem; Text Format UINT Nformat = Dt_left | Dt_singleline |
	Dt_vcenter;
	
	if (GetStyle () & lbs_usetabstops) Nformat |= dt_expandtabs; Draw the text Pdc->draWtext (Stext,-1, &rect, Nformat); 
	Restore old values Pdc->settextcolor (clrold);
Pdc->setbkmode (Nbkmode); } void Ccolorlistbox::measureitem (Lpmeasureitemstruct lpmeasureitemstruct) {lpmeasureitemstruct->itemheight =:: G	
Etsystemmetrics (Sm_cymenucheck);
 }

3. Now we can test it. Drag a ListBox control, and then define a variable M_listbox for the Ccolorlistbox type. You can manually add the following code directly to the location below, or you can add it automatically via MFC ClassWizard.

Add in header file in main dialog header file

Dialog Data
	//{{afx_data (clogsystemtestdlg) ...
	Ccolorlistbox	M_listbox;
	}}afx_data

Add code to the DoDataExchange () function in the code file of the main dialog box

DDX_Control (PDX, Idc_list1, M_listbox);
The purpose is to bind the variable m_listbox and the list control identified by Idc_list1, as follows

void Ctestdlg::D odataexchange (cdataexchange* PDX)
{
	CDialog::D odataexchange (PDX);
	{{Afx_data_map (Clogsystemtestdlg)
	DDX_Control (PDX, Idc_list1, m_listbox);
	}}afx_data_map
}

Add test code to the OnInitDialog () function in the code file of the main dialog box

BOOL Clogsystemtestdlg::oninitdialog ()
{
	cdialog::oninitdialog ();
	...........
	Todo:add Extra initialization here
	m_listbox.addstring ("Empty Mountain New Rain", RGB (255,0,0));
	M_listbox.addstring ("Weather late to Autumn", RGB (0,255,0));
	M_listbox.addstring ("Bright Moon", RGB (0,0,255));
	M_listbox.addstring ("Spring Stone High", RGB (255,0,255));
	return TRUE;  Return TRUE unless your set the focus to  a control
}


The compile-run effect looks like this


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.