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

标签 delphi lazarus

我必须将 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_ 。功能。

关于delphi - TA_CENTER 不适用于居中对齐 StringGrid,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18148412/

相关文章:

delphi - Delphi Seattle,编译错误: F2051 Unit System.SysUtils was compiled with a different version of “” .GetMappedFileName

delphi - 如果我不小心从控件引用的表单使用列表中删除某些内容,是否会产生任何有害影响?

delphi - 带有TRectangle的GetPropList返回StrokeThickness作为属性,应作为Stroke类的一部分

delphi - TStatusBar 与底部对齐面板

linux - Lazarus Linux Ubuntu 上的线程

ios - 在Mac上为iOS开发设置FPC和Lazarus

delphi - 对角画笔样式给我黑色区域

pascal - 什么是好的 Lazarus/FPC 资源?

database - 数据库的 Post、ApplyUpdates 和 Commit 之间有什么区别?

delphi - Lazarus IDE 中多个编译器的全局管理