Edit Control Messages

Windows Predefined Controls – Part 7
Volume – Windows User Interface

Introduction
This is part 7 of my series, Windows Predefined Controls. In order to understand this tutorial, you most have read all the previous tutorials of the series. In this part of the series, we look at messages that the application can send to the edit control. We also see how to work with the LOWORD and HIWORD. Finally, we look at a macro (function).

Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.

Messages that can be sent
An application can send a message to an edit control. The messages are processes by the predefined window class procedure. For the rest of this series we shall look at these edit control messages.

To send a message to a control, you can use the SendMessage function whose syntax is:

LRESULT WINAPI SendMessage(
    HWND hWnd,
    UINT Msg,
    WPARAM wParam,
    LPARAM lParam
);

We saw this syntax in one of the previous parts of the series. This function returns the return value of the message. For the parameters of the function, hWnd is the handle of the control. Msg is the message code (identifier), such as, EM_GETSEL (see below). wParam and lParam are for any accompanying data for the message code.

Let us now have the messages:

Messages

EM_CANUNDO
This message is used to determine if the most recent action in the edit control can be undone. If it can be undone, the return value is non-zero. The wParam and lParam parameters of this message are not used and must each be zero (cast to WPARAM or LPARAM accordingly).

EM_CHARFROMPOS
An application sends this message to the edit control to determine the character closest to a specified point in the client area of an edit control. Here, the wParam parameter is not used; set it to NULL. The lParam parameter has the coordinates of the point of the control’s client area, relative to the left-top corner of the client area, in screen units. The LOWORD of lParam contains the horizontal coordinate, while the HIWORD contains the vertical coordinate.

The return value of the message is returned as LRESULT, which is the returned value of the SendMessage function. The LOWORD of LRESULT specifies the zero-based index of the character nearest the specified point counted from the first character of the first line. If the specified point is beyond the last character in the edit control, the return value indicates the last character in the control. The HIWORD of LRESULT specifies the zero-based index of the line that contains the character, counted from the first line. For single-line edit controls, this value is zero. The index indicates the line delimiter if the specified point is beyond the last visible character in a line.

EM_GETLINE
The message copies a line of text from an edit control and places it in a specified buffer. The copied line does not contain a terminating null character. The buffer here is a C++ array of chars, which you have to declare before you send the message.

The wParam parameter is the zero-based index (number) of the line to retrieve from a multi-line edit control. A value of zero specifies the topmost line. This parameter is ignored by a single-line edit control.

The lParam parameter is a pointer to the buffer that receives a copy of the line. Before sending the message, set the first word of this buffer to the size, of the buffer. The size in the first word will be overwritten by the copied line.

The return value is the number of characters copied. The return value is zero if the line number specified by the wParam parameter is greater than the number of lines in the edit control.

EM_GETLINECOUNT
This message gets the number of lines in a multi-line edit control. The wParam parameter is not used and must be set to zero. The lParam is also not used and must be set to zero. The return value is an integer specifying the total number of text lines in the multi-line edit control. If the control has no text, the return value is 1. The return value will never be less than 1.

EM_GETMARGINS
This message gets the widths of the left and right margins of an edit control. Here the wParam parameter is not used and must be zero. The lParam is also not used and must be zero. The message returns the width of the left margin in the LOWORD, and the width of the right margin in the HIWORD.

The following code segment shows how to get the LOWORD from the LRESULT type returned by a SendMessage function and then convert (cast) it into an integer.

    LRESULT myIntL = SendMessage(hwndEdit1, EM_GETMARGINS, 0, 0);
    int myInt = (int)LOWORD(myIntL);

A similar reasoning works for the HIWORD.

EM_GETPASSWORDCHAR
This message gets the password character that an edit control displays when the user enters text. The wParam and lParam parameters are not used and should each be zero. The return value specifies the password character to be displayed in place of any characters typed by the user. If the return value is NULL, there is no password character, and the control displays the characters as typed by the user.

EM_GETSEL
The user can select a group of characters in an edit control. This message will get the starting and ending character positions of the current selection in an edit control. There is something to note here: The ending position is not the position of the last selected character. It is the position of the character just after the last selected character.

The wParam parameter is a pointer to a buffer that receives the starting position of the selection. This parameter can be NULL. The lParam parameter is a pointer to a buffer that receives the position of the first unselected character after the end of the selection. This parameter can also be NULL.

The return value gives you the desired starting and ending positions, so the wParam and lParam parameters can really be NULL. The return value is a zero-based value with the starting position of the selection in the LOWORD and the position of the first character just after the last selected character in the HIWORD. If either of these values exceeds 65,535, the return value is -1.

EM_LINEFROMCHAR
This message is used to get the index of the line that contains the specified character index in a multi-line edit control. Here the wParam parameter is the character index of the character contained in the line whose number is to be retrieved. If this parameter is -1, EM_LINEFROMCHAR retrieves either the line number of the current line (the line containing the caret) or, if there is a selection, the line number of the line containing the beginning of the selection. lParam is not used and should be set to NULL. The return value is the zero-based line index of the line containing the character index specified by wParam.

EM_LINEINDEX
This message is used to get the character index of the first character of a specified line in a multi-line edit control. The wParam parameter is the zero-based line number. A value of -1 specifies the current line number (the line that contains the caret). The lParam parameter is not used and so should set to NULL.

EM_LINELENGTH
This message is sent to an edit control to retrieve the length, in characters, of a line. The wParam parameter is the character index of a character in the line whose length is to be retrieved. If this parameter is greater than the number of characters in the control, the return value is zero.

This parameter can be -1. In this case, the message returns the number of unselected characters on lines containing selected characters.

EM_POSFROMCHAR
The message retrieves the client area coordinates (relative to the left-top corner of the client area) of a specified character in an edit control. wParam is the zero-based index (position number between zero and n-1) of the character. The lParam parameter is not used here; so it should be NULL.

The return value contains the client area coordinates of the character. The LOWORD contains the horizontal coordinate and the HIWORD contains the vertical coordinate.

A returned coordinate can be a negative value if the specified character is not displayed in the edit control’s client area. The coordinates are truncated to integer values.

If the character is a line delimiter (end of line – see later), the returned coordinates indicate a point just beyond the last visible character in the line. If the specified index is greater than the index of the last character in the control, the control returns -1.

EM_REPLACESEL
Assume that a group of text has been selected in the in an edit control, either by the user or by the application. The application can use the EM_REPLACESEL function to replace the selected text. Here, the wParam parameter specifies whether the replacement operation can be undone if the user would want to undo. If this is TRUE, the operation can be undone. If this is FALSE , the operation cannot be undone. The lParam parameter is a pointer to a null-terminated string (cast) containing the replacement text. This message does not return any value.

EM_SETMARGINS
The application uses this message to set the width of the left and right margins for an edit control. The message redraws the control to reflect the new margins. The wParam parameter of the message can be the identifier, EC_LEFTMARGIN, meaning set the left margin or EC_RIGHTMARGIN meaning set the right margin.

For the lParam parameter the LOWORD specifies the new width of the left margin, in pixels (smallest dot on screen). The HIWORD specifies the new width of the right margin, in pixels.

This message does not return a value.

The following statement shows how to make the LOWORD of the lParam parameter hold an integer value of 18 in the SendMessage function; a similar reasoning works for the HIWORD and the wParam parameter.

    SendMessage(hwndEdit1, EM_SETMARGINS, EC_LEFTMARGIN, LOWORD((LPARAM)18));

EM_SETPASSWORDCHAR
The message sets or removes the password character for an edit control. When a password character is set, that character is displayed in place of the characters typed by the user. The wParam parameter is the password character to be displayed in place of the characters typed by the user. If this parameter is zero, the control removes the current password character and displays the actual characters typed by the user. The lParam parameter is not used and should be set to NULL. This message returns no value.

EM_SETREADONLY
This message sets or removes the read-only style (ES_READONLY) of an edit control. The wParam parameter specifies whether to set or remove the ES_READONLY style. A value of TRUE sets the ES_READONLY style; a value of FALSE removes the ES_READONLY style. Here the lParam parameter is not used and should be NULL.

EM_SETSEL
Instead of the user selecting the characters of an edit control with his mouse or keyboard, the application can select the characters by sending this message to the edit control. The wParam parameter is the starting character position of the selection. The lParam parameter is the ending character position of the selection. The ending position is just after the selection. The message returns no value. If the start is 0 and the end is -1, all the text in the edit control is selected. If the start is -1, any current selection is deselected.

EM_SETTABSTOPS
This message sets the tab stops in a multi-line edit control. This message is processed only by multi-line edit controls.

The wParam parameter is the number of tab stops contained in the array (see below). If this parameter is zero, the lParam parameter is ignored and default tab stops are set at every 32 dialog template units (see later). If this parameter is 1, tab stops are set at every n dialog template units, where n is the distance pointed to by the lParam parameter. If this parameter is greater than 1, lParam is a pointer to an array of tab stops.

The lParam parameter is a pointer to an array of unsigned integers specifying the tab stops, in dialog template units. If the wParam parameter is 1, this parameter is a pointer to an unsigned integer containing the distance between all tab stops, in dialog template units.

EM_UNDO
This message is sent to an edit control to undo the last operation. When this message is sent to an edit control, the previously deleted text is restored or the previously added text is deleted.

The wParam and lParam parameters are not used and must each be zero. If the message succeeds, the return value is TRUE. If the message fails, the return value is FALSE.

Macro
A macro is like a function.

The Edit_Enable Macro
The Edit_Enable macro enables or disables an edit control. A control is by default enabled. When a control is disabled, the user cannot use it. The prototype for this macro is:

BOOL Edit_Enable(HWND hwndCtl, BOOL fEnable);

hwndCtl is the handle of the edit control. If the value of fEnable is TRUE, the edit control will be enabled; if it is FALSE the edit control will be disabled.

The return Boolean value is zero if the control was previously disabled; otherwise it is nonzero.

We have seen a lot in this part of the series. Let us take a break here and continue in the next part.

Chrys

To arrive at any of the parts of this series, just type the corresponding title below in the Search Box of this page and click Search (use menu if available):

Meaning of Controls in Windows
Control Messages
Edit Control in Windows
Windows Edit Control Styles
Edit Control Notification Messages and Processing
Manipulating Edit Control Text
Edit Control Messages
Button Types in Windows

Facebook
Twitter
Pinterest