在StringGrid的OnDrawCell 里处理,下面的代码不但水平居中,还垂直居中
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
var
s:string;
r:TRect;
begin
with StringGrid1 do
begin
Canvas.FillRect(Rect);
s:=Cells[ACol,ARow];
r:=Rect;
DrawText(Canvas.Handle,PChar(s),Length(s),r,DT_CENTER or DT_SINGLELINE or DT_VCENTER);
end;
end;
注意StringGrid的DefaultDrawing属性为True,否则你得自己决定Canvas 的字体颜色,大小等信息,比较麻烦