![]() |
|
QA: How can I change the background color of the header of a List Control?
By Joao Paulo Figueira, August 27, 2003.
QuestionHow can I change the background color of the header of a List Control? AnswerThere are two answers for this question: 1 - The window background color is changed by overriding the WM_ERASEBKGND message processing and painting the background with the chosen color (yellow on the picture). 2 - The item background color is changed by using the custom draw service (red and blue items on the picture).
There is nothing more to say about CColorListCtrl (by the way, the header control can be subclassed "outside" the list control: we use this here for convenience). Now, let's see what the CColorHeaderCtrl is doing. First, the window backgroung painting code: As you can see, it is very simple: it just paints the client area with yellow. The item painting code is a bit more tricky because it uses the custom draw service. This service interacts with the window through a message exchange mechanism. It uses the NM_CUSTOMDRAW notification message (reflected here) to tell the window in what stage the drawing process is (we handle CDDS_PREPAINT and CDDS_ITEMPREPAINT) and to request it to override the desired settings. Here, we only change the text and background colors according to the item number: It works like this: First, the service tells us that it is about to paint the header (CDDS_PREPAINT). We reply saying that we want to be notified for the painting of each item (CDRF_NOTIFYITEMDRAW). Next, the service tells us that it is about to paint an item (CDDS_ITEMPREPAINT). We change the appropriate colors on the DC and reply saying that we did so (CDRF_NEWFONT). Note that we could even have changed the font, specifying a different one per item. For all the other states we don't care about, we tell the service to do the default painting (CDRF_DODEFAULT). And that's it. SampleYou can download the sample project from here - ListColor.zip (182K). Related resources:
DiscussDiscuss this article. Here you can write your comments and read comments of other developers. |
|||||||||||||||||||||