Solved

Question about a color rows in my own listview component

Posted on 2010-09-16
5
457 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
[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
  • 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

Industry Leaders: 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!

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

726 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