Announcement

Collapse
No announcement yet.

RichEdit - Line Numbers & Alternating Line Colors

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • RichEdit - Line Numbers & Alternating Line Colors

    The attached code shows a way to use FillRect and DrawText to create line numbers and alternating line colors in a RichEdit control - but it's not compatible with allowing the user to type in text of their own and maintain the line #'s/colors.

    Other than writing a custom control from scratch (or use something like the custom EDM32 control) is there a way to maintain the line #'s and line colors wihle the user edits - while continuing to use the standard RichEdit control?

    I've seen hints of being able to set a background image for a RichEdit control, but cannot find any details.

    I've also seen the idea of making the RichEdit background transparent and of having an image control (which contains the #'s/colors) under the RE. I've not tried anything like this yet.

    Here's what the control looks like before editing, with line numbers and line colors drawn in.



    And here's the basic code to generate the numbers/colors.

    Code:
    'Compilable Example:
    #Compile Exe
    #Dim All
    #Include "Win32API.inc"
    #Include "RichEdit.inc"
    #Include "CommCtrl.inc"
    Global hDlg As Dword, hRichEdit As Dword, hDC As Dword
    %ID_RichEdit = 500
    Function PBMain() As Long
       Local style&, buf$
       buf$ =  "Rats! This text will covered!"
       style& = %WS_Child Or %WS_Visible Or %ES_MultiLine Or %WS_VScroll Or %ES_AutoHScroll _
                 Or %WS_HScroll Or %ES_AutoVScroll Or %ES_WantReturn Or %ES_NoHideSel Or %WS_TabStop
       Dialog New Pixels, 0, "Test Code",300,300,400,400, %WS_OverlappedWindow To hDlg
       Control Add Button, hDlg, 100,"Push", 30,10,140,20
       LoadLibrary("riched32.dll") : InitCommonControls
       Control Add "RichEdit", hDlg, %ID_RichEdit, buf$,20,40,340,340, style&, %WS_Ex_ClientEdge
       Control Handle hDlg, %ID_RichEdit To hRichEdit
       hDC = GetDC(hRichEdit)
       SendMessage  hRichEdit, %EM_SETMARGINS, %EC_LEFTMARGIN, 70
       Dialog Show Modal hDlg Call DlgProc
    End Function
    CallBack Function DlgProc() As Long
       Select Case CB.Msg
          Case %WM_Paint
                DrawStuff
          Case %WM_Destroy
                ReleaseDC (hRichEdit, hDC)
          Case %WM_Command
             If CB.Ctl = 100 Then DrawStuff
       End Select
    End Function
    
    Sub DrawStuff
        Local txtZ As Asciiz*10, R As Rect, temp$, i As Long, j As Long
        For i = 1 To 300 Step 30
           R.nleft = 40   :  R.ntop = i
           R.nright = 350 :  R.nbottom =i+15
           FillRect hDC, R, %Color_BtnFace+1     'FillRect
    
           R.nleft = 40   :  R.ntop = i+15
           R.nright = 350 :  R.nbottom =i+30
           FillRect hDC, R, %Color_GrayText+1     'FillRect
        Next i
    
        For i = 1 To 300 Step 15
           Incr j
           temp$ = LTrim$(Str$(j))
           R.nleft = 5   :  R.ntop = i
           R.nright = 30 :  R.nbottom =i+20
           DrawText hDC, ByVal StrPTR(temp$), Len(temp$), R, %DT_Left
        Next i
    End Sub

  • #2
    The richedit control should have a complete set of messages to change font size, color, margins, etc.
    Michael Mattias
    Tal Systems (retired)
    Port Washington WI USA
    [email protected]
    http://www.talsystems.com

    Comment


    • #3
      Thanks MCM - yes, there's a wide variety of such messages.

      I used the margin message for the example. But anything I draw into the margin is overwritten when the user types in content.

      Also, all of the color options I've found either color the whole background, or just the background of text. There's no built-in mechanism that I can see, or find after searching, to color alternating lines.

      In searching the web, what I'm asking seems to be an often wanted but I-can't-find-the-solution problem. The only hint I get of possible solutions are related to adding a background bitmap or making the control transparent and having a bitmap under it.

      Comment


      • #4
        Also, I've seen some code that might apply, using CreatePatternBrush (from a bitmap) and then FillRect with that brush.

        I can create the line#/alternating color bitmap easily enough, so if there's a way to use that as a background, that solution might work - as long as the user editing of text takes places over the inserted background.

        I don't know squat about it yet, but I'm looking into it.

        Comment


        • #5
          It is actually not difficult to do. One of the samples that ships with the Phoenix Visual Designer
          shows how to do this. When I find the time I will post the sample.
          To implement this you will have to use the Text Object Model(TOM) and the WM_NCCALCSIZE message.
          Dominic Mitchell
          Phoenix Visual Designer
          http://www.phnxthunder.com

          Comment


          • #6
            Thanks Dominic,

            I'll be watching for the post!

            Comment


            • #7
              Gary, some years ago MCM posted code to alternate/change colors of individual lines in a listview (like green bar paper). Maybe that would help. Sorry I don't have the link to it at hand.

              ==========================================================
              Every war when it comes, or before it comes,
              is represented not as a war but as an act of self-defense
              against a homicidal maniac.
              George Orwell
              ==========================================================
              It's a pretty day. I hope you enjoy it.

              Gösta

              JWAM: (Quit Smoking): http://www.SwedesDock.com/smoking
              LDN - A Miracle Drug: http://www.SwedesDock.com/LDN/

              Comment


              • #8
                >(like green bar paper).

                That demo is NOT an editor, it is only a display.

                It's here if you are interested for other purposes...Simple Report Viewer Using Listview Control November 23, 2002; updated 5/30/05 to 'greenbar' file viewer
                Michael Mattias
                Tal Systems (retired)
                Port Washington WI USA
                [email protected]
                http://www.talsystems.com

                Comment


                • #9
                  Here's what the control looks like before editing, with line numbers and line colors drawn in
                  Interesting... but are you sure you need to start with a richedit control?

                  You could use a listview control for the line numbers on the left, with "some kind" of control immediately to its right for the actual text entry.

                  Not that I can see that any standard 'edit' control includes an 'owner draw' style, but maybe just thinking in terms of multiple controls instead of one control will give you some ideas.

                  You know what might work? A grid-type control.. those accept entry on designated lines so you'd always know where you are , "line number" - wise. EGrid32 (Elias' control) provides notifications for just about anything that happens, and I know it handles 'painting' the different lines in different colors. (Egrid's "virtual mode" would reduce overhead).

                  I 'assume' what you want is, if the user enters text exceeding the current (visible?) width of the editing area, you want to 'push' the rest of the text down the page and "flow it" into the following lines; that is, you are providing these different colors strictly as a reading aid to separate different lines of text.

                  If so, you can detect (EN_CHANGE or whatever EGN_xxx code is used with EGrid) when the user has type a character, at which time you can measure the current line to see if it fits, and if not 'do something' to 'flow' the contents into subsequent lines and position the caret at the next line.

                  However, I think this will take more than four lines of code.

                  MCM
                  Last edited by Michael Mattias; 22 Oct 2009, 09:27 AM.
                  Michael Mattias
                  Tal Systems (retired)
                  Port Washington WI USA
                  [email protected]
                  http://www.talsystems.com

                  Comment


                  • #10
                    One possible way to make this work with a RichEdit control could be to stream in some RTF that defines a table with alternate rows having different background colours (\clcbpat<N>) ?
                    Rgds, Dave

                    Comment


                    • #11
                      Hi Dave,

                      Yep - I think that would work just fine to create the alternating colors.

                      But it wouldn't be a "background" thing, and wouldn't I have to handle splitting the text into the tables cells myself?

                      I was hoping for something that wouldn't interfere with all of the text-processing code I already have in my application. But, since I have nothing that works now, I'll keep in mind that "beggars can't be choosey"

                      A background image was my preference, but I haven't seen anything to indicate that a RichEdit control supports a background image.

                      Hmm... back to your comment ... does a table support a background image? If so, and I made the table one big cell, perhaps there's something there?

                      It will be interesting to see what Dominic posts, and how compatible it will be with my existing code.

                      Comment


                      • #12
                        Hi Dominic,
                        Three years later ... and I'm still interested in how you were able to do line coloring!
                        It is actually not difficult to do. One of the samples that ships with the Phoenix Visual Designer shows how to do this. When I find the time I will post the sample.


                        I can cheat to get a solution - by padding each line with spaces that share the same background color. But that seems like such a kludge, and it adversely affects text-handling messages I might want to use!

                        So if your solution is still available, I'd still like to see it!

                        PS - George Bleck, please don't read this post!

                        Comment


                        • #13
                          Gary,

                          If it would help ( and be ok with Dom ), I've got Phoenix v1 & v2.
                          But a quick search thru the Samples didn't turn up anything with
                          RichEdit or rich edit. and nothing on Text Object Model.

                          Well, 1 sample did have a declare with richedit in the comment but it
                          was for PBSpell.DLL

                          Can you give a clue as to what I'm supposed to be searching for?
                          Last edited by Paul D. Elliott; 3 Nov 2011, 02:34 PM.

                          Comment


                          • #14
                            That (old) code is pretty bad!

                            No result/cancellation for the WM_PAINT.
                            Saving the DC continously is a serious issue not the mention the 'conflict' during the WM_PAINT which provides the correct DC.

                            If i am not mistaken every WM_PAINT will use beginpaint under water, so if you do you must cancel further processing.

                            Just to let anyone know not to use this technique.
                            hellobasic

                            Comment


                            • #15
                              So if your solution is still available, I'd still like to see it!
                              Wow! I forgot about this thread. I will post the low-level COM solution.
                              If I find the time, I will post a version that uses PB-COM syntax.

                              Can you give a clue as to what I'm supposed to be searching for?
                              If it is present in the Phoenix 2.0 version that was last released, it would be located here
                              \Samples\SDK\RichEdit\LineNum\LineNum.pbp
                              Dominic Mitchell
                              Phoenix Visual Designer
                              http://www.phnxthunder.com

                              Comment


                              • #16
                                What Edwin said.

                                Plus, isn't it as simple as sending a %EM_SETCHARFORMAT message? Presumably you have to set a selection first for the range whose background color you need to change.

                                Comment


                                • #17
                                  Hi Chris(H),
                                  No, it only sets the background for characters within the specified formatting range. The unfilled space to the right of a line remains colored with the default background color. Like this:

                                  Last edited by Gary Beene; 3 Nov 2011, 06:03 PM.

                                  Comment


                                  • #18
                                    OK. How about using bitblt to OR the Richedit control's DC with a bitmap with the colours you want?

                                    Comment


                                    • #19
                                      Hi Dominic,
                                      Thanks! I'll watch for your post.

                                      Hi Chris (H),
                                      Back up on post #4, I mentioned:
                                      .. I've seen some code that might apply, using CreatePatternBrush (from a bitmap) and then FillRect with that brush.
                                      I've not pursued that as yet. In general, you'd think that would work - if not a bit complicated because not all lines are completely within the client area of the RichEdit control.

                                      Bummer that the RichEdit control doesn't support the NM_CustomDraw message, such as the ListView supports. That message makes it dirt simple to set the color of individual lines in the ListView control.
                                      Last edited by Gary Beene; 3 Nov 2011, 06:46 PM.

                                      Comment


                                      • #20
                                        I can understand the drive and desire to get it to work Gary. I was faced with the same problem.

                                        As you probably saw in my other thread I wound up using an Embedded browser window which is SO much easier to utilize. No programming required to format, just a basic knowledge of HTML which gives you the added bonus of all the wonderful formatting capabilities like tables, bookmarks, as well as built in printing, etc.
                                        <b>George W. Bleck</b>
                                        <img src='http://www.blecktech.com/myemail.gif'>

                                        Comment

                                        Working...
                                        X