4个回答
展开全部
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Sub Form_Load()
For i = 1 To 20
List1.AddItem String(100, "0") & i
Next
SendMessage List1.hwnd, &H194, 999, ByVal 0 '999是滚动条的宽度范围,单位为象素
End Sub
=============================================================
更优的代码
'计算出列表框内最长的项目有多少象素的宽度,这样可以使列表框的滚动条调整到一个合适的范围,因为列表框的内容很可能是会变动的,如果生硬的加一个很大的滚动条,同样会使你的程序显得很不专业,所以这一段是更优的代码!
Private 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 Long
Const LB_SETHORIZONTALEXTENT = &H194
Const DT_CALCRECT = &H400
Public 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
Next i
'返回最长列表项的长度(像素)
ListTextWidth = lWidth
End Function
'调用代码
Private Sub Command1_Click() '点击Command1会使列表框按当前内容中最长项目的宽度来设置滚动条的范围
'列表框内容有变化时,可以调用这行代码,随时改变滚动条的范围
SendMessage List1.hwnd, LB_SETHORIZONTALEXTENT, ListTextWidth(List1), 0
'写在这里是为了让你看清楚列表框滚动条的变化
End Sub
'测试数据
Private Sub Form_Load()
Command1.Caption = "点我"
For i = 1 To 100
'添加很长的项目给列表框
List1.AddItem String(i, "0")
Next
End Sub
Private Sub Form_Load()
For i = 1 To 20
List1.AddItem String(100, "0") & i
Next
SendMessage List1.hwnd, &H194, 999, ByVal 0 '999是滚动条的宽度范围,单位为象素
End Sub
=============================================================
更优的代码
'计算出列表框内最长的项目有多少象素的宽度,这样可以使列表框的滚动条调整到一个合适的范围,因为列表框的内容很可能是会变动的,如果生硬的加一个很大的滚动条,同样会使你的程序显得很不专业,所以这一段是更优的代码!
Private 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 Long
Const LB_SETHORIZONTALEXTENT = &H194
Const DT_CALCRECT = &H400
Public 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
Next i
'返回最长列表项的长度(像素)
ListTextWidth = lWidth
End Function
'调用代码
Private Sub Command1_Click() '点击Command1会使列表框按当前内容中最长项目的宽度来设置滚动条的范围
'列表框内容有变化时,可以调用这行代码,随时改变滚动条的范围
SendMessage List1.hwnd, LB_SETHORIZONTALEXTENT, ListTextWidth(List1), 0
'写在这里是为了让你看清楚列表框滚动条的变化
End Sub
'测试数据
Private Sub Form_Load()
Command1.Caption = "点我"
For i = 1 To 100
'添加很长的项目给列表框
List1.AddItem String(i, "0")
Next
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
什么叫让横向滚动条起作用?你的滚动条不能拖动么?把你遇到的问题说详细一点。我随便帮你查了个,你可以去看看这里面的能不能解决你的问题。不能的话把你的问题说详细点发上来。http://zhoumf1214.blog.163.com/blog/static/524194020106223014293/
追问
我的意思是比如 我把edit中的内容添加到了 listbox中。发现如果添加的内容如果非常多的话 一行显示不完 横向滚动条 不起作用
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
把listbox的属性中的Horizontal Scroll设置成True就可以了
追问
点上对勾不起作用 哥们
追答
listbox是需要自动创建列的,listbox的横向滚动条作用是你创建的所有列的宽度。很多时候我们拉动横向滚动条始终显示不全,需要反复拉才能看到完整的记录,其实你只需要双击一下你创建列和列之间的那条分隔符,listbox的列会更具内容自动调整大小,这个时候横向滚动条就能拖到最后了,不知道你是否是这个问题。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Horizontal Scroll设置成True
SendDlgItemMessage(IDC_LIST, LB_SETHORIZONTALEXTENT, 1000, 0);
SendDlgItemMessage(IDC_LIST, LB_SETHORIZONTALEXTENT, 1000, 0);
参考资料: http://www.yesky.com/164/1777164_1.shtml
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询