第一种方法的思路是:监视列表框中的鼠标移动事件,当鼠标移动到列表框的条目上时,利用SendMessage函数的LB_ITEMFROMPOINT参数获得条目的索引号,然后再判断条目的长度是否比列表框的宽度长,如果大于列表框宽度则将此索引号的条目内容赋予列表框的.ToolTipText属性。下面是一个例子,将演示此具有智能的列表框。
在窗体中添加一个列表框List1,在总体声明部分声明API函数如下:
Option Explicit
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_ITEMFROMPOINT = &H1A9
编写列表框的鼠标移动事件代码如下:
Private Sub List1_MouseMove(Button As Integer,
Shift As Integer, _X As Single, Y As Single)
Dim lXPoint As Long
Dim lYPoint As Long
Dim lIndex As Long
If Button = 0 Then
lXPoint = CLng(X / Screen.TwipsPerPixelX)
lYPoint = CLng(Y / Screen.TwipsPerPixelY)
With List1
’获得当前条目的索引号
lIndex = SendMessage(.hwnd, LB_ITEMFROMPOINT, 0, _
ByVal ((lYPoint * 65536) + lXPoint))
If (lIndex >= 0) And (lIndex < = .ListCount) Then
’判断条目的长度是否大于列表框宽度
If TextWidth(.List(lIndex)) > .Width Then
.ToolTipText = .List(lIndex)
Else
.ToolTipText = ""
End If
End If
End With
End If
End Sub
在Form_Load事件中为列表框添加
几个条目供验证使用:
Private Sub Form_Load()
With List1
.AddItem "智能列表框Listbox"
.AddItem "这是一个长条目信息,将鼠标移到其上,
你就能看到完整信息"
End With
End Sub
第二种方法的思路和第一种是基本类似的,但直接使用字体的大小属性来判断,不需要使用API函数,看一下下面的代码就明白了。可以使用下面的代码来替换上面的列表框鼠标移动事件代码:
Private Sub List1_MouseMove
(Button As Integer, Shift As Integer, _
X As Single, Y As Single)
Dim Ypos As Integer, iOldFontSize As Integer
iOldFontSize = Me.Font.Size
Me.Font.Size = List1.Font.Size
Ypos = Y \ Me.TextHeight("Xyz") + List1.TopIndex
Me.Font.Size = iOldFontSize
If Ypos < List1.ListCount Then
List1.ToolTipText = List1.List(Ypos)
Else
List1.ToolTipText = ""
End If
End Sub
程序运行环境:中文VB 5.0专业版,中文WIN95。
【VB中处理长列表框项的两种方法】相关文章:
2025年外省高中学业水平合格性考试成绩转入江苏认定的通告公布05-07
2012宁夏高考报名方式05-07
Vb中控件的自动排列05-07
写未来的作文300字05-07
Our Mother Is the Same 我们的妈妈是一样的05-07
中国大地保险公司实习报告05-07
小学父爱的作文600字05-07
07年4月北师大“中国古代文学史(一)”串讲(5)05-07
汪涵讲述小时候成长经历:鸡毛掸子的故事05-07
2023年广东梅州中考成绩查询时间及查分入口[7月7日起]05-07
关于清明节的英语作文:清明节05-07
