Count logical liines in a text control

Igor Korot 6 Reputation points
2024-03-12T05:03:09.4833333+00:00

Hi, ALL,

There is a EM_GETLINECOUNT, but it will count the number of text lines.

I'm looking for to count the displayed lines (or wrapped lines) in a text control.

Either using WinAPI or C++.

Thank you.

C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,765 questions
{count} votes

3 answers

Sort by: Most helpful
  1. RLWA32 45,701 Reputation points
    2024-03-12T12:28:32.5533333+00:00

    An edit control may not necessarily display integral lines of text. So its possible that the following code could return a visible line count that includes a partially displayed line.

    Used in a Win32 dialog -

    void CVisibleCountApp::OnCount()
    {
        RECT rc{};
        int lastLine{};
        BOOL bVisible{ TRUE };
    
        Edit_GetRect(m_hEdit, &rc);
        int firstLine = Edit_GetFirstVisibleLine(m_hEdit);
    
        for (int i = firstLine; bVisible; i++)
        {
            int charIndex = Edit_LineIndex(m_hEdit, i);
            auto coord = SendMessage(m_hEdit, EM_POSFROMCHAR, charIndex, 0);
            POINT pt{ LOWORD(coord), HIWORD(coord) };
            bVisible = PtInRect(&rc, pt);
            if (bVisible)
                lastLine = i;
        }
    
        TCHAR szMsg[128]{};
        _stprintf_s(szMsg, _T("First line index: %d, Last line index: %d, No. Lines %d"), firstLine, lastLine, lastLine - firstLine + 1);
        MessageBox(m_hWnd, szMsg, _T("Visible Line Count"), MB_ICONINFORMATION);
    }
    

    Example output -

    VisbleCount


  2. Hui Liu-MSFT 48,571 Reputation points Microsoft Vendor
    2024-03-14T07:21:36.1933333+00:00

    Hi,@Igor Korot. You could get the text first, and then count the number of \r\r\n which is the number of soft returns. For more details, you can check out EM_FMTLINES.
    Here is an example of getting the number of soft returns.

    int countSubstring(const std::string& str, const std::string& sub)
    
    {
    
        if (sub.length() == 0) return 0;
    
        int count = 0;
    
        for (size_t offset = str.find(sub); offset != std::string::npos;
    
            offset = str.find(sub, offset + sub.length()))
    
        {
    
            ++count;
    
        }
    
        return count;
    
    }
    
    
    
            SendMessage(m_hEdit, EM_FMTLINES, 1, 0);
    
            int lineCount = SendMessage(m_hEdit, EM_GETLINECOUNT, 0, 0);
    
            char buff[1024];
    
            GetWindowTextA(m_hEdit, buff, 1024);
    
            int result = countSubstring(buff, "\r\r\n");
    
            SendMessage(m_hEdit, EM_FMTLINES, 0, 0);
    
            MessageBoxA(hDlg, std::to_string(result).c_str(), " soft line-break Count", MB_ICONINFORMATION);
         
          
    
    
    

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  3. Igor Korot 6 Reputation points
    2024-05-27T18:42:34.95+00:00

    @RLWA32 ,

    In your little GIF at the end (before you started moving the cursor up) I should get the 4 as the number of logical lines. and 2 as the physical lines.

    (In my case 2 is defines as number of hard CR/LF plus the last line and 4 is defined as number of soft AND hard CR/LF plus the last line).

    Thank you.

    P.S.: I think the 2 will be produced by the EM_GETLINECOUNT. The question is - how to get 4.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.