我必须在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。有什么解决办法吗?
发布于 2013-08-09 14:24:43
由于您使用的是Lazarus,所以我不会依赖特定于平台的Windows函数,而是使用内置的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函数和SetTextAlign函数使用。您应该使用DT_函数使用的DrawText函数。
https://stackoverflow.com/questions/18148412
复制相似问题