|
-
Apr 21st, 2005, 05:04 PM
#1
Thread Starter
Addicted Member
[RESOLVED]Line Breaking...
I've recently wrote a routine to replace visual line breaks by real ones in a RichTextBox. It works like intended, but is extremely slow. Would someone know how could it be accelerated?
Here's the code:
VB Code:
Public Sub InsertLineBreaks(rtb As RichTextBox)
Dim xPos() As Long
Dim lastLine As Long, curLine As Long
Dim i As Long, l As Long
Do
rtb.Text = Replace$(rtb.Text, vbCrLf & vbCrLf, vbCrLf)
Loop While InStr(1, rtb.Text, vbCrLf & vbCrLf)
ReDim xPos(rtb.GetLineFromChar(Len(rtb.Text)) - 1)
For i = 0 To Len(rtb.Text)
curLine = rtb.GetLineFromChar(i)
If curLine <> lastLine Then
xPos(UBound(xPos)) = i + 1
l = l + 1
End If
lastLine = rtb.GetLineFromChar(i)
Next
rtb.Text = Replace$(rtb.Text, vbCrLf, " ")
If UBound(xPos) > 0 Then
For i = 0 To UBound(xPos)
rtb.SelStart = xPos(i) - (i * 2) + ((i - 1) * 2)
rtb.SelLength = 1
If rtb.SelText <> " " Then rtb.SelStart = rtb.SelStart - 1: rtb.SelLength = 1
rtb.SelText = Chr(201)
Next
rtb.Text = Replace$(rtb.Text, Chr(201), vbCrLf)
End If
End Sub
Last edited by Max_aka_NOBODY; Apr 21st, 2005 at 07:49 PM.
-
Apr 21st, 2005, 05:53 PM
#2
Re: Line Breaking...
I've been reading your code a couple of times now but I'm still still not sure what it does or why you need to loop through every character in the RTF box.
But I will make an educated guess from your description. If you would use the SendMessage API function with the EM_GETLINECOUNT message you would get the number of lines available regardless if it use hard line breaks or not. You could then use the EM_LINELENGTH message to get the length of the line. Now since you know the number of lines and you can get the length of each (zero based) line number the only thing you need to find is the character index of a specified line, for that use the EM_LINEINDEX message. Look at that message as the opposite of the GetLineFromChar method in the RTF box, the EM_LINEINDEX message is like a GetCharFromLine method, in other words it will return the index of the first character of the specified (zero based) line.
I've mentioned a couple of times above that the line index is zero based meaning the first line has the index 0 and the second line has the index 1 and so on. With the above messages you can treat a RTF box as a ListBox.
Let me know if you need any help using the SendMessage API function with the above messages.
Last edited by Joacim Andersson; Apr 21st, 2005 at 05:57 PM.
-
Apr 21st, 2005, 07:25 PM
#3
Thread Starter
Addicted Member
Re: Line Breaking...
Mucho Gracias! Works like a charm, and really fast. Here's the code:
VB Code:
'Declarations
Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" ( _
ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
ByRef lParam As Any) As Long
Private Const EM_GETLINECOUNT As Long = &HBA
Private Const EM_LINELENGTH As Long = &HC1
Private Const EM_LINEINDEX As Long = &HBB
Private Sub BreakLines(rtb As RichTextBox)
Dim i As Long
Dim xPos() As Long
'Remove padding
rtb.Text = Replace$(rtb.Text, vbCrLf, " ")
Do
rtb.Text = Replace$(rtb.Text, " ", " ")
Loop While InStr(1, rtb.Text, " ")
'Count Lines
ReDim xPos(SendMessage(rtb.hwnd, EM_GETLINECOUNT, 0&, 0&) - 1)
'Get Indices
For i = 0 To UBound(xPos)
xPos(i) = SendMessage(rtb.hwnd, EM_LINEINDEX, i, 0&)
Next
'Insert Hard Breaks
For i = 1 To UBound(xPos)
rtb.SelStart = xPos(i) - 1 + i
rtb.SelLength = 1
rtb.SelText = vbCrLf
Next
End Sub
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|