Determining the Visible Area of a Multiline Edit Control

Last reviewed: November 2, 1995
Article ID: Q88387
The information in this article applies to:
  • Microsoft Windows Software Development Kit (SDK) versions 3.1
  • Microsoft Win32 Application Programming Interface (API) included with:

        - Microsoft Windows NT versions 3.5 and 3.51
        - Microsoft Windows 95 version 4.0
    

SUMMARY

The multiline edit control provided with Microsoft Windows versions 3.0 and 3.1 does not provide a mechanism that allows an application to determine the currently visible lines of text. This article outlines an algorithm to provide that functionality.

MORE INFORMATION

The general idea is to determine the first and last visible lines and obtain the text of those lines from the edit control. The following steps detail this process:

  1. A Windows-3.1-based application can use the newly available message, EM_GETFIRSTVISIBLELINE, to determine the topmost visible line.

    A Windows-3.0-based application can use a technique described in the following Microsoft Knowledge Base article to determine the line number of the first visible line:

          ARTICLE-ID: Q68572
    
          TITLE     : Caret Position & Line Numbers in Multiline Edit Controls
    
    

  2. Obtain the edit control's formatting rectangle using EM_GETRECT. Determine the rectangle's height using this formula:

          nFmtRectHeight = rect.bottom - rect.top;
    

  3. Obtain the line spacing of the font used by the edit control to display the text. Use the WM_GETFONT message to determine the font used by the edit control. Select this font into a display context and use the GetTextMetrics function. The tmHeight field of the resulting TEXTMETRIC structure is the line spacing.

  4. Divide the formatting rectangle's height (step 2) with the line spacing (step 3) to determine the number of lines. Compute the line number of the last visible line based on the first visible line (step 1) and the number of visible lines.

  5. Use EM_GETLINE for each line number from the first visible line to the last visible line to determine the visible lines of text. Remember that the last visible line may not necessarily be at the bottom of the edit control (the control may only be half full). To detect this case, use EM_GETLINECOUNT to know the last line and compare its number with the last visible line. If the last line number is less than the last visible line, your application should use EM_GETLINE only on lines between the first and the last line.


Additional reference words: 3.00 3.10 3.50 4.00 95
KBCategory: kbui
KBSubcategory: UsrCtl


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: November 2, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.