Results 1 to 27 of 27

Thread: [RESOLVED] Capture Scroll Event of RichTextBox

Threaded View

  1. #20
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: [RESOLVED] Capture Scroll Event of RichTextBox

    I am developing a WYSIWYG editor for this forum which uses a richtextbox so I am going to need something similar to your request. I think this method of inserting a separator line is much better no subclassing or tracking involved.
    VB Code:
    1. Public Sub Separator(rtb As RichTextBox, lColor As Long)
    2.     Dim iPos As Long
    3.     Dim strRTF As String
    4.     With rtb
    5.         iPos = .SelStart
    6.         'bracket selection
    7.         .SelText = Chr(&H80) & .SelText & Chr(&H81) ' \'80 \'81
    8.         strRTF = rtb.TextRTF
    9. 'add new color
    10.         lColor = AddColorToTable(strRTF, lColor)
    11. 'add highlighting
    12.          strRTF = Replace(strRTF, "\'80", "\par\cf" & CStr(lColor) & "\protect ---------------------- Separator ----------------------------- ")
    13.          strRTF = Replace(strRTF, "\'81", "\protect0\cf0\par  ")
    14.  
    15.          .TextRTF = strRTF
    16.         .SelStart = iPos
    17.        End With
    18. End Sub
    19.  
    20. Function AddColorToTable(strRTF As String, lColor As Long) As Integer
    21. Dim iPos As Long, jpos As Long
    22. Dim ctbl As String
    23. Dim tagColors
    24. Dim nColors As Integer
    25. Dim tagNew As String
    26. Dim i As Integer
    27. Dim iLen As Integer
    28. Dim split1 As String
    29. Dim split2 As String
    30.  
    31.     'make new color into tag
    32.     tagNew = "\red" & CStr(lColor And &HFF) & _
    33.         "\green" & CStr(Int(lColor / &H100) And &HFF) & _
    34.         "\blue" & CStr(Int(lColor / &H10000))
    35.    
    36.     'find colortable
    37.     iPos = InStr(strRTF, "{\colortbl")
    38.    
    39.     If iPos > 0 Then 'if table already exists
    40.         jpos = InStr(iPos, strRTF, ";}")
    41.         'color table
    42.         ctbl = Mid(strRTF, iPos + 12, jpos - iPos - 12)
    43.         'array of color tags
    44.         tagColors = Split(ctbl, ";")
    45.         nColors = UBound(tagColors) + 2
    46.         'see if our color already exists in table
    47.         For i = 0 To UBound(tagColors)
    48.             If tagColors(i) = tagNew Then
    49.                 AddColorToTable = i + 1
    50.                 Exit Function
    51.             End If
    52.         Next i
    53.        
    54.         split1 = Left(strRTF, jpos)
    55.         split2 = Mid(strRTF, jpos + 1)
    56.         strRTF = split1 & tagNew & ";" & split2
    57.         AddColorToTable = nColors
    58.    
    59.     Else
    60.         'color table doesn't exists, let's make one
    61.         iPos = InStr(strRTF, "{\fonttbl") 'beginning of font table
    62.         jpos = InStr(iPos, strRTF, ";}}") + 2 'end of font table
    63.         split1 = Left(strRTF, jpos)
    64.         split2 = Mid(strRTF, jpos + 1)
    65.         strRTF = split1 & "{\colortbl ;" & tagNew & ";}" & split2
    66.         AddColorToTable = 1
    67.     End If
    68. End Function
    I'll continue to improve this code, but wanted to get it to you incase you'd rather use it.
    I also have code for highlighting and super/sub-scripts too if you need that.

    Edit Fixed bug in addcolor function
    Last edited by moeur; Aug 3rd, 2005 at 06:45 PM.

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