1

As far as I know you can use Custom Draw in a list-view to paint individual cells the way you want using CDDS_SUBITEM.

However, I would like to have multi-colored text within a cell. That is, I would like to, for example, set a cell's text to: "this program is called the [start red text]Red Program[end red text]" - is there a way to accomplish this?

1 Answers1

0

Well, in your NMCUSTOMDRAW structure you get an HDC. You will need to call DrawText multiple times. You may be able to just call SetTextColor to do that, or you may have to call SelectObject and select in your own custom Pen to change the color... yay GDI.

i_am_jorf
  • 53,608
  • 15
  • 131
  • 222
  • Thanks for the reply. How would I go about figuring out which section of the rectangle I'm given to draw in corresponds to each colored token? –  Aug 03 '11 at 01:05
  • I don't understand your question. You break your string into the various colors, then use DrawText (and maybe MeasureText) to figure out the rect for each one. – i_am_jorf Aug 08 '11 at 21:04
  • I'll need to call DrawText to draw each individual token, and I'll need to know the rect for each one -- but I only get one HDC/rect from the notification. How do I "figure out the rect" using DrawText? Doesn't it just take in a rect as input? And what is MeasureText? –  Aug 08 '11 at 22:40
  • 1
    Oh yeah, use use DrawText() to get the size your text will take (note the rect param is in/out). NMCUSTOMDRAW has a rect member. Did you try that guy? Once you have the bounding rect and the size your text will take, it's just simple 2D geometry. – i_am_jorf Aug 09 '11 at 01:05
  • Oh yeah, the `NMLVCUSTOMDRAW` struct you get has a rectangle that specifies the rect the text should be drawn in. Even better, you don't have to figure out the margins. – i_am_jorf Aug 09 '11 at 01:05
  • Okay, I think I've just about got it. I'm having a few minor issues with spacing but besides that, it's working out great. Thanks mate! –  Aug 09 '11 at 04:10
  • Cool. Yeah, GDI is a pain for stuff like this. You see why they were so keen to build WFP and Silverlight. :) – i_am_jorf Aug 10 '11 at 16:31