说明:展示 ListView 视其每一行 Item 的 Detail 字串长度自动调整高度(可每行高度不同)。
适用:Delphi XE7 / XE8
DetailStr: [..] String =
i:=
== Random(= DetailStr[r].Substring(=<span style="color: #0000ff;">procedure TForm1.ListView1UpdateObjects(<span style="color: #0000ff;">const<span style="color: #000000;"> Sender: TObject;
<span style="color: #0000ff;">const<span style="color: #000000;"> AItem: TListViewItem);
<span style="color: #0000ff;">var<span style="color: #000000;"> R: TRectF;
<span style="color: #0000ff;">begin
<span style="color: #0000ff;">if (AItem.Objects.DetailObject <> <span style="color: #0000ff;">nil) <span style="color: #0000ff;">and<span style="color: #000000;">
(AItem.Objects.DetailObject.Text <> <span style="color: #800000;">'') <span style="color: #0000ff;">then
<span style="color: #0000ff;">begin
<span style="color: #008000;">//<span style="color: #008000;"> 计算文字显示的区域
R := RectF(<span style="color: #800080;">0,<span style="color: #800080;">0,AItem.Objects.DetailObject.Width,<span style="color: #800080;">10000<span style="color: #000000;">);
ListView1.Canvas.MeasureText(R,AItem.Objects.DetailObject.Text,AItem.Objects.DetailObject.WordWrap,[],TTextAlign.Leading,TTextAlign.Leading);
<span style="color: #0000ff;">const<span style="color: #000000;"> AItem: TListViewItem);
<span style="color: #0000ff;">var<span style="color: #000000;"> R: TRectF;
<span style="color: #0000ff;">begin
<span style="color: #0000ff;">if (AItem.Objects.DetailObject <> <span style="color: #0000ff;">nil) <span style="color: #0000ff;">and<span style="color: #000000;">
(AItem.Objects.DetailObject.Text <> <span style="color: #800000;">'') <span style="color: #0000ff;">then
<span style="color: #0000ff;">begin
<span style="color: #008000;">//<span style="color: #008000;"> 计算文字显示的区域
R := RectF(<span style="color: #800080;">0,<span style="color: #800080;">0,AItem.Objects.DetailObject.Width,<span style="color: #800080;">10000<span style="color: #000000;">);
ListView1.Canvas.MeasureText(R,AItem.Objects.DetailObject.Text,AItem.Objects.DetailObject.WordWrap,[],TTextAlign.Leading,TTextAlign.Leading);
</span><span style="color: #008000;">//</span><span style="color: #008000;"> 设定高度</span>
AItem.Height :=<span style="color: #000000;"> Trunc(R.Height);
</span><span style="color: #0000ff;">end</span><span style="color: #000000;">;
<span style="color: #0000ff;">end;
补充:计算最后一项区域并卷到最后一项
= ListView1.GetItemRect(ListView1.Items.Count - );
ListView1.ScrollViewPos := R.Bottom;
;