Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

line numbering in rich edit control

line numbering in rich edit control

line numbering in rich edit control

Hi there,

I am trying to make line numbers on the left margin of my rich edit control. I have yet to find a solution to the particular problems I have with line numbering but I'm very close. So far I have created the rich edit control and set the left margin as follows...


SendMessage( hRichEdit, EM_SETMARGINS, EC_LEFTMARGIN, 70 );

Then is set the event mask for the rich edit control to trigger a WM_COMMAND/EN_UPDATE message whenever I update the rich edit control as follows....


    switch( HIWORD(wParam) ){
        case EN_UPDATE:
                   hRichEdit = FindWindowEx( hwnd, NULL, "RichEdit20A", NULL );
                   hdc = GetDC( hRichEdit );
                   makeLineNumber( hRichEdit, hdc );
                   ReleaseDC( hRichEdit, hdc );

Then I use this function to generate the line numbering....


void makeLineNumber( HWND hRichEdit, HDC hdc ){

     /*get height of rich edit so that the height of the rich edit control can be calculated*/

     RECT richEditRect;
     SendMessage( hRichEdit, EM_GETRECT, 0 , (LPARAM)&richEditRect );
     int iRectHeight = richEditRect.bottom - richEditRect.top;  //calculate height

     /*get height of font being used*/

         TEXTMETRIC tm;
         GetTextMetrics( hdc, &tm );

     /*use height of rich edit control  and font to get the maximum number of lines
     the edit control can hold, */

     int iMaxNumberOfLines = (iRectHeight/tm.tmHeight);

     /*get first visible line, return value is the zero-based index of the uppermost visible line, */

     int iFirstVisibleLine = SendMessage( hRichEdit, EM_GETFIRSTVISIBLELINE, 0, 0 );

     /*create integers to temporarily hold the line number/char to make the line number*/

     int iLineChar = 0;
     int iLineNumber = 0;

     /*loop cannot run more than the max number of lines in the edit control*/

        for( int c = 0; c <= iMaxNumberOfLines; c++ ){

          /*return value is the character index of the line specified in the wParam parameter,
          or it is –1 if the specified line number is greater than the number of lines in the
          edit control. */

                  iLineChar = SendMessage( hRichEdit, EM_LINEINDEX, (iFirstVisibleLine + c ), 0 );

          if( iLineChar == -1 )  //break loop if you've gone beyond the last line

          /*otherwise output line number in the left margin*/
               iLineNumber = SendMessage( hRichEdit, EM_LINEFROMCHAR, (WPARAM)iLineChar, 0 );
               std::stringstream strLineIndex;
                           strLineIndex << (iLineNumber + 1);

               POINTL pl;
               SendMessage( hRichEdit, EM_POSFROMCHAR, (WPARAM)&pl, (LPARAM)iLineChar );

               RECT tmpRect;
               tmpRect.right = 55;                      //right border is 55 (rich edit control left border is 70 so there is a gap of 15)
                       tmpRect.left = 0;              //left border is flush with edge of window
                       tmpRect.bottom = richEditRect.bottom;  //bottom is same as rich edit controls bottom
               tmpRect.top = pl.y;              //top is the y position of the characters in that line number

               DrawText( hdc, strLineIndex.str().c_str(), strlen( strLineIndex.str().c_str() ), &tmpRect, DT_RIGHT );


However, once I do this the line numbering shows up but when I scroll it I get a gap in the border at the top of the screen and I get a dragging effect from the numbers. I know I'm close to solving this but I can't figure this out ARRRRGGG!! Can anyone help me???

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close