我想用VB做一个简单的文本操作程序.
但是遇到以下关于TextBox的问题搞不定,向高手请教!***1,怎么通过代码修改或删除TextBox中指定的行N,或者增加一行?***2,我用API得到第N行的字符串,有时候在字符串后面有一个特殊符号"£",请问是怎么回事?
(我的代码是: SendMessage text1.hwnd, EM_GETLINE, N, ByVal s )
但是遇到以下关于TextBox的问题搞不定,向高手请教!***1,怎么通过代码修改或删除TextBox中指定的行N,或者增加一行?***2,我用API得到第N行的字符串,有时候在字符串后面有一个特殊符号"£",请问是怎么回事?
(我的代码是: SendMessage text1.hwnd, EM_GETLINE, N, ByVal s )
窗体
Option Explicit
Dim LineNo As Long, ColNo As Long, TmpStr As StringPrivate Sub Form_Load()
TxtTest.Text = "00000000" & vbCrLf & "111111111" & vbCrLf & "2222222222222"
End SubPrivate Sub txttest_KeyUp(KeyCode As Integer, Shift As Integer)
Call GetCaretPos(TxtTest.hwnd, LineNo, ColNo)
lblLine.Caption = LineNo
lblCol.Caption = ColNo
Call TB_GetLine(TxtTest.hwnd, lblLine.Caption-1, TmpStr)
Me.Caption = TmpStr
End SubPrivate Sub txttest_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
Call GetCaretPos(TxtTest.hwnd, LineNo, ColNo)
lblLine.Caption = LineNo
lblCol.Caption = ColNo
Call TB_GetLine(TxtTest.hwnd, lblLine.Caption-1, TmpStr)
Me.Caption = TmpStr
End Sub
模块
Option ExplicitConst EM_GETSEL = &HB0
Const EM_LINEFROMCHAR = &HC9
Const EM_LINEINDEX = &HBB
Public Const EM_GETLINE = &HC4
Public Const EM_LINELENGTH = &HC1
Private Declare Sub RtlMoveMemory Lib "KERNEL32" (lpvDest As Any, lpvSource As Any, ByVal cbCopy As Long)Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As LongPublic Sub GetCaretPos(ByVal hwnd5 As Long, LineNo As Long, ColNo As Long)
Dim i As Long, j As Long
Dim lParam As Long, wParam As Long
Dim k As Long
i = SendMessage(hwnd5, EM_GETSEL, wParam, lParam)
j = i / 2 ^ 16 '取得目前光标所在位置前有多少个Byte
LineNo = SendMessage(hwnd5, EM_LINEFROMCHAR, j, 0) '取得光标前面有多少行
LineNo = LineNo + 1
k = SendMessage(hwnd5, EM_LINEINDEX, -1, 0)
'取得目前光标所在行前面有多少个Byte
ColNo = j - k + 1
End SubSub 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 Longlc = 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 IfEnd Sub
dim str as string
dim A() as string
A=split(str,vbcrlf)
你现在就是在对数组操作了,就很简单
需要你注意的是,文本文件中的一行在textbox显示的不一定是一行(也就是说,textbox中每行结束的时候并不一定都有硬回车)
VB中对string array快速插入、删除某个元素的办法
http://www.cnblogs.com/rainstormmaster/archive/2006/01/09/313880.html
我用TextBox,是按照行的. 用API怎么实现我要的操作呢?
还有,得到第N行的时候,为什么有时候后面会出现一个"£"符号?
SDK API-drawn Unicode Textbox (不是VB自带的ANSI Textbox),VB6,SP6Private 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 CopyMemory Lib "KERNEL32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Private Const EM_GETLINE = &HC4
Private Const EM_LINEINDEX = &HBB
Private Const EM_LINELENGTH = &HC1Public Function GetLineText(ByVal handle As Long, ByVal Index As Long) As StringDim LineText() As Byte
Dim size As Long
Dim pos As Longpos = SendMessage(handle, EM_LINEINDEX, Index, 0)
size = SendMessage(handle, EM_LINELENGTH, pos, 0)
If size = 0 Then
GetLineText = ""
Else
ReDim LineText((size - 1) + 1)
CopyMemory LineText(0), size, 2
size = SendMessage(handle, EM_GETLINE, Index, LineText(0))
GetLineText = StrConv(LeftB(LineText, size), vbUnicode)
End IfEnd Function
谢谢 VBAdvisor(Sunlight) .我想修改或删除TextBox中指定的行N,有没有好的方法呢?
Private Sub DelLine(txtEditor As TextBoxEx, ByVal LineIndex As Long)
'Deletes the specified line from the textbox
Dim lngSelStart As Long 'used to save the caret position
Dim lngLineLen As Long 'the length of the line to delete
Dim lngCharPos As Long 'the index of the first character on the line
Dim LineCount As LongLineCount = SendMessage(txtEditor.hWnd,EM_GETLINECOUNT, 0&, 0&)If LineIndex >= LineCount Then
Exit Sub
End If
lngSelStart = txtEditor.SelStartIf LineIndex < LineCount Then
lngCharPos = SendMessage(txtEditor.hWnd,EM_LINEINDEX, LineIndex, 0&)
End IflngLineLen = SendMessage(txtEditor.hWnd, EM_LINELENGTH, lngCharPos, 0&)txtEditor.Text = Left$(txtEditor.Text, lngCharPos) & Mid$(txtEditor.Text, lngCharPos + lngLineLen + 1)txtEditor.SelStart = lngSelStartEnd Sub
谢谢大家!
问题解决了。