Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

line numbering in rich edit control

Status
Not open for further replies.

rakan

Programmer
Feb 16, 2008
1
US
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...

Code:

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

Code:

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

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



Code:

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
break;

/*otherwise output line number in the left margin*/
else{
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???
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top