Solved

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

Posted on 1998-04-21
11
845 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
ID: 1162813
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
ID: 1162814
Does this apply to a simple CListBox?What makes a control an ownerdrawn control?
0
 

Author Comment

by:englm
ID: 1162815
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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 10

Expert Comment

by:RONSLOW
ID: 1162816
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
ID: 1162817
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
ID: 1162818
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
ID: 1162819
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
ID: 1162820
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
ID: 1162821
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
ID: 1162822
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
ID: 1162823
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
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 viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

828 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