简单问题:
  怎么给 ListBox 加水平滚动条?

解决方案 »

  1.   

    在你的Form的OnCreate事件中添加以下代码: 
    procedure TForm1.FormCreate(Sender: TObject); 
    var 
    i, MaxWidth: integer; 
    begin 
    MaxWidth := 0; 
    for i := 0 to ListBox1.Items.Count - 1 do 
    if MaxWidth < ListBox1.Canvas.TextWidth(ListBox1.Items.Strings[i]) then 
    MaxWidth := ListBox1.Canvas.TextWidth(ListBox1.Items.Strings[i]); SendMessage(ListBox1.Handle, LB_SETHORIZONTALEXTENT, MaxWidth+2, 0); 
    end; 
      

  2.   

    在你的Form的OnCreate事件中添加以下代码: 
    procedure TForm1.FormCreate(Sender: TObject); 
    var 
    i, MaxWidth: integer; 
    begin 
    MaxWidth := 0; 
    for i := 0 to ListBox1.Items.Count - 1 do 
    if MaxWidth < ListBox1.Canvas.TextWidth(ListBox1.Items.Strings[i]) then 
    MaxWidth := ListBox1.Canvas.TextWidth(ListBox1.Items.Strings[i]); SendMessage(ListBox1.Handle, LB_SETHORIZONTALEXTENT, MaxWidth+2, 0); 
    end; 
      

  3.   

    Delphi的TListBox组件会自动添加一个垂直滚动条,即当列表框的高度容纳不下所有的列表条目时,垂直滚动条就自动显示。但是,当条目的宽度大于列表框的宽度时,水平滚动条不会自动显示。当然, 可以在列表框中加如水平滚动条,方法是在窗体的OnCreate事件处理程序中加入如下代码:
    procedure TForm1.FormCreate(Sender: TObject);
    var
      i, MaxWidth: integer;
    begin
      MaxWidth := 0;
      for i := 0 to ListBox1.Items.Count - 1 do
      if MaxWidth < ListBox1.Canvas.TextWidth(ListBox1.Items.Strings[i]) then
        MaxWidth := ListBox1.Canvas.TextWidth(ListBox1.Items.Strings[i]);
      SendMessage(ListBox1.Handle, LB_SETHORIZONTALEXTENT, MaxWidth+2, 0);
    end;
    这段代码先查找列表框中最长的条目的宽度(以象素点表示),然后, 用LB_SETHORIZONTALEXTENT消息来设置列表框的水平滚动条的宽度(以象素点表示),外加两个额外的象素。
      

  4.   

    谢谢hch_45(んこん) ,问题解决了!
    接分吧!