記事カテゴリ

ユーザー機能


 2024年4月24日(水) 00:24 JST

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

  • 投稿者:
  • 表示回数
    4,179

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;

トラックバック

このエントリのトラックバックURL:
https://www.blackcat.xyz/trackback.php/ProgramingFAQ_del0005

以下のコメントは、その投稿者が所有するものでサイト管理者はコメントに関する責任を負いません。