Solved

Question about a color rows in my own listview component

Posted on 2010-09-16
5
453 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

895 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

13 Experts available now in Live!

Get 1:1 Help Now