Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Question about a color rows in my own listview component

Posted on 2010-09-16
5
Medium Priority
?
465 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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

604 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