我必须将 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/