+ -
当前位置:首页 → 问答吧 → 如何设置StringGrid单元格内容水平、垂直居中

如何设置StringGrid单元格内容水平、垂直居中

时间:2011-12-20

来源:互联网

如何设置StringGrid单元格内容水平、垂直居中

作者: yuhong0725   发布时间: 2011-12-20

没有属性可以设置,只能自己画。比较麻烦
stringgrid可以用下面的方法,响应grid的 ondrawcell事件,在响应事件 
里自己提供画表格中元素的方法,当然你就可以随便怎样定义它的对齐方式了。 
一个例子: 
//表格OnDrawCell事件方法 
procedure TFormTest.StringGridDrawCell(Sender: TObject; ACol, 
  ARow: Integer; Rect: TRect; State: TGridDrawState); 
const 
  WSpace = 2; 
  HSpace = 2; 
var 
  tmpstr: string; 
  w,h: integer; 
begin 
  if (Sender as TStringGrid).tag>0 then 
  begin 
  tmpstr := (Sender as TStringGrid).Cells[ACol,ARow]; 
  with (Sender as TStringGrid).Canvas do 
  begin 
  w := TextWidth(tmpstr); 
  h := TextHeight(tmpstr); 
  if (gdFixed in State) then  
  TextRect(Rect,trunc((Rect.Right+Rect.Left-w)/2)+WSpace, //居中显示 
  trunc((Rect.Bottom+Rect.Top-H)/2)+HSpace,tmpstr) 
  else //右对齐显示 
  TextRect(Rect,Rect.Right-WSpace-W,Rect.Bottom-HSpace-H,tmpstr); 
  end; 
  end; 
end;

从别的贴子拷贝过来的。看来你自己实在懒的动!就在CSDN上。

作者: nm_wyh   发布时间: 2011-12-20

http://topic.csdn.net/t/20031028/09/2401656.html

作者: ksrsoft   发布时间: 2011-12-20

http://blog.csdn.net/csdsym/article/details/3047678

作者: ksrsoft   发布时间: 2011-12-20