读取Text指定行号内容
'添加 Text1 Text2 Command1
'Text1 Multiline属性要设为 True
'Text2 中输入你要读取第几行的内容
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 EM_GETLINE = &HC4
Const EM_LINEINDEX = &HBB
Const EM_LINELENGTH = &HC1
Dim a$, fname$
Private Sub Form_Load()
fname = "c:\axleop.txt"
Open fname For Input As #1
Text1.Text = ""
While Not EOF(1)
Line Input #1, a
Text1.Text = Text1.Text & a & vbCrLf
Wend
Close #1
End Sub
Private Sub Command1_Click()
If Val(Text2.Text) > 0 Then
MsgBox GetLine(Text1, Val(Text2.Text) - 1)
End If
End Sub
Function GetLine(txt As TextBox, ByVal Line As Integer) As String
Dim S As String, Length As Integer, pos As Long
GetLine = ""
pos = SendMessage(txt.hwnd, EM_LINEINDEX, Line, ByVal 0&)
Length = SendMessage(txt.hwnd, EM_LINELENGTH, pos, ByVal 0&)
S = String(Length, Chr(0))
RtlMoveMemory ByVal S, Length, 2
If SendMessage(Text1.hwnd, EM_GETLINE, Line, ByVal S) > 0 Then
GetLine = S
End If
End Function
参考资料:http://hi.baidu.com/cbm666/blog/item/9d224316e157c84b21a4e934.html