在使用列表框(listbox)中经常碰到的问题是, 用户使用时在其中添加的条目(item)长短不一,由于界面所限,只能将列表框调整到能满足显示大部分条目的宽度。 如果其中有比较长的条目则不能完全显示,而listbox没有水平滚动条,这给用户的使用造成一些不便,可以通过为列表框加上一个水平滚动条的方法来解决,但这需要使用api函数,比较麻烦。使用vb5.0的用户可能发现一个有趣的现象:在联机手册的使用中,如果条目过长,当鼠标指针移到其上时,将自动弹出包含完整信息的提示框,类似于流行的按钮提示信息。笔者经过分析,找到了实现这个功能的两种方法。
第一种方法的思路是:监视列表框中的鼠标移动事件,当鼠标移动到列表框的条目上时,利用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。
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器