Delphi:如何使单元格文本在TStringGrid中心对齐?
发布时间:2020-12-15 04:27:21 所属栏目:大数据 来源:网络整理
导读:看起来好像很明显.我想要的文本是在单元格的中心,但由于某些原因,我找不到它的属性.我该怎么做? 解决方法 在TStringGrid中没有任何文本中心的属性,但您可以在DrawCell事件中执行以下操作: procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol,ARo
看起来好像很明显.我想要的文本是在单元格的中心,但由于某些原因,我找不到它的属性.我该怎么做?
解决方法
在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; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |