whats new ¦  programming tips ¦  indy articles ¦  intraweb articles ¦  informations ¦  links ¦  interviews
 misc ¦  tutorials ¦  Add&Win Game

Tips (1547)

Database (90)
Files (137)
Forms (107)
Graphic (115)
IDE (21)
Indy (5)
Internet / LAN (131)
IntraWeb (0)
Math (76)
Misc (127)
Multimedia (45)
Objects/
ActiveX (51)

OpenTools API (3)
Printing (35)
Strings (83)
System (268)
VCL (243)

Top15

Tips sort by
component


Search Tip

Add new Tip

Add&Win Game

Advertising

33 Visitors Online


 
...display an alternating color for each TListBox row?
Autor: Peter Below
Homepage: http://www.teamb.com
[ Print tip ]  

Tip Rating (5):  
     




unit Unit1;

interface

uses
  
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type

  
TListbox = class(StdCtrls.TListbox)
  private
    procedure 
wmEraseBkGnd(var Msg: TWMEraseBkGnd); message WM_ERASEBKGND;
  end;
  
  TForm1 = class(TForm)
    ListBox1: TListBox; // (!) ListBox1.Style := lbOwnerDrawFixed
    
Button1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure ListBox1DrawItem(Control: TWinControl; Index: Integer;
      Rect: TRect; State: TOwnerDrawState);
    { ... }
  
end;

var
  
Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var
  
i: Integer;
begin
  for 
i := listbox1.Items.Count to listbox1.Items.Count + 5 do
    
listbox1.Items.Add(Format('Item %d', [i]));
end;

{ TListbox }
const
  
colors: array [Boolean] of TColor = ($FFFFC0, $C0FFFF);

procedure TListbox.wmEraseBkGnd(var Msg: TWMEraseBkGnd);
var
  
cv: TCanvas;
  h, max: Integer;
  r: TRect;
  b: Boolean;
begin
  
Msg.Result := 1;
  h          := Perform(LB_GETITEMHEIGHT, 0, 0);
  if h = LB_ERR then h := ItemHeight;
  cv := TCanvas.Create;
  try
    
cv.Handle := Msg.DC;
    r         := Rect(0, 0, ClientWidth, h);
    b         := Odd(TopIndex) and (TopIndex >= 0);
    max       := ClientHeight;
    cv.Brush.Style := bsSolid;
    while r.Top < max do 
    begin
      
cv.Brush.Color := colors[b];
      b := not b;
      cv.FillRect(r);
      OffsetRect(r, 0, h);
    end;
  finally
    
cv.Handle := 0;
    cv.Free;
  end;
end;

procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
  Rect: TRect; State: TOwnerDrawState);
var
  
cb, ct: TColor;
begin
  if not 
(odSelected in State) then
    with 
Control as TListbox do 
    begin
      
Canvas.Brush.Color := colors[Odd(Index)];
      Canvas.Brush.Style := bsSolid;
    end;
  Rect.Right := Control.ClientWidth;
  with Control as TListbox do 
  begin
    
Canvas.FillRect(Rect);
    Canvas.Brush.Style := bsClear;
    Canvas.TextRect(Rect, Rect.Left + 2, Rect.Top, Items[Index]);
  end;
end;

end.




 

Rate this tip:

poor
very good


Copyright © by SwissDelphiCenter.ch
All trademarks are the sole property of their respective owners