Results 1 to 27 of 27

Thread: [RESOLVED] Capture Scroll Event of RichTextBox

  1. #1

    Thread Starter
    eltiT resU motsuC Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Resolved [RESOLVED] Capture Scroll Event of RichTextBox

    I did a search and found very little..

    I am trying to draw lines to seperate sections in a RichTextBox
    except the lines do not stay after scrolling off the screen.

    So should I capture the scroll event and redraw or is there a better way?

    if not.. how do I capture the scroll event?

    thanks!
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  2. #2
    Lively Member
    Join Date
    Jan 2002
    Location
    Posts
    114

    Re: Capture Scroll Event of RichTextBox

    Quote Originally Posted by [LGS]Static
    I did a search and found very little..

    I am trying to draw lines to seperate sections in a RichTextBox
    except the lines do not stay after scrolling off the screen.

    So should I capture the scroll event and redraw or is there a better way?

    if not.. how do I capture the scroll event?

    thanks!

    check for the previos scroll value and compare with the new value - if there is any change in these two values then the scroll occured in the rich text box... hope this will help u

  3. #3

    Thread Starter
    eltiT resU motsuC Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Capture Scroll Event of RichTextBox

    ok.. so how do I capture the scroll?
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  4. #4

    Thread Starter
    eltiT resU motsuC Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Capture Scroll Event of RichTextBox

    anyone?
    Bueller?
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  5. #5
    Lively Member
    Join Date
    Jan 2002
    Location
    Posts
    114

    Re: Capture Scroll Event of RichTextBox

    seems not possible ...

  6. #6
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Capture Scroll Event of RichTextBox

    Maybe inser space(80) and make it underlined?

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

    Re: Capture Scroll Event of RichTextBox

    how do I capture the scroll event?
    Didn't we already discuss this?
    You can generate your own scroll event
    send a EM_SETEVENTMASK message to your rtb to set the mask for EN_VSCROLL notification.
    Then subclass your main window and trap WM_COMMAND messages
    when EN_VSCROLL comes up the user has scrolled
    If you just redraw the lines during scrolling there shouldn't be too much flicker, if there is you can do a double buffer.
    http://msdn.microsoft.com/library/d...eteventmask.asp

  8. #8

    Thread Starter
    eltiT resU motsuC Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Capture Scroll Event of RichTextBox

    moeur, yes... but I cant find any good code examples! I Need an example to be able to change / learn how to use EM_SETEVENTMASK etc..

    CANT FIND ANY!!

    dglienna.. I dont want to use an underline because then it will save into the file..(This is for the CodeBank app.. the underline will become part of the code snippet)
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

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

    Re: Capture Scroll Event of RichTextBox

    I can work up an example for you. But first let's discuss using an underline further.

    What are the reasons you don't want to use it?

    If you're only worried about it being saved to the rtf file then you can remove it before saving.

    and why are you saving as rtf and not plain text?

  10. #10

    Thread Starter
    eltiT resU motsuC Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Capture Scroll Event of RichTextBox

    not saving as rtf.. just the text..
    I would rather not have to remove before save...
    and I dont want the user to be able to highlight / edit / change the line...
    if they do.. then it would be hard to remove it from the text before save...
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

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

    Re: Capture Scroll Event of RichTextBox

    If you're just saving as text, then the underline won't get saved so you don't have to worry about that.

  12. #12

    Thread Starter
    eltiT resU motsuC Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Capture Scroll Event of RichTextBox

    but the user will still be able to edit it...
    no good

    Just like in VB IDE.. Lines
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

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

    Re: Capture Scroll Event of RichTextBox

    Are you worried that if the user edits a line that has an underline then the new text won't be underlined? If so, then you could handle that pretty easily by putting some code in the selchange event to underline new text when the cursor is in the special underlined region.

  14. #14

    Thread Starter
    eltiT resU motsuC Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Capture Scroll Event of RichTextBox

    no , I just dont want the user to be able to interact with the lines at all.. as iff they dont exist.. just like in VB IDE. there are line seperating the subs etc.. but you cant touch them
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

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

    Re: Capture Scroll Event of RichTextBox

    OK, I understand. I'll write something up for you.

  16. #16

    Thread Starter
    eltiT resU motsuC Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Capture Scroll Event of RichTextBox

    Cool! Thanks moeur!
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

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

    Re: Capture Scroll Event of RichTextBox

    OK, here is something. For some stupid reason, the EN_VSCROLL notification is not sent when the user drags the thumb. So I had to also subclass the form to catch those messages.
    In a module define these public routines
    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" ( _
    4.     ByVal hWnd As Long, _
    5.     ByVal wMsg As Long, _
    6.     ByVal wParam As Long, _
    7.     lParam As Any _
    8. ) As Long
    9.  
    10. Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" ( _
    11.     ByVal lpPrevWndFunc As Long, _
    12.     ByVal hWnd As Long, _
    13.     ByVal Msg As Long, _
    14.     ByVal wParam As Long, _
    15.     ByVal lParam As Long _
    16. ) As Long
    17.  
    18. Private Type POINTAPI
    19.         x As Long
    20.         y As Long
    21. End Type
    22.  
    23. Private Const WM_USER = &H400
    24. Private Const EN_HSCROLL = &H601
    25. Private Const EN_VSCROLL = &H602
    26. Private Const EN_CHANGE = &H300
    27. Private Const EN_UPDATE = &H400
    28. Private Const EM_SETEVENTMASK = (WM_USER + 69)
    29. Private Const ENM_SCROLL = &H4
    30. Private Const EM_GETSCROLLPOS = (WM_USER + 221)
    31. Private Const EM_SETSCROLLPOS = (WM_USER + 222)
    32. Private Const WM_COMMAND = &H111
    33. Private Const WM_VSCROLL = &H115
    34. Private Const WM_HSCROLL = &H114
    35.  
    36. 'Must be declared in BAS file not FRM file.
    37. Public frmSubClass As CSubclass
    38. Public RTBsubClass As CSubclass
    39.  
    40. Public Function NewFrmWndProc(ByVal hWnd As Long, ByVal uMsg As Long, _
    41.     ByVal wParam As Long, ByVal lParam As Long) As Long
    42. 'This is where the messages for our Form will arrive
    43. Dim ScrollPos As POINTAPI
    44.         If uMsg = WM_COMMAND Then
    45.             Select Case Int(wParam \ &H10000)
    46.             Case EN_VSCROLL
    47.                 Debug.Print Timer, "EN_VSCROLL",
    48.                 SendMessage frmRTB.RTB.hWnd, EM_GETSCROLLPOS, 0, ScrollPos
    49.                 Debug.Print ScrollPos.y
    50.             Case Else
    51.             End Select
    52.         End If
    53.     NewFrmWndProc = CallWindowProc(frmSubClass.OrigWndProc, hWnd, uMsg, wParam, lParam)
    54. End Function
    55.  
    56. Public Function NewRTBWndProc(ByVal hWnd As Long, ByVal uMsg As Long, _
    57.     ByVal wParam As Long, ByVal lParam As Long) As Long
    58. 'This is where the messages for our RichTExtBox will arrive
    59. Dim ScrollPos As POINTAPI
    60.         If uMsg = WM_VSCROLL Then
    61.             Debug.Print Timer, "WM_VSCROLL",
    62.             SendMessage hWnd, EM_GETSCROLLPOS, 0, ScrollPos
    63.             Debug.Print ScrollPos.y
    64.         End If
    65.     NewRTBWndProc = CallWindowProc(RTBsubClass.OrigWndProc, hWnd, uMsg, wParam, lParam)
    66. End Function
    67.  
    68. Public Sub SetCaptureScroll(hWndFRM As Long, hWndRTB As Long)
    69.     'subclass our form to get scrollbar events
    70.     Set frmSubClass = New CSubclass
    71.     frmSubClass.EnableSubclass hWndFRM, AddressOf NewFrmWndProc
    72.     'subclass the RTB to get scrollbar events
    73.     Set RTBsubClass = New CSubclass
    74.     RTBsubClass.EnableSubclass hWndRTB, AddressOf NewRTBWndProc
    75.     'set notify mask for RTB so we receive scroll messages
    76.     SendMessage hWndRTB, EM_SETEVENTMASK, 0, ByVal ENM_SCROLL
    77. End Sub

    To start it off just do this
    VB Code:
    1. Private Sub Form_Load()
    2.     'pass form hWnd and RichTextBox hWnd
    3.     SetCaptureScroll Me.hWnd, RTB.hWnd
    4. End Sub

    I've zipped it all up for you, including a class to do the subclassing.

    What you'll get from the new window procedures is a vertical position in pixels that you can use to keep track of your lines.
    Attached Files Attached Files

  18. #18

    Thread Starter
    eltiT resU motsuC Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Capture Scroll Event of RichTextBox

    To bad I cant Swear on here... Becase I Would Say HOLY S***
    IT WORKS PERFECTLY!!!

    MANY MANY MANY THANX!!


    The only thing is occasionally the lines disappear on resize.. but I dont care!
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  19. #19
    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 think I might have an even better solution. Hang on for a bit and I'll get back.

  20. #20

    Thread Starter
    eltiT resU motsuC Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: [RESOLVED] Capture Scroll Event of RichTextBox

    woot!
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  21. #21
    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.

  22. #22
    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

    Here is a better looking separator routine.
    The beauty of this routine is that it inserts a seperator that looks nice, scrolls properly and the user cannot access. The one problem is that you will have to remove it before saving, but that is easily handled with a call to replace.

    Use the above (fixed) colortable function and this new separator routine
    VB Code:
    1. Public Sub Separator(rtb As RichTextBox, lColor1 As Long, _
    2.     lColor2 As Long, halfWidth As Integer, txtTitle As String)
    3.     Dim iPos As Long
    4.     Dim strRTF As String
    5.     Dim txtTemp As String
    6.     Dim icolor1 As Integer
    7.     Dim icolor2 As Integer
    8.     Dim iUP As Integer
    9.     With rtb
    10.         iPos = .SelStart
    11.         iUP = Int(.SelFontSize) - 1
    12.         'bracket selection
    13.         .SelText = Chr(&H80) & .SelText & Chr(&H81) ' \'80 \'81
    14.         strRTF = rtb.TextRTF
    15. 'add new colors
    16.         icolor1 = AddColorToTable(strRTF, lColor1)
    17.         icolor2 = AddColorToTable(strRTF, lColor2)
    18. 'add separator
    19.  
    20.         txtTemp = "\par" & _
    21.          "\cf" & CStr(icolor1) & "\protect\up" & CStr(iUP) & " " & String(halfWidth, "_") & "\up0" & _
    22.          "\cf" & CStr(icolor2) & " " & txtTitle & _
    23.          "\cf" & CStr(icolor1) & "\up" & CStr(iUP) & " " & String(halfWidth, "_") & "\up0\cf0"
    24.  
    25.          strRTF = Replace(strRTF, "\'80", txtTemp)
    26.          strRTF = Replace(strRTF, "\'81", "\protect0\cf0\par  ")
    27.  
    28.          .TextRTF = strRTF
    29.         .SelStart = iPos
    30.        End With
    31. End Sub
    Just call it like this
    VB Code:
    1. Separator rtb, vbRed, vbBlue, 20, " Visual Basic Code "
    Last edited by moeur; Aug 3rd, 2005 at 07:02 PM.

  23. #23

    Thread Starter
    eltiT resU motsuC Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: [RESOLVED] Capture Scroll Event of RichTextBox

    ok, the lines look great..
    but you still can interact with them. put you cursor in the middle of one of them and hit enter... splits them

    I will stick with the original way.. just need to tweak it here and there.

    thanks again for all your help!
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  24. #24
    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 don't see that behavior in my example,
    Anyway you've got something that works.
    Your program looks great. I'll start using it when it's finished.

  25. #25

    Thread Starter
    eltiT resU motsuC Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: [RESOLVED] Capture Scroll Event of RichTextBox

    ok, your going to be mad.. but I've found after all that. The code for putting lines in and keep them, seems to be interfering with other code!!

    Not sure why.. example: RichTextBox_Change event no longer gets fired...
    my auto indent code is adding extra #'s to the selstart location so it moves it when done.. etc.

    I removed all the code/ module/ cls and it works fine..
    I was concentrating so much on the lines.. I never tried other code until the lines worked right....I may go back to your other code.. need to play more
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  26. #26
    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 hate it when that happens

  27. #27

    Thread Starter
    eltiT resU motsuC Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: [RESOLVED] Capture Scroll Event of RichTextBox

    me too! thanks again for your help
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

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