The previous section described the static text box, this section is about the edit box is also a very common control, we can enter and edit text in the edit box. The basic application of the edit box has been demonstrated in the example of the previous addition calculator. The following is a detailed explanation of the use of the edit box.
The notification message for the edit box
When certain events occur in the edit box, a notification message is sent to the parent window. You can see these messages in the message type list when you right-click on the edit box in the dialog template, select "Add Event Handler", and add the messages handler function for the edit box. The following is a brief description of some of the notification messages in the edit box.
En_change: The contents of the edit box are changed by the user, and unlike en_update, the message is emitted when the body of the edit box appears refreshed.
En_errspace: The edit box control cannot request sufficient dynamic memory to meet the needs.
En_hscroll: The user clicks the mouse on the horizontal scroll bar.
En_killfocus: The edit box loses the input focus.
En_maxtext: The character entered exceeds the specified maximum number of characters. In the absence of Es_autohscroll or
Es_autovscroll: In the edit box, the message is also emitted when the body is outside the border of the edit box.
En_setfocus: The edit box gets the input focus.
En_update: Sends a change message when the edit box is ready to display the changed body.
En_vscroll: The user clicks the mouse on the vertical scroll bar.
Create an edit box
MFC provides the CEdit class for the edit box. All the actions of the edit box are encapsulated in the CEdit class.
Similar to the creation of a static text box, in addition to being able to drag an edit box on a dialog template and then associate a variable or use it through an API function, you can also dynamically create an edit box in your program, called the member function of the CEdit class, create. The prototype of the Create member function is as follows:
Virtual BOOL Create (
DWORD Dwstyle,
Const rect& RECT,
cwnd* pParentWnd,
UINT NID
);
Parameter description:
Dwstyle: Specifies the style of the edit box. Can be any combination of styles in the MSDN "Edit Styles". Here is a description of all the styles for "edit Styles".
Es_autohscroll: When a user types a character at the end of a line, the body automatically scrolls to the right by 10 characters, and the body always rolls to the left when the user presses the ENTER key.
Es_autovscroll: The body scrolls up one page when the user presses the ENTER key on the last visible row.
Es_center: Use the text center in the multiline edit box.
Es_left: Left-aligns the body.
Es_lowercase: Converts the letters entered by the user into lowercase letters.
Es_multiline: Specifies a multiline editor. If the multiline editor does not specify a es_autohscroll style, the line will wrap, and if you do not specify Es_autovscroll, the multi-line editor will emit a warning when the body of the window is filled.
Es_nohidesel: By default, when the edit box loses input focus, the selected body is hidden and is displayed when the input focus is obtained. Setting this style prevents this default behavior.
Es_number: Only numbers are allowed in the edit box.
Es_oemconvert: Use the body of the edit box to convert from one to the other between the ANSI character set and the OEM character set. This is useful when you include a file name in the edit box.
Es_password: Causes all typed characters to be displayed with "*".
Es_readonly: Sets the edit box to read-only.
Es_right: Right-aligns the body.
Es_uppercase: Converts the letter entered by the user into uppercase letters.
Es_wantreturn: Causes the multiline editor to receive the ENTER key and wrap. If you do not specify this style, pressing ENTER will select the default command button, which often causes the dialog box to close.
In addition to the above style, the edit box will generally also set the Ws_child, Ws_visible, Ws_border and other window styles. In addition, the edit box can be multi-line, that is, in the edit box to display multiple lines of text, which need to set Es_multiline style, if you want to support the Multiple line edit box enter, you also set Es_wantreturn.
For creating an edit box in a dialog template, its properties include the style described above, for example, the Multiline property corresponds to the Es_multiline style, and the want return property corresponds to the Es_eantreturn style.
The other three parameters are similar to the parameters of the CREATE function of a static text box, and are not described.
Main member functions of the CEdit class
The most important thing to do with the edit box is to get and set the body of the edit box, which correspond to the member functions GetWindowText and SetWindowText, which are member functions that inherit from the CWnd class, plus You can also use the Getwindowtextlength function of the CWnd class to get the length of the body in the edit box.
The following is a brief introduction to several other major member functions of the CEdit class:
int Linefromchar (int nIndex =-1) const;
Returns the line number (zero-based) of the row of characters in the specified index in the multiline edit box, and applies only to the multi-line edit box. Nindex equals 1 Returns the index of the row that contains the first character of the selected body. If no body is selected, the line number of the current row is returned.
int lineindex (int nLine =-1) const;
Returns the index of the starting character of the specified row by nline in the entire string of the edit box, only applicable to the multiline edit box. If the row exceeds the maximum number of rows in the edit box, 1 is returned, and if Nline is-1, the index of the starting character of the row that contains the current caret is returned.
void GetSel (int& nStartChar, int& nendchar) const;
Gets the index range of the selection body. nStartChar returns the starting index of the selected body, NENDCHAR returns the terminating index of the selected body (not included in the selection). If no body is selected, both are the index of the current caret.
void SetSel (int& nStartChar, int& nendchar) const;
Select the body of the edit box. nStartChar the index at the beginning of the selection, nEndChar the index at the end of the selection. If nStartChar is 0 and nEndChar is-1, all bodies are selected, and if nStartChar is 1, all selections are canceled. Bnoscroll is false when the caret is scrolled and made visible, true when it is not scrolled.
void Replacesel (LPCTSTR lpsznewtext, BOOL bcanundo = FALSE);
Replaces the selected body with a string pointed to by Lpsznewtext. If Bcanundo is true, the substitution can be canceled.
int linelength (int nLine =-1) const;
Gets the length of the byte in the row of the specified character index (carriage returns and line breaks are not counted), and the parameter nline describes the character index. If the value of nline is-1, the function returns the length of the current line (if no body is selected), or selects the total number of characters of the line occupied by the body minus the number of characters in the selection body (if the body is selected). If used in a single-line edit box, the function returns the length of the entire row.
int GetLine (int nIndex, LPCTSTR lpszbuffer) const;
int GetLine (int nIndex, LPCTSTR lpszbuffer, int nmaxlength) const;
Used to get the specified body (not including the carriage return and line breaks at the end of the line), only for multi-line edit boxes. The parameter nindex is the line number, Lpszbuffer points to the buffer that holds the body, and nmaxlength specifies the maximum number of bytes for the copy. If the specified line number is less than the actual number of rows in the edit box, the function returns the actual copy of the number of bytes, and if the specified line number is greater than the actual number of rows in the edit box, the function returns 0. It is important to note that the Getline function does not add a string terminator (NULL) at the end of the string in the buffer.
UINT Getlimittext () const
Gets the maximum number of bytes in the body that the edit box can accept.
void Limittext (int nchars = 0);
Sets the maximum length (in bytes) of the body that the user can enter in the edit box. If Nchars is 0, the maximum length is uint_max bytes. If Nchars is 0, the maximum length is uint_max bytes.
Introduction to MFC Programming 21 (Common controls: Edit control)