textbox 是以 vbcr+vblf 为分行符号, 如果我们要逐一读取 textbox 每一行,无非是寻找 vbcr+vblf 的所在位置, 然后取出每一行的字串, 不过这个方法真的不快,而且如果我们要读取第 n 行资料, 还是要从第 1、2、┅n-1 行逐一读起, 实在麻烦。
还好 windows api 提供有读取 textbox 第 n 行的功能, 细节如下:
1. api 的声明:
const em_getline = &hc4
const em_linelength = &hc1
const em_lineindex = &hbb
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 declare sub rtlmovememory lib "kernel32" (lpvdest as any, lpvsource as any, byval cbcopy as long)
注:如果以上的声明放在「一般模块」底下, 应在 const 之前加上 public 保留字, 并且将 private 保留字去掉。
2. 程序范例:
sub tb_getline(byval hwnd as long, byval whichline as long, line as string)
dim length as long, barr() as byte, barr2() as byte, lc as long
lc = sendmessage(hwnd, em_lineindex, whichline, byval 0&)
length = sendmessage(hwnd, em_linelength, lc, byval 0&)
if length > 0 then
redim barr(length + 1) as byte, barr2(length - 1) as byte
call rtlmovememory(barr(0), length, 2)
call sendmessage(hwnd, em_getline, whichline, barr(0))
call rtlmovememory(barr2(0), barr(0), length)
line = strconv(barr2, vbunicode)
else
line = ""
end if
end sub
' 假设要读取 text1 第 5 行的资料
dim s as string
call tb_getline( text1.hwnd, 5, s )
' 传回值 s 即等于到 5 行的资料
注:textbox 的行次是从 0 起算。
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 注册表 操作系统 服务器 应用服务器