Solved

Question about a color rows in my own listview component

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

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.

Join & Write a Comment

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…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

747 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

9 Experts available now in Live!

Get 1:1 Help Now