Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Draw a selection in a listview

Posted on 2010-09-13
9
Medium Priority
?
392 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 2000 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
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!

 
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

721 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