Announcement

Collapse
No announcement yet.

TListBox und Icons

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • TListBox und Icons

    Hallo

    Ist es möglich in der Listbox auch Icons oder bilder hinzuzufügen?
    ich möchte bei jeder zeile vor dem Text ein kleines icon oder bild.

    ich meine so

    myListBox.Items.add(Icon+'ihrgend ein text');

    wie geht das

    kan mir da einer helfen

    vielen dank sascha

  • #2
    Hallo,<br>Du mußt die Listbox-Einträge selber zeichen. Dafür liefert die TListbox die Ereignisse OnDrawItem und OnMeasureItem. OnMeasureItem wird vor OnDrawItem aufgerufen. In OnMeasureItem mußt Du die neue Höhe des Listbox-Eintrages berechnen und über den var-Parameter Height zurückgeben. In OnDrawItem zeichnest Du dann mit den üblichen TCanvas-Methoden die Listbox-Einträge <br>Jens Schuman

    Comment


    • #3
      Hallo,<br>hier ist ein kleines Beispiel.<br>
      <pre><font size="1" face="Verdana">
      unit Unit1;

      interface

      uses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      ImgList, StdCtrls;

      type
      TForm1 = class(TForm)
      ListBox1: TListBox;
      ImageList1: TImageList;
      procedure ListBox1DrawItem(Control: TWinControl; Index: Integer;
      Rect: TRect; State: TOwnerDrawState);
      procedure ListBox1MeasureItem(Control: TWinControl; Index: Integer;
      var Height: Integer);
      private
      { Private-Deklarationen }
      public
      { Public-Deklarationen }
      end;

      var
      Form1: TForm1;

      implementation

      {$R *.DFM}

      procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
      Rect: TRect; State: TOwnerDrawState);
      var
      aTextRect : TRect;
      aText : String;
      Bmp : TBitmap;
      BmpY : Integer;
      begin
      Bmp:=TBitmap.Create;
      Try
      ImageList1.GetBitmap(Index,Bmp);
      aText:=TListbox(Control).Items[Index];
      aTextRect:=Rect;
      aTextRect.Left:=aTextRect.Left+2+Bmp.Width;
      BmpY:=Rect.Top+(Rect.Bottom-Rect.Top) div 2 - Bmp.Height div 2;
      With TListbox(Control).Canvas do
      begin
      FillRect(Rect);
      Draw(Rect.Left,BmpY,Bmp);
      DrawText(Handle,PChar(aText),-1,aTextRect,dt_singleline+dt_vcenter+dt_left);
      end;
      Finally
      Bmp.Free;
      end;
      end;

      procedure TForm1.ListBox1MeasureItem(Control: TWinControl; Index: Integer;
      var Height: Integer);
      var
      aTextSize : TSize;
      aBmpHeight : Integer;
      Bmp : TBitmap;
      begin
      Bmp:=TBitmap.Create;
      Try
      TListbox(Control).Canvas.Font.Assign(TListbox(Cont rol).Font);
      aTextSize:=TListbox(Control).Canvas.TextExtent(TLi stbox(Control).Items[Index]);
      ImageList1.GetBitmap(Index,Bmp);
      If aTextSize.cy&gt;Bmp.Height then
      Height:=aTextSize.cy+2
      else
      Height:=Bmp.Height+2;
      Finally
      Bmp.Free;
      end;
      end;

      end.

      object ListBox1: TListBox
      Left = 20
      Top = 24
      Width = 285
      Height = 225
      Font.Charset = DEFAULT_CHARSET
      Font.Color = clWindowText
      Font.Height = -11
      Font.Name = 'MS Sans Serif'
      Font.Style = []
      ItemHeight = 16
      Items.Strings = (
      'Eintrag 1'
      'Eintrag 2'
      'Eintrag 3'
      'Eintrag 4'
      'Eintrag 5'
      'Eintrag 6')
      ParentFont = False
      Style = lbOwnerDrawVariable
      TabOrder = 0
      OnDrawItem = ListBox1DrawItem
      OnMeasureItem = ListBox1MeasureItem
      end
      </font></pre>
      <br>Jens Schuman

      Comment

      Working...
      X