自动适应列宽

以下这段过程是本人自制的TSortDBG的自动调整显示宽度的过程,其中self指本TSortDBG.

///
procedure TSortDBG.AdjustShowColumnWidth;
var
  i, iWidth: Integer;
  aiWidth: array of Integer;
  bm: TBookMark;
begin
  if not FAutoColumnWidth then Exit;
  SetLength(aiWidth,Columns.Count);
  Screen.Cursor := crHourGlass;
  for i := Low(aiWidth) to High(aiWidth) do
    aiWidth[i] := 0;
  if DataSource<>nil then
    if DataSource.DataSet<>nil then
      if DataSource.DataSet.Active then
      begin
        bm := DataSource.DataSet.GetBookmark;
        DataSource.DataSet.DisableControls;
        DataSource.DataSet.First;
        while not DataSource.DataSet.Eof do
        begin
          for i := Low(aiWidth) to High(aiWidth) do
          begin
            iWidth := Self.Canvas.TextWidth(Self.Fields[i].DisplayText);
            if iWidth>aiWidth[i] then
              aiWidth[i] := iWidth;
          end;
          DataSource.DataSet.Next;
        end;
        DataSource.DataSet.GotoBookmark(bm);
        DataSource.DataSet.EnableControls;
        DataSource.DataSet.FreeBookmark(bm);
      end;
  for i := Low(aiWidth) to High(aiWidth) do
  begin
    iWidth := Self.Canvas.TextWidth(Self.Columns.Items[i].Title.Caption);
    if iWidth>aiWidth[i] then
      aiWidth[i] := iWidth;
    Columns.Items[i].Width := aiWidth[i]+4;
  end;
  Screen.Cursor := crDefault;
end;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值