Solved

Draw a selection in a listview

Posted on 2010-09-13
9
371 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
  • 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

809 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