Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: changing TListView selection color when focused and non focused


This question is not answered. Helpful answers available: 0. Correct answers available: 1.


Permlink Replies: 5 - Last Post: Jun 19, 2014 1:48 AM Last Post By: Gregory Klas Threads: [ Previous | Next ]
Gregory Klas

Posts: 6
Registered: 8/18/01
changing TListView selection color when focused and non focused  
Click to report abuse...   Click to reply to this thread Reply
  Posted: May 30, 2014 9:39 AM
Hello, hopefully an easy question -

I've got 2 owner data virtual ListViews on a form. One is sort of a 'master' view and the other is a 'detail' view.

I set HideSelection on both ListViews to false.

When an item in one ListView is selected, I can still see the selection in the other ListView but just barely as it's been shaded very light grey (with HideSelection checked, the selection obviously disappears completely). I'd like to change the color of so the selected items in both ListViews are very easy to see and are the same color - no matter whether the control is focused or not.

I know I'll have to OwnerDraw the ListViews ::OnCustomDrawItem().

Does anyone have an example of how to do such a thing(or know where one can be found)?

Thanks & kind regards,

Greg
Asger Joergensen

Posts: 370
Registered: 11/18/08
Re: changing TListView selection color when focused and non focused [Edit]
Helpful
Click to report abuse...   Click to reply to this thread Reply
  Posted: May 30, 2014 10:38 AM   in response to: Gregory Klas in response to: Gregory Klas
Hi Gregory

Gregory Klas wrote:

Hello, hopefully an easy question -

I've got 2 owner data virtual ListViews on a form. One is sort of a 'master'
view and the other is a 'detail' view.

I set HideSelection on both ListViews to false.

When an item in one ListView is selected, I can still see the selection in the
other ListView but just barely as it's been shaded very light grey (with
HideSelection checked, the selection obviously disappears completely). I'd
like to change the color of so the selected items in both ListViews are very
easy to see and are the same color - no matter whether the control is focused
or not.

I know I'll have to OwnerDraw the ListViews ::OnCustomDrawItem().

Does anyone have an example of how to do such a thing(or know where one can be
found)?

There should also be an example in among the examples that come with
C++Builder in the virtual list view, if I remember correctly they just
change the font color for different file types.

But if you do a search on: TCUSTOMDRAWSTATE LISTVIEW ^CPPBUILDER
http://www.codenewsfast.com

you get this result:
http://www.codenewsfast.com/cnf/articles/1152411807.5304

Best regards
Asger-P
MacroMenu a must have for programmer, view the video:
http://asger-p.dk/software/macromenu.php#QuickTour

Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: changing TListView selection color when focused and non focused [Edit]
Helpful
Click to report abuse...   Click to reply to this thread Reply
  Posted: May 30, 2014 11:05 AM   in response to: Gregory Klas in response to: Gregory Klas
Gregory wrote:

I'd like to change the color of so the selected items in both ListViews
are very easy to see and are the same color - no matter whether the
control is focused or not.

You have to owner-draw the ListView to do that.

I know I'll have to OwnerDraw the ListViews ::OnCustomDrawItem().

In one of my projects, I use an owner-data owner-drawn ListView, but use
the OnAdvancedCustomDraw(Sub)Item events to customize the background and
font colors of my list items.

Does anyone have an example of how to do such a thing(or know
where one can be found)?

Try this:

void __fastcall TMyForm::ListViewAdvancedCustomDrawItem(TCustomListView *Sender, 
TListItem *Item, TCustomDrawState State, TCustomDrawStage Stage, bool &DefaultDraw)
{
    if ((Stage == cdPrePaint) && (State.Contains(cdsSelected)))
    {
        Sender->Canvas->Brush->Color = ...;
        Sender->Canvas->Font->Color = ...;
    }
}
 
void __fastcall TMyForm::ListViewAdvancedCustomDrawSubItem(TCustomListView 
*Sender, TListItem *Item, int SubItem, TCustomDrawState State, TCustomDrawStage 
Stage, bool &DefaultDraw)
{
    if ((Stage == cdPrePaint) && (State.Contains(cdsSelected)))
    {
        Sender->Canvas->Brush->Color = ...;
        Sender->Canvas->Font->Color = ...;
 
        // workaround for a bug where TListView does not report a new Font 
back to Windows correctly...
        if (Sender->Canvas->Font->OnChange)
            Sender->Canvas->Font->OnChange(Sender->Canvas->Font);
    }
}


--
Remy Lebeau (TeamB)
Gregory Klas

Posts: 6
Registered: 8/18/01
Re: changing TListView selection color when focused and non focused  
Click to report abuse...   Click to reply to this thread Reply
  Posted: May 31, 2014 1:39 PM   in response to: Gregory Klas in response to: Gregory Klas
Gregory Klas wrote:
Hello, hopefully an easy question -

I've got 2 owner data virtual ListViews on a form. One is sort of a 'master' view and the other is a 'detail' view.

I set HideSelection on both ListViews to false.

When an item in one ListView is selected, I can still see the selection in the other ListView but just barely as it's been shaded very light grey (with HideSelection checked, the selection obviously disappears completely). I'd like to change the color of so the selected items in both ListViews are very easy to see and are the same color - no matter whether the control is focused or not.

I know I'll have to OwnerDraw the ListViews ::OnCustomDrawItem().

Does anyone have an example of how to do such a thing(or know where one can be found)?

Thanks & kind regards,

Greg


Thanks Asger and Remy. Unfortunately the solution is appearing to be a little more elusive and potentially time consuming than I had originally hoped. I'll have to come back to this at a later time...

Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: changing TListView selection color when focused and non focused[Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 1, 2014 1:58 AM   in response to: Gregory Klas in response to: Gregory Klas
Gregory wrote:

Thanks Asger and Remy. Unfortunately the solution is appearing
to be a little more elusive and potentially time consuming than I
had originally hoped.

Is there a problem with what I described? I use it myself, I know it works.

--
Remy Lebeau (TeamB)
Gregory Klas

Posts: 6
Registered: 8/18/01
Re: changing TListView selection color when focused and non focused  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 17, 2014 4:14 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Remy Lebeau (TeamB) wrote:
Gregory wrote:

Thanks Asger and Remy. Unfortunately the solution is appearing
to be a little more elusive and potentially time consuming than I
had originally hoped.

Is there a problem with what I described? I use it myself, I know it works.

--
Remy Lebeau (TeamB)

The problem I kept running into when using owner draw was the disappearance of text and lines from the list view. The listitems are still there, I can click on them - but I'm missing something and I assume it's because I'm not drawing everything I need to. Unfortunately I am just not very familiar with OwnerDraw, but I'm still working on solving the issue and researching what I can. In the solution you used in your application, did you happen to run into issues like this?
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02