首页 > 代码库 > 为TListBox添加水平滚动条

为TListBox添加水平滚动条

TListBox添加水平滚动条

 实例说明

TListBox组件是一个较为常用的列表组件,在默认情况下该组件是没有水平滚动条的,所以文字过长会显示不完全,在文字较短的情况下还可以,但是如果一行的文字很长的话也不行。本例为TListBox添加了一个水平的滚动条,运行本程序单击【添加滚动条】按钮在TListBox组件中就会增加一个水平滚动条,运行效果如图1所示。

 

1  TListBox添加水平滚动条

 思路与技术

Windows中大部分窗口都具有滚动条,如果窗口被建立时没有让它具有滚动条特性的话,滚动条就不会被显示出来。Windows提供的消息LB_SETHORIZONTALEXTEND可以设置窗体的滚动条。因为TListBox也属于一种窗体,所以通过使用该消息同样可以设置TListBox的水平滚动条。

TListBox组件发送该消息应使用SendMessage函数,当向TListBox组件发送LB_SETHORI ZONTALEXTEND消息时,SendMessage函数的参数wParam是水平滚动条的宽度。

  SendMessage(listbox1.Handle ,lb_sethorizontalextent,100,0);

 

{ Listbox messages }

const
  {$EXTERNALSYM LB_ADDSTRING}
  LB_ADDSTRING            = $0180;
  {$EXTERNALSYM LB_INSERTSTRING}
  LB_INSERTSTRING         = $0181;
  {$EXTERNALSYM LB_DELETESTRING}
  LB_DELETESTRING         = $0182;
  {$EXTERNALSYM LB_SELITEMRANGEEX}
  LB_SELITEMRANGEEX       = $0183;
  {$EXTERNALSYM LB_RESETCONTENT}
  LB_RESETCONTENT         = $0184;
  {$EXTERNALSYM LB_SETSEL}
  LB_SETSEL               = $0185;
  {$EXTERNALSYM LB_SETCURSEL}
  LB_SETCURSEL            = $0186;
  {$EXTERNALSYM LB_GETSEL}
  LB_GETSEL               = $0187;
  {$EXTERNALSYM LB_GETCURSEL}
  LB_GETCURSEL            = $0188;
  {$EXTERNALSYM LB_GETTEXT}
  LB_GETTEXT              = $0189;
  {$EXTERNALSYM LB_GETTEXTLEN}
  LB_GETTEXTLEN           = $018A;
  {$EXTERNALSYM LB_GETCOUNT}
  LB_GETCOUNT             = $018B;
  {$EXTERNALSYM LB_SELECTSTRING}
  LB_SELECTSTRING         = $018C;
  {$EXTERNALSYM LB_DIR}
  LB_DIR                  = $018D;
  {$EXTERNALSYM LB_GETTOPINDEX}
  LB_GETTOPINDEX          = $018E;
  {$EXTERNALSYM LB_FINDSTRING}
  LB_FINDSTRING           = $018F;
  {$EXTERNALSYM LB_GETSELCOUNT}
  LB_GETSELCOUNT          = $0190;
  {$EXTERNALSYM LB_GETSELITEMS}
  LB_GETSELITEMS          = $0191;
  {$EXTERNALSYM LB_SETTABSTOPS}
  LB_SETTABSTOPS          = $0192;
  {$EXTERNALSYM LB_GETHORIZONTALEXTENT}
  LB_GETHORIZONTALEXTENT  = $0193;
  {$EXTERNALSYM LB_SETHORIZONTALEXTENT}
  LB_SETHORIZONTALEXTENT  = $0194;
  {$EXTERNALSYM LB_SETCOLUMNWIDTH}
  LB_SETCOLUMNWIDTH       = $0195;
  {$EXTERNALSYM LB_ADDFILE}
  LB_ADDFILE              = $0196;
  {$EXTERNALSYM LB_SETTOPINDEX}
  LB_SETTOPINDEX          = $0197;
  {$EXTERNALSYM LB_GETITEMRECT}
  LB_GETITEMRECT          = $0198;
  {$EXTERNALSYM LB_GETITEMDATA}
  LB_GETITEMDATA          = $0199;
  {$EXTERNALSYM LB_SETITEMDATA}
  LB_SETITEMDATA          = $019A;
  {$EXTERNALSYM LB_SELITEMRANGE}
  LB_SELITEMRANGE         = $019B;
  {$EXTERNALSYM LB_SETANCHORINDEX}
  LB_SETANCHORINDEX       = $019C;
  {$EXTERNALSYM LB_GETANCHORINDEX}
  LB_GETANCHORINDEX       = $019D;
  {$EXTERNALSYM LB_SETCARETINDEX}
  LB_SETCARETINDEX        = $019E;
  {$EXTERNALSYM LB_GETCARETINDEX}
  LB_GETCARETINDEX        = $019F;
  {$EXTERNALSYM LB_SETITEMHEIGHT}
  LB_SETITEMHEIGHT        = $01A0;
  {$EXTERNALSYM LB_GETITEMHEIGHT}
  LB_GETITEMHEIGHT        = $01A1;
  {$EXTERNALSYM LB_FINDSTRINGEXACT}
  LB_FINDSTRINGEXACT      = $01A2;
  {$EXTERNALSYM LB_SETLOCALE}
  LB_SETLOCALE            = $01A5;
  {$EXTERNALSYM LB_GETLOCALE}
  LB_GETLOCALE            = $01A6;
  {$EXTERNALSYM LB_SETCOUNT}
  LB_SETCOUNT             = $01A7;
  {$EXTERNALSYM LB_INITSTORAGE}
  LB_INITSTORAGE          = $01A8;
  {$EXTERNALSYM LB_ITEMFROMPOINT}
  LB_ITEMFROMPOINT        = $01A9;
  {$EXTERNALSYM LB_MSGMAX}
  LB_MSGMAX               = 432;

 开发步骤

1)新建一个标准工程,创建一个新窗体,默认主窗体的Name属性为Form1

2)在窗体中添加TGroupBoxTListBoxTButton组件。

3)程序完整代码如下:

 

 

procedure TForm1.FormCreate(Sender: TObject);
begin
     ListBox1.Items.Add(‘aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa‘);
end;


procedure TForm1.Button1Click(Sender: TObject);
var
  i,ItemWith: integer;
begin
  ItemWith:= 0;
  for i := 0 to listbox1.Items.Count - 1 do
    if ItemWith < listbox1.Canvas.TextWidth(listbox1.Items[i])then
      ItemWith := listbox1.Canvas.Textwidth(listbox1.Items[i]);
  SendMessage(listbox1.Handle ,lb_sethorizontalextent,ItemWith,0);
end;


来自为知笔记(Wiz)