微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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;

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐