用VB快速读取TextBox第N行的数据
1、API 的声明:
Public Const EM_GETLINE = &HC4
Public Const EM_LINELENGTH = &HC1
Public 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)
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 起算。)
站长在去掉有关bArr2的定义后,并将Line=Strconv(bArr2,vbUnicode)改为Line=StrConv(bArr,vbUnicode)发现程序照常运行,似乎bArr2在本程序中是多余的。不知哪位大虾能告知本人bArr2在这里的作用。
Tags:
作者:佚名评论内容只代表网友观点,与本站立场无关!
评论摘要(共 0 条,得分 0 分,平均 0 分)
查看完整评论