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

yuhong0725 2011-12-20 01:42:00
如何设置StringGrid单元格内容水平、垂直居中
...全文
833 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
kaikai_kk 2011-12-21
  • 打赏
  • 举报
回复
这个要重画了
nm_wyh 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上。

16,744

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 语言基础/算法/系统设计
社区管理员
  • 语言基础/算法/系统设计社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧