Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Get a ListView cell's text color.

6 views
Skip to first unread message

Jean-Christophe

unread,
Jan 18, 2014, 4:03:03 AM1/18/14
to
Hi All,

I want to retreive the color of the text displayed in a cell
of a ListView, where different cells have different text colors.

I tried to use this :
COLORREF ListView_GetTextColor( HWND hwnd );
but it doesn't work since I need to get
the text color of a _particular_ cell of the list.

How can I do that ?

Deanna Earley

unread,
Jan 20, 2014, 4:42:45 AM1/20/14
to
You can't easily.
The colour is set each time it's painted by asking the parent window
(with the CDDS_ITEMPREPAINT notification and NMLVCUSTOMDRAW structure).
There is no (internal to the control) persistent colour information.

If it's your own control, you'll need to ask the code that's setting the
colour.

--
Deanna Earley (dee.e...@icode.co.uk)
iCatcher Development Team
http://www.icode.co.uk/icatcher/

iCode Systems

(Replies direct to my email address will be ignored. Please reply to the
group.)

Jean-Christophe

unread,
Jan 20, 2014, 12:17:07 PM1/20/14
to
>> Jean-Christophe :
>> Hi All,
>> I want to retreive the color of the text displayed in a cell of a
>> ListView, where different cells have different text colors.
>> I tried to use this : COLORREF ListView_GetTextColor( HWND hwnd );
>> but it doesn't work since I need to get the text color of a _particular_
>> cell of the list.

> "Deanna Earley"
> You can't easily.
> The colour is set each time it's painted by asking the parent window (with
> the CDDS_ITEMPREPAINT notification and NMLVCUSTOMDRAW structure).

Yes, when responding to the WM_DRAWITEM message,
my software serves all the lists's requests (text, color, etc)
But the text's colors depends on many other parameters
which are somehow tricky to gather,
that's why I want to avoid redoing that again.


> There is no (internal to the control) persistent colour information.

Once my software has set anything required for the list
( text, color, etc ) the system displays it, so the color
information is indeed stored somewhere in memory.
Just like the text is -- and I can retrieve the text
of any cell by calling ListView_GetItemText();
That's why I'm still surprized that there seems
to be no function to retrieve the text's color.

Anyway, I *could* use ListView_GetTextColor(HWND)
if there's a way to retreive the HWND of a particular cell
( w/ XY coordinates, just like ListView_GetItemText() params )
Is there a function that takes the HWND of the list and
the coordinates of a cell, and returns the cell's HWND ?


> If it's your own control, you'll need to ask the code that's setting the
> colour.

Yes, and that's precisely what I wanted to avoid.
If I have no choice, while filling the list
I'll catch the colors and store them in a tab[]
( but this is lacking élégance, if you ask me )


Thanks for your reply, Deanna.


Deanna Earley

unread,
Jan 20, 2014, 12:56:49 PM1/20/14
to
On 20/01/2014 17:17, Jean-Christophe wrote:
>>> Jean-Christophe :
>>> Hi All,
>>> I want to retreive the color of the text displayed in a cell of a
>>> ListView, where different cells have different text colors.
>>> I tried to use this : COLORREF ListView_GetTextColor( HWND hwnd );
>>> but it doesn't work since I need to get the text color of a _particular_
>>> cell of the list.
>
>> "Deanna Earley"
>> You can't easily.
>> The colour is set each time it's painted by asking the parent window
>> (with
>> the CDDS_ITEMPREPAINT notification and NMLVCUSTOMDRAW structure).
>
> Yes, when responding to the WM_DRAWITEM message,
> my software serves all the lists's requests (text, color, etc)
> But the text's colors depends on many other parameters
> which are somehow tricky to gather,
> that's why I want to avoid redoing that again.

Then cache it in your code.
The control is asking you what colour it should be so asking it what
colour you told it last time isn't an option.

>> There is no (internal to the control) persistent colour information.
>
> Once my software has set anything required for the list
> ( text, color, etc ) the system displays it, so the color
> information is indeed stored somewhere in memory.

No, the pixels are drawn to the GDI device context, then any colour
context id forgotten.

> Just like the text is -- and I can retrieve the text
> of any cell by calling ListView_GetItemText();

The text is a property it remembers (along with some style information).
When it changes, it redraws it and asks again what colour it should be.

> That's why I'm still surprized that there seems
> to be no function to retrieve the text's color.

It wasn't part of the original design, it's part of the custom draw
system which allows different fonts to be selected (also not remembered
as it's selected in at the time), graphics, and anything else the
application desires.
It just keeps track of the item itself.

> Anyway, I *could* use ListView_GetTextColor(HWND)
> if there's a way to retreive the HWND of a particular cell
> ( w/ XY coordinates, just like ListView_GetItemText() params )
> Is there a function that takes the HWND of the list and
> the coordinates of a cell, and returns the cell's HWND ?

Individual "cells" don't have hWnds.
The listview has a single hWnd that it draws whatever should be visible
to whenever it needs updating.
That will stay on screen until it is obscured, and repainted again.

>> If it's your own control, you'll need to ask the code that's setting the
>> colour.
>
> Yes, and that's precisely what I wanted to avoid.
> If I have no choice, while filling the list
> I'll catch the colors and store them in a tab[]
> ( but this is lacking élégance, if you ask me )

Welcome to the win32 API.
0 new messages