怎么做到为StringGrid中的某一行定颜色啊???请高手赐教!! ( 积分: 100 )

2

2973

Unregistered / Unconfirmed
GUEST, unregistred user!
怎么做到为StringGrid中的某一行定颜色啊???请高手赐教!!
怎么做到为StringGrid中的某一行定颜色啊???请高手赐教!!
 
怎么做到为StringGrid中的某一行定颜色啊???请高手赐教!!
怎么做到为StringGrid中的某一行定颜色啊???请高手赐教!!
 
自己画出来的,在stringgrid的ondrawcell事件写代码:
procedure TForm1.StringGrid1DrawCell(Sender: TObject;
ACol, ARow: Integer;
Rect: TRect;
State: TGridDrawState);
begin
if ARow = 2 then
//将第二行变色
begin
StringGrid1.Canvas.Brush.Color := clblue;
StringGrid1.Canvas.TextRect(rect,rect.left,rect.top,StringGrid1.cells[acol,arow]);
end;
end;
 
1、声明一个结构,用于分辨不同的颜色,如:
type
TData=packed record
Color:TColor;
end;
PData=^TData;
2、用TStringGrid的Objects属性赋一个新的PData的指针。
New(PData);
pData.Color:=clRed;
StringGrid1.Object[1,2]:=pData;
3、在TStringGrid的OnDrawCell编写控制颜色:
procedure TForm1.OnStringGrid1DrawCell(Sender: TObject;
ACol, ARow: Longint;
Rect: TRect;
State: TGridDrawState
)
begin
self.StringGrid1.Canvas.Brush.Color:=PData(StringGrid1.Object[ACol,ARow])^.Color;
end;
4、要记录写释放掉Object的函数:
procedure TForm1.FormDestroy(Sender: TObject);
var
n:integer
p:pData;
begin
for n:=0 to self.StringGrid1.RowCount-1do
begin
p:=PData(self.StringGrid1.Object[1,n]);
Dispose(p);
end;
end;
 
顶部