当前位置 : 主页 > 编程语言 > delphi >

delphi – TA_CENTER不适用于居中对齐StringGrid

来源:互联网 收集:自由互联 发布时间:2021-06-23
我必须在StringGrid(它的单元格)中居中对齐文本,我使用你在这里看到的代码.我在这里找到了另一个答案,我编辑了一些东西. procedure TForm1.StringGrid1DrawCell(Sender: TObject; aCol, aRow: Integer; aRec
我必须在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.有解决方案吗

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

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函数使用.您应该使用DrawText函数使用的DT_.

网友评论