4

我必须将 StringGrid(其单元格)中的文本居中对齐,并且我正在使用您在此处看到的代码。我在这里的另一个答案中找到了它,并编辑了一些东西。

procedure TForm1.StringGrid1DrawCell(Sender: TObject; aCol, aRow: Integer;
  aRect: TRect; aState: TGridDrawState);
var
  LStrCell: string;
  LRect: TRect;
  qrt:double;
begin
  LStrCell := StringGrid1.Cells[ACol, ARow];
  StringGrid1.Canvas.FillRect(aRect);
  LRect := aRect;
  DrawText(StringGrid1.Canvas.Handle, PChar(LStrCell), Length(LStrCell), LRect, TA_CENTER);

  //other code

end;

我正在使用Lazarus它给我一个错误,因为它无法识别TA_CENTER. 有什么解决办法吗?

4

1 回答 1

5

由于您使用的是 Lazarus,因此我不会依赖特定于平台的 Windows API 函数,而是使用内置的 canvasTextRect方法。在(未经测试的)代码中,它可能是:

procedure TForm1.StringGrid1DrawCell(Sender: TObject; aCol, aRow: Integer;
  aRect: TRect; aState: TGridDrawState);
var
  CellText: string;
  TextStyle: TTextStyle;
begin
  CellText := StringGrid1.Cells[ACol, ARow];
  StringGrid1.Canvas.FillRect(ARect);

  TextStyle := StringGrid1.Canvas.TextStyle;
  TextStyle.Alignment := taCenter;
  StringGrid1.Canvas.TextRect(ARect, 0, 0, CellText, TextStyle);
  ...    
end;

无论如何,您已经使用了一个TA_CENTER由不同的 Windows API 函数使用的常量SetTextAlign。您应该已经使用了该函数使用的DT_那些。DrawText

于 2013-08-09T14:24:43.187 回答