Add 2 line signature to TListView?

in the shortcut i can add a new line like this

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

can this be done in TListView?

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

thank

+3
source share
3 answers

You can have multi-line strings in the standard style TListViewin vsReport, but AFAIK does not support different line heights. However, if you have all the rows with the same number of rows> 1, you can achieve this quite easily.

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

, , , TListView, StateImages, height, StateImages . .

, , - , .

+1

I seem to be unable to achieve this using TListView. But using TMS TAdvListView , you can use HTML in the text of the element, so that it places the signature on 2 lines:

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

Source: https://habr.com/ru/post/1725225/


All Articles