VB ListBox 添加横向滚动条

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 Const LB_SETHORIZONTALEXTENT = &H194
--------------------------------------------------------------------------------------------------
Private Sub setListWidth() '如果列表框不够宽,则增加水平滚动条 Dim i As Integer Dim List_MaxL As Integer '获得选项内容的最大长度 For i = 0 To List1.ListCount - 1 ''让list_maxl中保存最长的一条字串 If Len(List1.List(i)) > List_MaxL Then List_MaxL = Len(List1.List(i)) + 2 End If Next i '判断是否内容显示不完全,如果是则添加水平滚动条 If Me.TextWidth("AA ") * List_MaxL > List1.Width Then SendMessage List1.hwnd, LB_SETHORIZONTALEXTENT, Me.TextWidth("a") * List_MaxL, ByVal 0& End If End Sub
--------------------------------------------------------------------------------------------------
Private Sub Form_Load()
      Dim i     As Integer
      '为ListBox控件添加选项
      For i = 0 To 100
            List1.AddItem ("这是,最据jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj:(第   " + CStr(i)) & "行) "
           'List1.AddItem   ( "(第   "   +   CStr(i))   &   "行) "
      Next i
     ' 设置窗体坐标尺度模式和字体大小
      Me.ScaleMode = vbPixels
      Me.FontSize = List1.FontSize
     设置列表框的水平滚动条
      Call setListWidth
End Sub


'方法二:-----------------------------------------------------------------------------------------------

'添加 ListBox 水平滚动条-------------------------------------------------
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
ListTextWidth = lWidth + 20 '返回最长列表项的长度(像素)
End Function

Private Sub Form_Load()
'设置 List 横向滚动条
dim l As Long
l = ListTextWidth(ltCol)
SendMessage ltCol.hwnd, LB_SETHORIZONTALEXTENT, l, 0

End Sub
 
   

 

 
 

 


 

 



转载于:https://www.cnblogs.com/wx881208/p/4180035.html

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值