Search code examples
winapirichedit

TEXTMETRIC is giving wrong height when resizing text with mouse wheel


I'm calculating the number of lines in a rich edit control.

Currently I'm using next code

TEXTMETRIC tm; {
    HDC hdc = GetDC(hwndRichEdit);
    GetTextMetrics(hdc, &tm);
    ReleaseDC(hwndRichEdit, hdc);
}

RECT editRect;
GetClientRect(hwndRichEdit, &editRect);
long int countLines = (editRect.bottom - editRect.top) / (tm.tmHeight + tm.tmExternalLeading);

The code yields out the right number of lines until I start to change the size of the text via mouse wheel + ctr.

Is it possible to get the right text height even if the text is resized with mouse wheel?

N.B. I'm recalculating the number of lines with EN_UPDATE notification.


Solution

  • You can send an EM_GETZOOM message to the control to retrieve the current zoom ratio. Dividing the countLines value by the zoom ratio should yield the correct line count. Use the MulDiv API call to implement the division.