Results 1 to 3 of 3

Thread: [RESOLVED]Line Breaking...

  1. #1

    Thread Starter
    Addicted Member Max_aka_NOBODY's Avatar
    Join Date
    Jul 2004
    Location
    Amman, Jordan
    Posts
    179

    Resolved [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:
    1. Public Sub InsertLineBreaks(rtb As RichTextBox)
    2.  
    3. Dim xPos() As Long
    4. Dim lastLine As Long, curLine As Long
    5. Dim i As Long, l As Long
    6.  
    7. Do
    8.    rtb.Text = Replace$(rtb.Text, vbCrLf & vbCrLf, vbCrLf)
    9. Loop While InStr(1, rtb.Text, vbCrLf & vbCrLf)
    10.  
    11. ReDim xPos(rtb.GetLineFromChar(Len(rtb.Text)) - 1)
    12.  
    13. For i = 0 To Len(rtb.Text)
    14.     curLine = rtb.GetLineFromChar(i)
    15.     If curLine <> lastLine Then
    16.         xPos(UBound(xPos)) = i + 1
    17.         l = l + 1
    18.     End If
    19.     lastLine = rtb.GetLineFromChar(i)
    20. Next
    21.  
    22. rtb.Text = Replace$(rtb.Text, vbCrLf, " ")
    23.  
    24. If UBound(xPos) > 0 Then
    25.     For i = 0 To UBound(xPos)
    26.         rtb.SelStart = xPos(i) - (i * 2) + ((i - 1) * 2)
    27.         rtb.SelLength = 1
    28.         If rtb.SelText <> " " Then rtb.SelStart = rtb.SelStart - 1: rtb.SelLength = 1
    29.         rtb.SelText = Chr(201)
    30.     Next
    31.     rtb.Text = Replace$(rtb.Text, Chr(201), vbCrLf)
    32. End If
    33.  
    34. End Sub
    Last edited by Max_aka_NOBODY; Apr 21st, 2005 at 07:49 PM.

  2. #2
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    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.

  3. #3

    Thread Starter
    Addicted Member Max_aka_NOBODY's Avatar
    Join Date
    Jul 2004
    Location
    Amman, Jordan
    Posts
    179

    Re: Line Breaking...

    Mucho Gracias! Works like a charm, and really fast. Here's the code:

    VB Code:
    1. 'Declarations
    2. Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" ( _
    3.      ByVal hwnd As Long, _
    4.      ByVal wMsg As Long, _
    5.      ByVal wParam As Long, _
    6.      ByRef lParam As Any) As Long
    7. Private Const EM_GETLINECOUNT As Long = &HBA
    8. Private Const EM_LINELENGTH As Long = &HC1
    9. Private Const EM_LINEINDEX As Long = &HBB
    10.  
    11. Private Sub BreakLines(rtb As RichTextBox)
    12.  
    13.     Dim i As Long
    14.     Dim xPos() As Long
    15.    
    16. 'Remove padding
    17.     rtb.Text = Replace$(rtb.Text, vbCrLf, " ")
    18.     Do
    19.         rtb.Text = Replace$(rtb.Text, "  ", " ")
    20.     Loop While InStr(1, rtb.Text, "  ")
    21.     'Count Lines
    22.     ReDim xPos(SendMessage(rtb.hwnd, EM_GETLINECOUNT, 0&, 0&) - 1)
    23.     'Get Indices
    24.     For i = 0 To UBound(xPos)
    25.         xPos(i) = SendMessage(rtb.hwnd, EM_LINEINDEX, i, 0&)
    26.     Next
    27.    
    28. 'Insert Hard Breaks
    29.     For i = 1 To UBound(xPos)
    30.         rtb.SelStart = xPos(i) - 1 + i
    31.         rtb.SelLength = 1
    32.         rtb.SelText = vbCrLf
    33.     Next
    34.  
    35. 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
  •  



Click Here to Expand Forum to Full Width