怎么改变StringGrid某一行的颜色?(50分)

  • 主题发起人 主题发起人 dohye
  • 开始时间 开始时间
procedure TForm1.DB ?Grid1DataCell(Sender:TObject;const Rect:TRect;
Field:TField;State:TGridDraw ?State);
var
ch:string;
begin
ch: =Field.fieldname;
if ch ='XM'then DBgrid1.canvas.brush.color: = $000000ff else
if ch ='XB'then DBgrid1.canvas.brush.color: = $0000800 else
if ch ='NY'then DBgrid1.canvas.brush.color: = $00ff0000 else
DBgrid1.canvas.brush.color: = $0020508f;
DGgridl.canvas.fillRect(Rect);
DBgridl.canvas.textout(Rect.left +4,Rect.top +4,field.Asstring);
end;
 
不好意思,刚才看错了,贴了DBGRID的内容了,不过StringGrid也差不多,
也主要是用canvas.brush.color,canvas.fillrect,canvas.textout的方式
进行多颜色的输出
 
改变当前行的颜色

在StringGrid1.Options中要包含goRowSelect



unit Unit1;

interface

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

type
TForm1 = class(TForm)
StringGrid1: TStringGrid;
procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
begin
with StringGrid1 do
with Canvas do
begin
if (ARow=Row)and(ACol>=FixedCols) then
Brush.Color:=clRed
else
if (ARow>=FixedRows)and(ACol>=FixedCols) then Brush.Color:=clWhite;
FillRect(Rect);
end;

end;

end.
 
呵呵, 不是有人已经回答过这个问题了么?
看<a href="http://www.gislab.ecnu.edu.cn/delphibbs/dispq.asp?LID=134658">这里</a>
 
我看了以前的问题,对自己重提老问题感到抱歉!!

procedure TForm1.StringGrid1Click(Sender: TObject);
begin
StringGrid1.Repaint; //由于DrawCell事件每次只画一个(我的试验结果),
在此强制刷新.
end;
过程后感到界面有闪烁!
不只怎么解决?
 
不要用repaint, 这玩意重画整个控件, 可以用下列代码只刷新当前行:
var
arect: TRect;
begin
arect := cellrect(row,col);
arect.left := 0;
arect.right := width;
invalidaterect(stringgrid1.handle, @arect, true);
end;
 
后退
顶部