//请参考下面的DELPHI程序,把它改成VB格式即可
procedure TForm1.Button1Click(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[i]) then
MaxWidth := ListBox1.Canvas.TextWidth(ListBox1.Items[i]);
SendMessage(ListBox1.Handle, LB_SETHORIZONTALEXTENT, MaxWidth+2, 0);
end;
否定我的第二个回答,因此列表框控件缺省的创建风格里含有对水平滚动条的支持,因此只需要发消息即可:
先建立一个新模块,然后使用API文本查看器拷贝一个关于Sendmessage的声明,和一个LB_SETHORIZONTALEXTENT常量的声明,粘贴到模块中,
在表单的ONLOAD事件里,写以下代码:
dim i as long
sendmessage(list1.hwnd,LB_SETHORIZONTALEXTENT,list1.width+1,0)
要显式说明设置的滚动条宽度比列表框宽,水平滚动条会显示出来,否则水平滚动条会消失。
Also, to respond to the LB_SETHORIZONTALEXTENT message, the list box must have been defined with the WS_HSCROLL style. Then if the width of the list box is smaller than the value of wParam, the horizontal scroll bar horizontally scrolls items in the list box. If the width of the list box is equal to or greater than this value, the horizontal scroll bar is hidden.