上一帖因為論壇掛點 不見哩..重發!
快速讀取Text內第X行文字內容
VB 內建指令的做法:
Private Sub Command1_Click()
Label1 = Split(Text1, vbCrLf)(0)
Label2 = Split(Text1, vbCrLf)(1)
Label3 = Split(Text1, vbCrLf)(2)
End Sub
透過 API 的做法:
<快速讀取 TextBox 第 N 行的資料>:(Text1之MultiLine屬性請設為True)
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)
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
Private Sub Command1_Click()
Dim S As String
Call TB_GetLine(Text1.hWnd, 5, S)
' 傳回值 S 即等於第 5 行的資料
' TextBox 的行次是從 0 起算。
MsgBox S
End Sub
|