看起来好像很明显.我想要的文本是在单元格的中心,但由于某些原因,我找不到它的属性.我该怎么做?
解决方法
在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;
更新:
在单元格中写入文本时,将此代码添加到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;