procedure TMainForm.StringGrid1DrawCell(Sender: TObject; ACol,
ARow: Integer; Rect: TRect; State: TGridDrawState);
var
vText: PChar;
begin
with TStringGrid(Sender) do
begin
vText := PChar(Cells[ACol, ARow]);
Canvas.FillRect(Rect);
//下面这行设置对齐方式
//右对齐
DrawText(Canvas.Handle, vText, StrLen(vText), Rect,DT_Right);
//左对齐
DrawText(Canvas.Handle, vText, StrLen(vText), Rect,DT_left);
//居中
DrawText(Canvas.Handle, vText, StrLen(vText), Rect,DT_CENTER OR DT_VCENTER OR DT_SINGLELINE )
if gdFocused in State then
begin
Rect.Left := Rect.Left +1;
Rect.Top := Rect.Top +1;
Rect.Right := Rect.Right-1;
Rect.Bottom := Rect.Bottom-1;
Canvas.DrawFocusRect(Rect);
end;
end;
end;
DrawText(Canvas.Handle, vText, StrLen(vText), Rect,DT_Right);
//左对齐
DrawText(Canvas.Handle, vText, StrLen(vText), Rect,DT_left);
//居中
DrawText(Canvas.Handle, vText, StrLen(vText), Rect,DT_CENTER OR DT_VCENTER OR DT_SINGLELINE )