How to get real lines count of CEdit control

67 Views Asked by At

I'm trying to add verticall scrollbar to my CEdit-inherited control which should appear only if it's needed. The obvious startegy I'm using is to determine whether text height is larger than control's height. I've found few examples of such a task here and in SO post Any way to retrieve text height, in pixels, of text within a CEditBox?. In both examples the height of text is determined as linesCount * lineHeight. However I has a text that doesn't contain line breaks, for example:

CString text = L"Hello Hello Hello /*few more times*/ Hello Hello Hello";

In that case CEdit::GetLinesCount method always returns 1 despite it has an ES_MULTILINE style option (and horizontal scrollbar is off). So, how can I find whether I need vertical scrollbar or not?

1

There are 1 best solutions below

1
qloq On

With the hint of IInspectable I ended up using the DrawText method, however at least my variant is not precise - it gives bigger height than actually needed:

void ScrollEdit::CheckVerticalScrolling()
{
    CString text;
    GetWindowText(text);

    CClientDC dc { this };
    CRect     rect;
    GetClientRect(&rect);
    int r =
        DrawText(dc.GetSafeHdc(), text, text.GetLength(), &rect, DT_CALCRECT | DT_EDITCONTROL | DT_WORDBREAK);
    GetWindowRect(&rect);

    ShowVertScrollBar(r > rect.Height());
}