📄 20.txt
字号:
VB中list控件的功能扩充
中原油田计算中心 武正伦
List控件是VB中常用、方便而有效的控件,是开发者非常喜欢的控件之一,但开发者常常会遇到这样的麻烦:一方面由于列表筐列表项内容长短不一,有的可能很长;另一方面由于界面的限制又不能把List控件作的很大,结果造成list控件中列表项内容太长而无法完整地显示出来。
笔者在多年的开发过程中找到了两种简便的解决方法。
一种是利用list控件的tooltiptext属性,在List控件mousemove事件中编程动态地改变list控件的tooltiptext属性值从而全部显示鼠标所在项的全部内容。
另一种是在form中另加一个Label控件,把其caption属性设为false,利用list控件mousemove事件中编程,当鼠标移至list控件是则把鼠标所在列表项的内容赋给label控件的caption,label控件的visible属性设为true,这样就可通过label控件来显示鼠标所在项的全部内容,当鼠标离开list控件再把其visible属性设为false。这两种方法都必须解决一个问题,就是当鼠标移至List控件时从鼠标点阵位置算出鼠标所在list控件列表项位置。好在Win API为我们提供了一个API函数SendMessage可以解决这个问题,使用时作以下说明:
Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, _
ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
函数中hwnd为list控件的hwnd,而wmsg为LB_ITEMFORMPOINT,lParam设为"x坐标+y坐标*65535"。
下面详细介绍这两种方法,源程序已在VB5.0上调试通过。
方法一:利用利用list控件的tooltiptext属性
1、在Module1中作以下说明
Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, _
ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
2、在form1中加入以下代码:
Const LB_ITEMFROMPOINT = &H1A9
Private Sub Form_Load()
With List1
.AddItem "fdhvgkh;lsbkfgjlkglkmjl,k'j;l,';hj"
.AddItem "我是一个兵"
End With
End Sub
Private Sub List1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim lcx As Long
Dim lcy As Long
Dim lci As Long
If Button = 0 Then '若不按下鼠标键
lcx = CLng(X / Screen.TwipsPerPixelX)
lcy = CLng(Y / Screen.TwipsPerPixelY)
'获得当前鼠标位置
With List1
lci = SendMessage(.hwnd, LB_ITEMFROMPOINT, 0, ByVal ((lcy * 65536) + lcx))
'计算鼠标所在列表项
If (lci >= 0) And (lci <= .ListCount) Then
ToolTipText=".List(lci)"
Else
.ToolTipText
End If
End With
End If
'MsgBox CStr(lci)
End Sub
这种方法利用list控件本身的tooltiptext属性直接、简便。但是,显示时位置总是和鼠标所在的列表项错一行。方法2 可以解决这个问题。
方法2:利用label控件
1、在form1中加入label控件label1,autosize属性设为true,其visible属性设为False,字体属性设为和list1字体属性相同。
2、写入下列代码
Const LB_ITEMFROMPOINT = &H1A9
Private Sub Form_Load()
With List1
.AddItem "fdhvgkh;lsbkfgjlkglkmjl,k'j;l,';hj"
.AddItem "恢复稻谷单簧管列士大夫国家了发火点"
End With
End Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Label1.Visible = False
End Sub
Private Sub List1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim lcx As Long
Dim lcy As Long
Dim lci As Long
If Button = 0 Then '若不按下鼠标键
lcx = CLng(X / Screen.TwipsPerPixelX)
lcy = CLng(Y / Screen.TwipsPerPixelY)
'获得当前鼠标位置
With List1
lci = SendMessage(.hwnd, LB_ITEMFROMPOINT, 0, ByVal ((ly * 65536) + lx))
If (lci >= 0) And (lci <= .ListCount) Then
Label1.Left="List1.Left"
Label1.Top="List1.Top" + lci * (List1.Height / 3) ' Y
Label1.Caption="List1.List(lci)"
Label1.Visible="True"
Else
.ToolTipText
End If
End With
End If
'MsgBox CStr(lci)
End Sub
(其实在WIN32API中有一个消息可以直接给List控件增加一个横向的滚动条的方式来简单的解决该问题,这样也许可以成为第三种方式。)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -