Thread: line number on a rich edit control

  1. #1
    Registered User
    Join Date
    Jan 2006

    line number on a 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....

    case WM_COMMAND:
    	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 -;  //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
 = 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???

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    You'll probably need to 'synchronize' your drawing with the controls drawing by subclassing the control and intercepting WM_PAINT.

    This method turned up after searching google groups...


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. edit control text buffer
    By scurvydog in forum Windows Programming
    Replies: 4
    Last Post: 12-11-2008, 09:13 AM
  2. sending something to a rich edit control without keyboard
    By RancidWannaRiot in forum Windows Programming
    Replies: 1
    Last Post: 11-21-2005, 07:53 AM
  3. newbie to rich edit, cant get it to work
    By hanhao in forum Windows Programming
    Replies: 1
    Last Post: 03-24-2004, 09:54 PM
  4. endless edit control
    By ZerOrDie in forum Windows Programming
    Replies: 3
    Last Post: 03-21-2003, 01:51 AM
  5. Rich edit control example Win API
    By Echidna in forum Windows Programming
    Replies: 1
    Last Post: 09-17-2001, 02:12 AM