Public Const EM_GETLINE = &HC4 Public Const EM_LINEINDEX = &HBB Public Const EM_LINELENGTH = &HC1 Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long Declare Sub RtlMoveMemory Lib "KERNEL32" (lpvDest As Any, lpvSource As Any, ByVal cbCopy As Long)'读取第21行字符 Private Sub Command1_Click() Dim S As String, Length As Integer, pos As Long
吧richtextbox 改为textbox就行了 'Adding line numbers to the front of text in a rich text box 'Example by HeSaidJoe 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 SendMessageStr Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As String) As Long Private Const EM_GETLINE = &HC4 Private Const EM_GETLINECOUNT = &HBA Private Const EM_LINEINDEX = &HBB Private Const EM_LINELENGTH = &HC1 Private Sub Command1_Click() Dim lngCount As Long Dim lngLineIndex As Long Dim lngLength As Long Dim strBuffer As String Dim strRichText As String Dim i As Integer 'Get Line count lngCount = SendMessage(RichTextBox1.hwnd, EM_GETLINECOUNT, 0, 0) With RichTextBox1 For i = 0 To lngCount - 1 'Get line index lngLineIndex = SendMessage(.hwnd, EM_LINEINDEX, i, 0) 'get line length lngLength = SendMessage(.hwnd, EM_LINELENGTH, lngLineIndex, 0) 'resize buffer strBuffer = Space(lngLength) 'get line text Call SendMessageStr(.hwnd, EM_GETLINE, i, ByVal strBuffer) 'Number each line strRichText = strRichText & CStr(i + 1) & " " & strBuffer & vbCrLf Next 'rewrite numbered text in RichTextBox .Text = strRichText End With End Sub Private Sub Form_Load() With RichTextBox1 .Text = "line one" & vbCrLf .Text = .Text & "line two" & vbCrLf .Text = .Text & "line three" End With End Sub
Public Const EM_LINEINDEX = &HBB
Public Const EM_LINELENGTH = &HC1
Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Declare Sub RtlMoveMemory Lib "KERNEL32" (lpvDest As Any, lpvSource As Any, ByVal cbCopy As Long)'读取第21行字符
Private Sub Command1_Click()
Dim S As String, Length As Integer, pos As Long
pos = SendMessage(Text1.hwnd, EM_LINEINDEX, 21, ByVal 0&)
Length = SendMessage(Text1.hwnd, EM_LINELENGTH, pos, ByVal 0&)
S = String(Length, Chr(0))
RtlMoveMemory ByVal S, Length, 2
SendMessage Text1.hwnd, EM_GETLINE, 21, ByVal S
MsgBox S, , "第21行"
End Sub
'Adding line numbers to the front of text in a rich text box
'Example by HeSaidJoe
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 SendMessageStr Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As String) As Long
Private Const EM_GETLINE = &HC4
Private Const EM_GETLINECOUNT = &HBA
Private Const EM_LINEINDEX = &HBB
Private Const EM_LINELENGTH = &HC1
Private Sub Command1_Click()
Dim lngCount As Long
Dim lngLineIndex As Long
Dim lngLength As Long
Dim strBuffer As String
Dim strRichText As String
Dim i As Integer
'Get Line count
lngCount = SendMessage(RichTextBox1.hwnd, EM_GETLINECOUNT, 0, 0)
With RichTextBox1
For i = 0 To lngCount - 1
'Get line index
lngLineIndex = SendMessage(.hwnd, EM_LINEINDEX, i, 0)
'get line length
lngLength = SendMessage(.hwnd, EM_LINELENGTH, lngLineIndex, 0)
'resize buffer
strBuffer = Space(lngLength)
'get line text
Call SendMessageStr(.hwnd, EM_GETLINE, i, ByVal strBuffer)
'Number each line
strRichText = strRichText & CStr(i + 1) & " " & strBuffer & vbCrLf
Next
'rewrite numbered text in RichTextBox
.Text = strRichText
End With
End Sub
Private Sub Form_Load()
With RichTextBox1
.Text = "line one" & vbCrLf
.Text = .Text & "line two" & vbCrLf
.Text = .Text & "line three"
End With
End Sub