[Delphi] 2行表示のリストを作成する

2行表示のリストを作成するには、OnDrawItem イベントと OnMeasureItem イベントを使用します。
以下のステップで行います。

  1. OnMeasureItemイベントで2行分表示するための高さを確保します。
  2. OnDrawItemイベントでCanvasにテキストを描画します。
  3. Styleを「lbOwnerDrawVariable」に変更します。(自分で中身を書き換えますよ!という定義)

この方法は、TCheckListBox でも使用できます。(2行目が固定で“2行目”になっているので、お好みに合わせて変更してください。 また、Styleを「lbOwnerDrawVariable」に変更するのを忘れないでください。)

procedure TForm1.ListBox1MeasureItem(Control: TWinControl; Index: Integer;
  var Height: Integer);
begin
  Height := -1 * TListBox(Control).Font.Height * 2 + 6; // リストボックスの1項目の高さを設定
end;

procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
  Rect: TRect; State: TOwnerDrawState);
begin
  with TListBox(Control) do
  begin
  Canvas.FillRect(Rect);
  Canvas.TextOut(Rect.Left + 2, Rect.Top + 2, Items[Index]);                 // 1行目
  Canvas.TextOut(Rect.Left + 2, Rect.Top + (-1 * Font.Height) + 4, '2行目'); // 2行目
  end;
end;

コメント (0件)


くろねこ研究所
https://www.blackcat.xyz/article.php/ProgramingFAQ_del0005