Solved

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

Posted on 1998-04-21
11
780 Views
Last Modified: 2008-02-01
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
Comment
Question by:englm
  • 6
  • 4
11 Comments
 
LVL 4

Accepted Solution

by:
piano_boxer earned 20 total points
Comment Utility
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
 
LVL 10

Expert Comment

by:RONSLOW
Comment Utility
Does this apply to a simple CListBox?What makes a control an ownerdrawn control?
0
 

Author Comment

by:englm
Comment Utility
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
 
LVL 10

Expert Comment

by:RONSLOW
Comment Utility
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
 
LVL 10

Expert Comment

by:RONSLOW
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 10

Expert Comment

by:RONSLOW
Comment Utility
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
 

Author Comment

by:englm
Comment Utility
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
 
LVL 10

Expert Comment

by:RONSLOW
Comment Utility
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
 

Author Comment

by:englm
Comment Utility
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
 
LVL 10

Expert Comment

by:RONSLOW
Comment Utility
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
 

Author Comment

by:englm
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now