在标签中,我可以像这样添加新行

Label.Caption:='First line'+#13#10+'SecondLine';


可以在TListView中完成吗?

listItem:=listView.Items.Add;
listItem.Caption:='First line'+#13#10+'SecondLine';


谢谢

最佳答案

可以使用TListView样式的标准vsReport中的多行字符串,但是AFAIK它不支持变化的行高。但是,如果所有行的行数均大于1,则可以轻松实现。

您需要将列表视图设置为OwnerDraw模式,首先,您可以实际绘制多行标题,其次,可以将行高增加到必要的值。这是通过处理WM_MEASUREITEM消息完成的,该消息仅针对所有者绘制的列表视图发送。

一个小例子来说明这一点:

type
  TForm1 = class(TForm)
    ListView1: TListView;
    procedure FormCreate(Sender: TObject);
    procedure ListView1DrawItem(Sender: TCustomListView; Item: TListItem;
      Rect: TRect; State: TOwnerDrawState);
  private
    procedure WMMeasureItem(var AMsg: TWMMeasureItem); message WM_MEASUREITEM;
  end;

{ TForm1 }

procedure TForm1.FormCreate(Sender: TObject);
begin
  ListView1.ViewStyle := vsReport;
  ListView1.OwnerDraw := True;
  ListView1.OwnerData := True;
  ListView1.Items.Count := 1000;
  with ListView1.Columns.Add do begin
    Caption := 'Multiline string test';
    Width := 400;
  end;
  ListView1.OnDrawItem := ListView1DrawItem;
end;

procedure TForm1.ListView1DrawItem(Sender: TCustomListView;
  Item: TListItem; Rect: TRect; State: TOwnerDrawState);
begin
  if odSelected in State then begin
    Sender.Canvas.Brush.Color := clHighlight;
    Sender.Canvas.Font.Color := clHighlightText;
  end;
  Sender.Canvas.FillRect(Rect);
  InflateRect(Rect, -2, -2);
  DrawText(Sender.Canvas.Handle,
    PChar(Format('Multiline string for'#13#10'Item %d', [Item.Index])),
    -1, Rect, DT_LEFT);
end;

procedure TForm1.WMMeasureItem(var AMsg: TWMMeasureItem);
begin
  inherited;
  if AMsg.IDCtl = ListView1.Handle then
    AMsg.MeasureItemStruct^.itemHeight := 4 + 2 * ListView1.Canvas.TextHeight('Wg');
end;

10-07 19:36