当向列表中加入的列表项超出了列表的显示范围后,列表并不会出现横向滚动条让你可以通过滚动来浏览项目 的全部内容。利用LB_SETHORIZONTALEXTENT消息可以设置列表的横向滚动条以及滚动长度。下面是范例程序: Option ExplicitPrivate Type RECT Left As Long Top As Long Right As Long Bottom As Long End Type Private Declare Function DrawText Lib "user32" Alias "DrawTextA" _ (ByVal hdc As Long, _ ByVal lpStr As String, _ ByVal nCount As Long, _ lpRect As RECT, _ ByVal wFormat As Long) As Long Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _ (ByVal hwnd As Long, _ ByVal wMsg As Long, _ ByVal wParam As Long, _ lParam As Any) As LongConst LB_SETHORIZONTALEXTENT = &H194 Const DT_CALCRECT = &H400Public Function ListTextWidth(ByRef lstThis As ListBox) As Long Dim i As Long Dim tR As RECT Dim lW As Long Dim lWidth As Long Dim lHDC As Long With lstThis.Parent.Font .Name = lstThis.Font.Name .Size = lstThis.Font.Size .Bold = lstThis.Font.Bold .Italic = lstThis.Font.Italic End With
lHDC = lstThis.Parent.hdc
'便历所有的列表项以找到最长的项 For i = 0 To lstThis.ListCount - 1 DrawText lHDC, lstThis.List(i), -1, tR, DT_CALCRECT lW = tR.Right - tR.Left + 8 If (lW > lWidth) Then lWidth = lW End If Next i
'返回最长列表项的长度(像素) ListTextWidth = lWidth End FunctionPrivate Sub Form_Load() Dim astr As String Dim i Dim l As Long
l = List1.FontSize * 20 / Screen.TwipsPerPixelX For i = 1 To 10 astr = astr + "我们This is a very long item " + Str(i) Next i List1.AddItem astr + "aaa" '加入一个很厂的列表项 l = ListTextWidth(List1)
SendMessage List1.hwnd, LB_SETHORIZONTALEXTENT, l, 0 End Sub 首先在Form1中加入一个ListBox控件,然后再将上面的代码加入到Form1的代码窗口中。运行程序,可以 看到列表中出现了横向滚动条,而且滚动范围正好是列表项的长度。
的全部内容。利用LB_SETHORIZONTALEXTENT消息可以设置列表的横向滚动条以及滚动长度。下面是范例程序:
Option ExplicitPrivate Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Declare Function DrawText Lib "user32" Alias "DrawTextA" _
(ByVal hdc As Long, _
ByVal lpStr As String, _
ByVal nCount As Long, _
lpRect As RECT, _
ByVal wFormat As Long) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
lParam As Any) As LongConst LB_SETHORIZONTALEXTENT = &H194
Const DT_CALCRECT = &H400Public Function ListTextWidth(ByRef lstThis As ListBox) As Long
Dim i As Long
Dim tR As RECT
Dim lW As Long
Dim lWidth As Long
Dim lHDC As Long With lstThis.Parent.Font
.Name = lstThis.Font.Name
.Size = lstThis.Font.Size
.Bold = lstThis.Font.Bold
.Italic = lstThis.Font.Italic
End With
lHDC = lstThis.Parent.hdc
'便历所有的列表项以找到最长的项
For i = 0 To lstThis.ListCount - 1
DrawText lHDC, lstThis.List(i), -1, tR, DT_CALCRECT
lW = tR.Right - tR.Left + 8
If (lW > lWidth) Then
lWidth = lW
End If
Next i
'返回最长列表项的长度(像素)
ListTextWidth = lWidth
End FunctionPrivate Sub Form_Load()
Dim astr As String
Dim i
Dim l As Long
l = List1.FontSize * 20 / Screen.TwipsPerPixelX
For i = 1 To 10
astr = astr + "我们This is a very long item " + Str(i)
Next i
List1.AddItem astr + "aaa"
'加入一个很厂的列表项
l = ListTextWidth(List1)
SendMessage List1.hwnd, LB_SETHORIZONTALEXTENT, l, 0
End Sub
首先在Form1中加入一个ListBox控件,然后再将上面的代码加入到Form1的代码窗口中。运行程序,可以
看到列表中出现了横向滚动条,而且滚动范围正好是列表项的长度。
好久没玩VB了 看起来不顺眼不过谢谢了
如果楼主要做到这样,干脆另外
绑定一个TScrollBar好了!