How To Determine the Visible Area of a Multiline Edit Control (88387)



The information in this article applies to:

  • Microsoft Platform Software Development Kit (SDK) 1.0
  • Microsoft Windows Software Development Kit (SDK) 3.1

This article was previously published under Q88387

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:

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

Modification Type:MinorLast Reviewed:7/11/2005
Keywords:kbCtrl kbEditCtrl kbhowto KB88387