Solved

Question about a color rows in my own listview component

Posted on 2010-09-16
5
456 Views
Last Modified: 2012-05-10
Dear Experts,

I have this to color the rows of a listview:

procedure TMainForm.PkListv1CustomDrawItem(Sender: TCustomListView;
  Item: TListItem; State: TCustomDrawState; var DefaultDraw: Boolean);
begin
  if Odd(Item.Index) then
    PkListv1.Canvas.Brush.Color := clGray
  else
    PkListv1.Canvas.Brush.Color := clWindow;
end;

I am making my own listview-component, can this code above
be build in the component itself, i guess i have to use the onpaint
event, but I have no clue how to start. Does someone has the
experience with that?

Greetings, Peter
unit PkListView;

interface

uses
  SysUtils, Classes, Controls, ComCtrls, Windows;

type
  PkListv = class(TListView)
  private
    { Private declarations }
  protected
    { Protected declarations }
   FHeaderHandle: HWND;
   function GetHeaderHeight:Integer;
   procedure SetHeaderHeight(h:Integer);
  public
    { Public declarations }
    constructor Create(AOwner: TComponent);
    destructor Destroy();
  published
    { Published declarations }
   property HeaderHeight:Integer read GetHeaderHeight write SetHeaderHeight;
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('Samples', [PkListv]);
end;

{ PkListv }

const
  LVM_FIRST = $1000;
  LVM_GETHEADER = LVM_FIRST + 31;

constructor PkListv.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);

end;

destructor PkListv.Destroy;
begin
  inherited;
end;

function PkListv.GetHeaderHeight: Integer;
Var
  Rc:TRect;
begin
 Result:=0;
 FHeaderHandle:=SendMessage(Handle, LVM_GETHEADER, 0, 0);
 If GetWindowRect(FHeaderHandle, rc)
  Then Result := rc.Bottom - rc.Top
end;

procedure PkListv.SetHeaderHeight(h: Integer);
begin
 FHeaderHandle:=SendMessage(Handle, LVM_GETHEADER, 0, 0);
 SetWindowPos( FHeaderHandle, 0 , 0, 0, Width, h, 0 );
 InvalidateRect(FHeaderHandle,nil,True);
end;

end.

Open in new window

0
Comment
Question by:peterkiers
  • 3
  • 2
5 Comments
 
LVL 25

Expert Comment

by:epasquier
ID: 33689660
you'll have to override TCustomListView.CustomDrawItem method that is calling this event, see Delphi code below :

function TCustomListView.CustomDrawItem(Item: TListItem; State: TCustomDrawState;
  Stage: TCustomDrawStage): Boolean;
begin
  Result := True;
  if (Stage = cdPrePaint) and Assigned(FOnCustomDrawItem) then FOnCustomDrawItem(Self, Item, State, Result);
  if Assigned(FOnAdvancedCustomDrawItem) then FOnAdvancedCustomDrawItem(Self, Item, State, Stage, Result);
end;
  PkListv = class(TListView)
  private
    { Private declarations }
  protected
    { Protected declarations }
   FHeaderHandle: HWND;
   function GetHeaderHeight:Integer;
   procedure SetHeaderHeight(h:Integer);
   function CustomDrawItem(Item: TListItem; State: TCustomDrawState;
      Stage: TCustomDrawStage): Boolean; override;
  public
    { Public declarations }
    constructor Create(AOwner: TComponent);
    destructor Destroy();
  published
    { Published declarations }
   property HeaderHeight:Integer read GetHeaderHeight write SetHeaderHeight;
  end;

implementation
...

function PkListv.CustomDrawItem(Item: TListItem; State: TCustomDrawState;
  Stage: TCustomDrawStage): Boolean;
begin
 if Odd(Item.Index) 
  then Canvas.Brush.Color := clGray
  else Canvas.Brush.Color := clWindow;
 Result:= inherited CustomDrawItem(Item,State,Stage);
end;

Open in new window

0
 
LVL 1

Author Comment

by:peterkiers
ID: 33689708
How do you do that?

How do you know that the customdrawitem has to be changed?

Peter

0
 
LVL 1

Author Comment

by:peterkiers
ID: 33689729
In order to test your code I have to put data in the rows of the listview:
How can I fill a line of text dynamicly?

procedure TMainForm.btnTESTClick(Sender: TObject);
var
  MyListv: PkListv;
  MyListCol : TListColumn;
begin
  MyListv:= PkListv.Create(Self);
  MyListv.Parent := self;
  MyListv.Align := alTop;
  MyListv.Height := 300;
  MyListv.ViewStyle := vsReport;
  MyListv.GridLines := True;
  MyListv.ColumnClick := False;
  MyListv.SmallImages := ImageList1;
  MyListv.HeaderHeight:=20;
  MyListv.Items <==================================
  MyListCol := MyListv.Columns.Add;
  MyListCol.Alignment := taLeftJustify;
  MyListCol.Width := 150;
  MyListCol.Caption := 'Naam';
end;
0
 
LVL 25

Accepted Solution

by:
epasquier earned 500 total points
ID: 33689793
Fill a TListView with random text - Free for you :o)
function RandomChar(CharSet:String):Char;
begin
 Result:=CharSet[Random(Length(CharSet))+1];
end;

function RandomString(Max:Integer):String;
Var L:Integer;
begin
 L:=0;
 Result:='';
 For L:=0 To Max-1 do
  begin
   if (L=0) Or (Result[L]=' ')
    Then Result:=Result+RandomChar('ABCDEFGHIJKLMNOPQRSTUVWXYZ')
    Else if (L<Max-1) And (Random(6)=0)
     Then Result:=Result+' '
     Else Result:=Result+RandomChar('abcdefghijklmnopqrstuvwxyz');
  end;
end;

procedure TForm1.btn1Click(Sender: TObject);
Var
 i,N:integer;
begin
 for N:=0 to 3 do With lv1.Items.Add do
  begin
   Caption:=RandomString(15);
   for i:=0 to lv1.Columns.Count-2 do SubItems.Add(RandomString(10));
  end;
end;

Open in new window

0
 
LVL 1

Author Comment

by:peterkiers
ID: 33689945
Thanks it works.

Peter
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

828 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