以下这段过程是本人自制的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;
///
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;