怎样使TListBox有自动换行的功能?

解决方案 »

  1.   

    超出边界另起一行,就像这样-----------------------|
    fgsdfhsdhsdhgghghghhghg|
    ghfghfghfhgfhghgh      |
    -----------------------|
    fgsdfhsdhsdhgghghghhghg|
    ghfghfghf              |
    -----------------------|
    fgsdfhsdhsdhgghghghhghg|
    ghfghfghffdfghshghghhgh|
    ghfhghhghghghgggh      |
    -----------------------|
      

  2.   

    首先listbox的style属性要设置为:
      form1.ListBox1.Style:=lbVirtual;
      

  3.   

    function WrapTextWidthHeight(mText: string; mCount: Integer; mCanvas: TCanvas): TPoint;
    { 得到文本换行后的宽和高 }
    var
      I: Integer;
    begin
      Result.X := 0;
      with TStringList.Create do try
        Text := WrapText(mText, mCount);
        Result.Y := Count * mCanvas.TextHeight('|');
        for I := 0 to Count - 1 do
          if mCanvas.TextWidth(Strings[I]) > Result.X then
            Result.X := mCanvas.TextWidth(Strings[I]);
      finally
        Free;
      end;
    end; { WrapTextWidthHeight }procedure TextToCanvas(mText: string; mCanvas: TCanvas; mOffset: TPoint);
    { 将多行文本输出到画布上 }
    var
      I: Integer;
      vTextHeight: Integer;
    begin
      with mCanvas, TStringList.Create do try
        Text := mText;
        vTextHeight := TextHeight('|');
        for I := 0 to Pred(Count) do
          TextOut(mOffset.X, mOffset.Y + vTextHeight * I, Strings[I]);
      finally
        Free;
      end;
    end; { TextToCanvas }procedure TForm1.ListBox1MeasureItem(Control: TWinControl; Index: Integer;
      var Height: Integer);
    begin
      Height := WrapTextWidthHeight(TListBox(Control).Items[Index], FCharCount,
        TListBox(Control).Canvas).Y + 1;
    end;procedure TForm1.FormCreate(Sender: TObject);
    begin
      FCharCount := ListBox1.Width div ListBox1.Canvas.TextWidth('W') - 20;
      Font.Name := '宋体';
      Font.Size := 9;
      ListBox1.Items.Text :=
    '14. Delphi Utilities'#13#10 +
    '==========================================================='#13#10 +
    'The utility programs included with Delphi 6 may NOT be'#13#10 +
    'redistributed with deployed applications. These utility'#13#10 +
    'programs include, but are not limited to:'#13#10 +
    ''#13#10 +
    ''#13#10 +
    '  SQL Explorer (Enterprise only)'#13#10 +
    '  SQL Monitor (Enterprise only)'#13#10 +
    '  Translation Manager (Enterprise only)'#13#10 +
    '  Database Explorer (Professional and Enterprise only)'#13#10 +
    '  Database Desktop (Professional and Enterprise only)'#13#10 +
    '  Openhelp (Professional and Enterprise only)'#13#10 +
    '  Package Collection Editor (Professional and Enterprise only)'#13#10 +
    '  WinSight (Professional and Enterprise only)'#13#10 +
    '  DCC32'#13#10 +
    '  GREP'#13#10 +
    '  TDump'#13#10 +
    '  TLibImp'#13#10 +
    '  Image Editor'#13#10 +
    ''#13#10 +
    'Additional licensing information for distribution of the'#13#10 +
    'Translation Manager will be made available on the Borland'#13#10 +
    'website, www.borland.com.'#13#10;
    end;procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
      Rect: TRect; State: TOwnerDrawState);
    begin
      TListBox(Control).Canvas.FillRect(Rect);
      TextToCanvas(WrapText(ListBox1.Items[Index], FCharCount),
        TListBox(Control).Canvas, Rect.TopLeft);
      TListBox(Control).Canvas.Pen.Color := clYellow;
      TListBox(Control).Canvas.MoveTo(0, Rect.Bottom - 1);
      TListBox(Control).Canvas.LineTo(Rect.Right, Rect.Bottom - 1);
    end;
      

  4.   

    object ListBox1: TListBox
      Left = 288
      Top = 32
      Width = 185
      Height = 209
      Style = lbOwnerDrawVariable
      ItemHeight = 16
      TabOrder = 1
      OnDrawItem = ListBox1DrawItem
      OnMeasureItem = ListBox1MeasureItem
    end
      

  5.   

    可能不行
    可以加水平scroll
    procedure TForm1.FormCreate(Sender: TObject);
    var
      i,maxwidth:integer;
    begin
      maxwidth:=0;
      for i:=0 to form1.ListBox1.Items.Count-1 do
        begin
          if maxwidth<form1.ListBox1.Canvas.TextWidth(form1.ListBox1.Items.Strings[i]) then
            begin
            maxwidth:=form1.ListBox1.Canvas.TextWidth(form1.ListBox1.Items.Strings[i]);
            sendmessage(form1.ListBox1.Handle,LB_SETHORIZONTALEXTENT,maxwidth+2,0);
            end;
        end;
    end;