Solved

Draw a selection in a listview

Posted on 2010-09-13
9
363 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
 
LVL 1

Author Comment

by:peterkiers
ID: 33664495
The code is not correct as you can see in the image.
Naamloos.jpg
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

706 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now