• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1081
  • Last Modified:

VC++: How to set colors(text and highlight) of an item in a listbox

How do you change the text color and backcolor of an individual listbox item? Also, how do you change the backcolor of the listbox itself?
0
englm
Asked:
englm
  • 6
  • 4
1 Solution
 
piano_boxerCommented:
To set the color of individual items you need to create a ownerdraw listbox.

You can get a skeleton ownerdraw listbox using the component library in MS Visual C++.
0
 
RONSLOWCommented:
Does this apply to a simple CListBox?What makes a control an ownerdrawn control?
0
 
englmAuthor Commented:
Use ClassWizard to derive your own class from CListBox (or other control)

Use ClassWizard to override DrawItem and PreSubClassWindow

PreSubClassWindow should set the owner draw style bit.

Draw Item is where you right the code to draw your listbox (or whatever)

[NOTE: You can set owner draw style in the dialog template .. but doing it in the class means you don't have to remember to set owner draw every time you use it]

Use ClassWizard in your dialog to create a control member variable for your listbox.  Select your new CListBox-derived owner draw class as the type.

Now it should all work.

The tricky part is the drawing of the listbox.


0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
RONSLOWCommented:
Look at DrawItem code in the MFC class CCheckListBox.  It draws the text and you can see where it sets the text color and background color for each item (so you can change this to set your own colors).  This class does lots of other things to do with the checkboxes that it adds to each line .. but you can probably ignore all that and just look at the DrawItem code.



0
 
RONSLOWCommented:
Look at DrawItem code in the MFC class CCheckListBox.  It draws the text and you can see where it sets the text color and background color for each item (so you can change this to set your own colors).  This class does lots of other things to do with the checkboxes that it adds to each line .. but you can probably ignore all that and just look at the DrawItem code.



0
 
RONSLOWCommented:
I have created my sub-class CMyListBox from CListBox (which I did in classwizard), but where is an instance of  CMyListBox created? In DrawItem()? I placed a ListBox control on my form via the Dialog Editor, but will CMyListBox::DrawItem() use that control?
 
MFC help indicates that there is CListbox::GetItemRect function - couldn't this be used somehow to repaint the item backcolor? Just a thought - this all very confusing.
0
 
englmAuthor Commented:
OK .. if you created it with ClassWizard then ClassWizard will now know this class can be associated with a list box.

In ClassWizard, look at your dialog and in the member variable tab create a control member vairable for your listbox control.

Make sure you select your new class as the type of the control instead of CListBox.

This will add a CMyListBox variable to your dialog class for that dialog.  And It will add a DDX_Control call to you DoDataExchange that will link your control to the CMyListBox member varaible in the dialog.  That way, your CMyListBox will get the draw item method.

DrawItem receives a struct that has a DC and bounding rectangel etc for drawing.  Grab the code from CCheckListBox::DrawItem and modify it to draw your items (just make it work at all first, then worry about your colors).

When you have, you'll see lines in that code that get the system defaults for text and background color.  Change these lines to get the colour that you want.

0
 
RONSLOWCommented:
Ronslow,

Your last comment:
"DrawItem receives a struct that has a DC and bounding rectangel etc for drawing.  Grab the code from CCheckListBox::DrawItem and modify it to draw your items (just make it work at all first, then worry about your colors)."

I looked up CCheckListBox::DrawItem in the help, but saw no specific code for darwitem as you mentioned. Where is it?

PS - who gets the grade for helping me?
0
 
englmAuthor Commented:
Looks like you have already given the points to piano_boxer! SO much for my help here :-(

You get the CCheckListBox code from the MFC source .. you will either have it installed on your HD or you can get it from the CD or I can post it here to you.

But I'd appreciate some points for my efforts here :-)

0
 
RONSLOWCommented:
I would like to see a post of the code (I still don't see any in the help on CD).

I figured out how to change the backcolor, but now other problems arise with owner-drawn box - adding items via ::AddString doesn't work anymore.

For, now we can close this question I'll post a new one for owner-drawn concerns.

Also, if there is a way to give you points let me know - I don't see any options on this site for doing so.
0
 
englmAuthor Commented:
Once you've accepted an answer that's it.  If you want to reward me for my efforts, post another question (label it "For RONSLOW only", say) and let me answer it.

I look forward to being able to help you with your owner-draw problem
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now