Delphi   发布时间:2022-04-11  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Delphi:如何使单元格文本在TStringGrid中心对齐?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
看起来好像很明显.我想要的文本是在单元格的中心,但由于某些原因,我找不到它的属性.我该怎么做?

解决方法

在TStringGrid中没有任何文本中心的属性,但您可以在DrawCell事件中执行以下操作:
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol,ARow: Integer;
  Rect: TRect; State: TGridDrawStatE);
var
  S: String;
  SavedAlign: word;
begin
  if ACol = 1 then begin  // ACol is zero based
    S := StringGrid1.Cells[ACol,ARow]; // cell contents
    SavedAlign := SetTextAlign(StringGrid1.Canvas.Handle,TA_CENTER);
    StringGrid1.Canvas.TextRect(Rect,Rect.Left + (Rect.Right - Rect.Left) div 2,Rect.Top + 2,S);
    SetTextAlign(StringGrid1.Canvas.Handle,SavedAlign);
  end;
end;

我从here发布的代码

更新:

在单元格中写入文本时,将此代码添加到GetEditText事件中:

procedure TForm1.StringGrid1GetEditText(Sender: TObject; ACol,ARow: Integer;
  var Value: String);
var
  S : String;
  I: Integer;
  IE : TinplaceEdit ;
begin
  for I := 0 to StringGrid1.ControlCount - 1 do
    if StringGrid1.Controls[i].ClassName = 'TinplaceEdit' then
    begin
      IE := TinplaceEdit(StringGrid1.Controls[i]);
      ie.Alignment := taCenter
    end;
end;

大佬总结

以上是大佬教程为你收集整理的Delphi:如何使单元格文本在TStringGrid中心对齐?全部内容,希望文章能够帮你解决Delphi:如何使单元格文本在TStringGrid中心对齐?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。