Solved

Draw a selection in a listview

Posted on 2010-09-13
9
375 Views
Last Modified: 2012-05-10
Hi,

I have a listview on my form, with the the property OwnerDraw is True.
I want to draw a selection, just like the property RowSelect
(Brush = darkblue and white font)

This is what I got, but I don't get further than this:

procedure TMainForm.lstvFoodDBCustomDrawItem(Sender: TCustomListView;
  Item: TListItem; State: TCustomDrawState; var DefaultDraw: Boolean);
begin
  if Item.Selected then
  begin
  Sender.Brush.Color := clNavy;
  end
  else
  Sender.Brush.Color := clWIndow;
end;

Who can help me?

P.
0
Comment
Question by:peterkiers
[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
  • 5
  • 4
9 Comments
 
LVL 25

Accepted Solution

by:
epasquier earned 500 total points
ID: 33664422
try this
procedure TForm1.ListView1CustomDrawItem(Sender: TCustomListView;
  Item: TListItem; State: TCustomDrawState; var DefaultDraw: Boolean);
Var
 R:TRect;
 i,NC:integer;
 TotWidth:Integer;
begin
 DefaultDraw:=False;
 With Sender.Canvas do
  begin
   if Item.Selected then
    begin
     Brush.Color := clNavy;
     Font.Color:= clWhite;
    end else
    begin
     Brush.Color := clWIndow;
     Font.Color:= clBlack;
    end;
   FillRect(Item.DisplayRect(drBounds));
   R:=Item.DisplayRect(drLabel);
   TextRect(R,R.Left,R.Top,Item.Caption);
   NC:=Min(Item.SubItems.Count+1,TListView(Sender).Columns.Count);
   if NC<2 Then Exit;
   TotWidth:=Sender.Column[0].Width;
   for i:=1 to NC-1 do
    begin
     TextOut(R.Left+TotWidth,R.Top,Item.SubItems[i-1]);
     TotWidth:=TotWidth+Sender.Column[i].Width;
    end;
  end;
end;

Open in new window

0
 
LVL 1

Author Comment

by:peterkiers
ID: 33664437
Undeclared identifier 'min'

0
 
LVL 25

Expert Comment

by:epasquier
ID: 33664440
Uses Math;
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:peterkiers
ID: 33664495
The code is not correct as you can see in the image.
Naamloos.jpg
0
 
LVL 25

Expert Comment

by:epasquier
ID: 33664508
Note that it will not be pretty if columns are resized.
You can use this technique, it adds OnColumnResize events, and the bonus to invalidate the column drawings when resized - so they are redrawn properly without even needing to be told. It works well for me.

http://www.swissdelphicenter.ch/torry/showcode.php?id=1264
0
 
LVL 1

Author Comment

by:peterkiers
ID: 33664530
Well a you can see in the picture its not quit how I wanted it
0
 
LVL 25

Expert Comment

by:epasquier
ID: 33664578
Are you sure you did it right ? Check this :
*  DefaultDraw:=False;  (in custom draw event)
*  OwnerDraw := True

I have changed the colors to be sure that it is my custom drawing :
LVCustomDraw.gif
0
 
LVL 25

Expert Comment

by:epasquier
ID: 33664585
I tested it with Delphi 7, you seem to have a newer version (2007?), I'll test tomorrow with 2007.
0
 
LVL 1

Author Comment

by:peterkiers
ID: 33664645
It works fine. I had use the JvListview-component from JEDI VCL.
But now I have tried it on a listview and it works fine.

Greetings, Peter Kiers
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

752 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