?
Solved

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

Posted on 1998-04-21
11
Medium Priority
?
975 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 80 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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 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.
Suggested Courses

800 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