Solved

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

Posted on 1998-04-21
11
931 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

696 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