以下是例子:
procedure TForm1.Button1Click(Sender: TObject);
var
x, i, j, w, h: integer;
rect1: TRect;
begin
i := 3; //列
j := 4; //行
w := 0;
h := 0;
for x := 0 to i-1 do
w := w + StringGrid1.ColWidths[x] + StringGrid1.GridLineWidth;
for x := 0 to j-1 do
h := h + StringGrid1.RowHeights[x] + StringGrid1.GridLineWidth;
with rect1 do
begin
Left := w;
Top := h;
Right := Left + StringGrid1.ColWidths + StringGrid1.GridLineWidth;
Bottom := Top + StringGrid1.RowHeights[j] + StringGrid1.GridLineWidth;
end;
StringGrid1.Canvas.Brush.Color := clRed;
StringGrid1.Canvas.FillRect(rect1);
end;