procedure TForm1.ListView1CustomDrawItem(Sender: TCustomListView;
Item: TListItem; State: TCustomDrawState; var DefaultDraw: Boolean);
var
Rect:Trect;
begin
DefaultDraw:=false;
Rect:=Item.DisplayRect( drBounds );
With Sender as TListView do begin
case item.Index of //行数控制
0,2,4:begin
if cdsSelected in State then begin //高亮判断
canvas.brush.Color :=clHighlight;
Canvas.font.Color :=clWhite;
end else begin
Canvas.Brush.Color :=rgb(255,222,222);
Canvas.font.Color :=clBlack;
end;
end;
1,3:begin
if cdsSelected in State then begin
canvas.brush.Color :=clHighlight;
Canvas.font.Color :=clWhite;
end else begin
Canvas.Brush.Color :=rgb(222,222,255);
Canvas.font.Color :=clBlack;
end;
end;
end;
Canvas.FillRect (Rect);
Canvas.brush.Style :=bsClear;
Canvas.TextRect (Rect,rect.left,rect.top,item.Caption);
if ListView1.ViewStyle =vsReport then begin
Canvas.TextRect (Rect,(Rect.left+(Rect.right-rect.left)div 2),rect.top,
item.SubItems.Text );
end;
end;
end;
procedure SetCell(ListView: TListView; Row, Col: Integer; const Value: string);
begin
if Col = 0 then
ListView.Items[Row].Caption := Value
else begin
while ListView.Items[Row].SubItems.Count < Col - 2 do
ListView.Items[Row].SubItems.Add('');
ListView.Items[Row].SubItems[Col - 1] := Value;
end
end;
需要这样的方法是在LISTVIEW的OnCustomDraw,以及OnCustomDrawitem事件里重写,并且要把DeafaultDraw设为假,这样完全控制如何绘制你想要的效果.下面是一个在背景变成红色后,写一行字的例子:
procedure TForm1.ListView1CustomDraw(Sender: TCustomListView;
const ARect: TRect; var DefaultDraw: Boolean);
begin
With ListView1 do begin
Canvas.brush.Color :=clRed;
canvas.FillRect (Arect);
Canvas.TextOut (0,0, 'This is a test');
end;
DefaultDraw:=true;
procedure TForm1.DrawGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
begin
With DrawGrid1 do begin
if Arow mod 2 =0 then begin
canvas.brush.Color :=rgb(200,255,212);
canvas.FillRect (rect);
end;
end;
end;