下面用一个简单的实例演示这两个功能: 新建工程,在Form1上添加三个TextBox(名称分别为Text1、txtLineCount、TxtString,将Text1的Multi Line属性置为True)、三个标签和一个命令按钮。为工程添加一个模块Moudle1,在其中写如下声明(其中 SendMessage函数的声明可以从VB的“API浏览器”中复制): 消息常量名 消息值 wParam lParam 返回值 EM_GETLINECOUNT &HBA 未用 未用 行数 EM_GETLINE &HC4 要找的行号 存结果的字节串 结果字节串的字节数 Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long,lParam As Any) As Long Public Const EM_GETLINECOUNT=&HBA Public Const EM_GETLINE=&HC4 在Form1的代码模块中写如下代码: Private Sub Command1_Click() Dim str(256) As Byte str(1)=1 '最大允许存放256个字符 '获取总行数,结果显示在文本框txtLineCount中 txtlineCount=SendMessage(Text1.hwnd,EM_GETLINECOUNT,0,0) '获取第3行的数据放在str中,转换为字符串后显示在文本框txtString中 SendMessage Text1.hwnd,EM_GETLINE,2,str(0) txtString= StrConv(str,vbUnicode) End Sub
Private Sub Form_Load() Text3.Text = "1111" & vbCrLf & "2222" & vbCrLf & "3333" & vbCrLf & "4444" & vbCrLf & "5555" & vbCrLf & "6666" & vbCrLf & "7777" & vbCrLf End SubPrivate Sub Command4_Click() Dim str As String Dim strArr() As String strArr = Split(Text3.Text, vbCrLf) If UBound(strArr) > 0 Then Debug.Print strArr(UBound(strArr) - 1) End If End Sub
''使用GetLineText获取指定行数的内容。 Private Const EM_LINEINDEX = &HBB Private Const EM_GETLINECOUNT = &HBA Private Const EM_GETLINE = &HC4 Private Const EM_LINELENGTH = &HC1 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 Function SendMessageStringA Lib "USER32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long Private Declare Function SendMessageLong Lib "USER32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As LongPrivate Sub Command1_Click() MsgBox GetLineText(2) End SubFunction GetLineText(LineIndex As Long) As String Dim lc As Long
lc = GetLineLength(LineIndex) GetLineText = String$(lc + 2, 0) Mid$(GetLineText, 1, 1) = Chr(lc And &HFF) Mid$(GetLineText, 2, 1) = Chr(lc \ &H100) lc = SendMessageStringA(Text1.hWnd, EM_GETLINE, LineIndex, GetLineText) GetLineText = StripTerminator(GetLineText) 'Left(GetLineText, lc) End Function Function GetLineLength(LineIndex As Long) As Long Dim linechar& linechar = SendMessageLong(Text1.hWnd, EM_LINEINDEX, LineIndex, 0) GetLineLength = SendMessageLong(Text1.hWnd, EM_LINELENGTH, linechar, 0) End FunctionPrivate Function StripTerminator(ByVal strString As String) As String Dim intZeroPos As Integer intZeroPos = InStr(strString, Chr$(0)) If intZeroPos > 0 Then StripTerminator = Left$(strString, intZeroPos - 1) Else StripTerminator = strString End If End Function
num是要得到的行数,如下: Private Sub Command1_Click() num = 9tmp = Split(Text1.Text, vbCrLf) If UBound(tmp) > 0 Then MsgBox tmp(LBound(tmp) + num - 1) End If End Sub
新建工程,在Form1上添加三个TextBox(名称分别为Text1、txtLineCount、TxtString,将Text1的Multi
Line属性置为True)、三个标签和一个命令按钮。为工程添加一个模块Moudle1,在其中写如下声明(其中
SendMessage函数的声明可以从VB的“API浏览器”中复制): 消息常量名 消息值 wParam lParam 返回值
EM_GETLINECOUNT &HBA 未用 未用 行数
EM_GETLINE &HC4 要找的行号 存结果的字节串 结果字节串的字节数 Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long,lParam As Any) As Long
Public Const EM_GETLINECOUNT=&HBA
Public Const EM_GETLINE=&HC4
在Form1的代码模块中写如下代码:
Private Sub Command1_Click()
Dim str(256) As Byte
str(1)=1 '最大允许存放256个字符
'获取总行数,结果显示在文本框txtLineCount中
txtlineCount=SendMessage(Text1.hwnd,EM_GETLINECOUNT,0,0)
'获取第3行的数据放在str中,转换为字符串后显示在文本框txtString中
SendMessage Text1.hwnd,EM_GETLINE,2,str(0)
txtString= StrConv(str,vbUnicode)
End Sub
Text3.Text = "1111" & vbCrLf & "2222" & vbCrLf & "3333" & vbCrLf & "4444" & vbCrLf & "5555" & vbCrLf & "6666" & vbCrLf & "7777" & vbCrLf
End SubPrivate Sub Command4_Click()
Dim str As String
Dim strArr() As String
strArr = Split(Text3.Text, vbCrLf)
If UBound(strArr) > 0 Then
Debug.Print strArr(UBound(strArr) - 1)
End If
End Sub
Private Const EM_LINEINDEX = &HBB
Private Const EM_GETLINECOUNT = &HBA
Private Const EM_GETLINE = &HC4
Private Const EM_LINELENGTH = &HC1
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 Function SendMessageStringA Lib "USER32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long
Private Declare Function SendMessageLong Lib "USER32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As LongPrivate Sub Command1_Click()
MsgBox GetLineText(2)
End SubFunction GetLineText(LineIndex As Long) As String
Dim lc As Long
lc = GetLineLength(LineIndex)
GetLineText = String$(lc + 2, 0)
Mid$(GetLineText, 1, 1) = Chr(lc And &HFF)
Mid$(GetLineText, 2, 1) = Chr(lc \ &H100)
lc = SendMessageStringA(Text1.hWnd, EM_GETLINE, LineIndex, GetLineText)
GetLineText = StripTerminator(GetLineText) 'Left(GetLineText, lc)
End Function
Function GetLineLength(LineIndex As Long) As Long
Dim linechar&
linechar = SendMessageLong(Text1.hWnd, EM_LINEINDEX, LineIndex, 0)
GetLineLength = SendMessageLong(Text1.hWnd, EM_LINELENGTH, linechar, 0)
End FunctionPrivate Function StripTerminator(ByVal strString As String) As String
Dim intZeroPos As Integer intZeroPos = InStr(strString, Chr$(0))
If intZeroPos > 0 Then
StripTerminator = Left$(strString, intZeroPos - 1)
Else
StripTerminator = strString
End If
End Function
Private Sub Command1_Click()
num = 9tmp = Split(Text1.Text, vbCrLf)
If UBound(tmp) > 0 Then
MsgBox tmp(LBound(tmp) + num - 1)
End If
End Sub
这什么意思,有误吧?