在TListView中添加2行标题?

时间:2009-12-09 22:26:15

标签: delphi listview

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

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

可以在TListView中完成吗?

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

感谢

3 个答案:

答案 0 :(得分:1)

可以在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;

答案 1 :(得分:1)

我知道这是一个旧线程,我无法理解这一点,但要调整TListView中的行高,您可以为StateImages添加图像列表,然后通过展开属性窗口中的StateImages项来指定图像高度。您无需加载任何实际图像。

对不起,我不能相信那个想出来的人 - 这是我在一段时间前去过的论坛。

答案 2 :(得分:0)

我似乎无法使用TListView实现此目的。但是使用TMS TAdvListView,您可以在项目文本中使用HTML,这样就可以将标题放在2行上:

  with AdvListView1.Items.Add do
  begin                           
    Caption := '<FONT color="clBlue">Line 1<BR>Line 2</font>';
  end;
相关问题