Delphi的TA_CENTER为何不适应StringGrid居中显示长尾词?

2026-04-10 02:411阅读0评论SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计347个文字,预计阅读时间需要2分钟。

Delphi的TA_CENTER为何不适应StringGrid居中显示长尾词?

在StringGrid(它的单元格)中居中对齐文本,请参考以下代码。我找到了一个解决方案,并做了一些编辑。

Delphi的TA_CENTER为何不适应StringGrid居中显示长尾词?

procedure TForm1.StringGrid1DrawCell(Sender: TObject; aCol, aRow: Integer; aRect: TRect);var TextRect: TRect;begin TextRect :=aRect; TextRect.Offset((aRect.Width - TextWidth(StringGrid1.Cells[aCol, aRow])) div 2, 0); Canvas.TextOut(TextRect, StringGrid1.Cells[aCol, aRow]);end;

我必须在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_.

本文共计347个文字,预计阅读时间需要2分钟。

Delphi的TA_CENTER为何不适应StringGrid居中显示长尾词?

在StringGrid(它的单元格)中居中对齐文本,请参考以下代码。我找到了一个解决方案,并做了一些编辑。

Delphi的TA_CENTER为何不适应StringGrid居中显示长尾词?

procedure TForm1.StringGrid1DrawCell(Sender: TObject; aCol, aRow: Integer; aRect: TRect);var TextRect: TRect;begin TextRect :=aRect; TextRect.Offset((aRect.Width - TextWidth(StringGrid1.Cells[aCol, aRow])) div 2, 0); Canvas.TextOut(TextRect, StringGrid1.Cells[aCol, aRow]);end;

我必须在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_.