5,392
社区成员
发帖
与我相关
我的任务
分享
procedure TForm1.FormCreate(Sender: TObject);
begin
StringGrid1.Cells[1,1]:='StringGrid同一个单元格颜色';
end;
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
var s:String;
i,len,w1,w2:integer;
begin
if(ACol=1)and(ARow=1)then begin
s:=StringGrid1.Cells[1,1];
if trim(s)='' then exit;
len:=pos('单元格',s);
w1:=0;
w2:=0;
for i:=1 to length(s) do begin
if i<len then w1:=w1+StringGrid1.Canvas.TextWidth(s[i])
else if i<len+6 then w2:=w2+StringGrid1.Canvas.TextWidth(s[i])
end;
StringGrid1.Canvas.Font.Color:=clWindowText;
StringGrid1.Canvas.TextOut(Rect.Left+2,Rect.Top+4,copy(s,1,len-1));
StringGrid1.Canvas.Font.Color:=clBlue;
StringGrid1.Canvas.TextOut(Rect.Left+w1+4,Rect.Top+4,'单元格');
if length(s)>len+5 then begin
StringGrid1.Canvas.Font.Color:=clRed;
StringGrid1.Canvas.TextOut(Rect.Left+w1+w2+8,Rect.Top+4,copy(s,len+6,length(s)));
end;
end;
end;