editing Box Edit Control
The edit control in this verse is also a very common kind of controls, and we can enter and edit the 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.
A. Notification message for the edit box
The edit box sends a notification message to the parent window when certain events occur. 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 a message handler function for the edit box. The following is a brief introduction to some of the notification messages for the edit box.
En_change: The contents of the edit box have been changed by the user, unlike En_update, which is issued after the text that is displayed in the edit box is refreshed
En_errspace: Edit box control cannot request enough dynamic memory to meet needs
En_hscroll: The user clicks the mouse on the horizontal scroll bar
En_killfocus: edit box loses input focus
En_maxtext: The characters entered exceed the maximum number of characters specified. In the absence of Es_autohscroll or
Es_autovscroll: In the edit box, this message is also emitted when the body text is outside the border of the edit box
En_setfocus: edit box to get input focus
En_update: Send this message when the edit box is ready to display the changed body
En_vscroll: The user clicks the mouse on the vertical scroll bar
two. edit box Creation
MFC provides an 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 dragging an edit box on a dialog template, then associating a variable or using it through an API function, you can also dynamically create an edit box in a program that invokes the member function create of the CEdit class. The prototype of the Create member function is as follows:
Virtual BOOL Create (
const rect& RECT,
cwnd* pparentwnd, UINT nID
Parameter description: dwstyle: Specifies the style of the edit box. Can be any combination of the styles contained in the "Edit Styles" in MSDN. The following 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 will automatically scroll 10 characters to the right, and when the user presses ENTER, the body always rolls to the left
Es_autovscroll: When the user presses the ENTER key on the last visible line, the body scrolls up one page
Es_center: Center The body in a multiline edit box
Es_left: left-aligned body
Es_lowercase: Convert all user input letters into lowercase letters
Es_multiline: Specifies a multiline editor. If the multiline editor does not specify the Es_autohscroll style, the line wraps automatically, and if you do not specify a es_autovscroll, the multiline editor is filled in when the body of the window is full
Give a warning sound
Es_nohidesel: By default, when the edit box loses the input focus, the selected body is hidden and displayed when the input focus is obtained. Set this style to prevent this default behavior
Es_number: Only allow numbers to be entered in the edit box
Es_oemconvert: Enables the body of the edit box to be converted 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: Make all typed characters "*" to display
Es_readonly: Set the edit box to read-only
Es_right: Right Align Body
Es_uppercase: Converts all the letters entered by the user into uppercase letters
Es_wantreturn: Causes the multiline editor to receive the ENTER key input and wrap the line. If you do not specify this style, pressing ENTER selects the default command button, which often causes the dialog box to close
In addition to the above style, the editorial section will generally set Ws_child, ws_visible, Ws_border and other window styles. In addition, the edit box can be multi-line, that is, display multiple lines of text in the edit box, which requires setting the Es_multiline style, and setting Es_wantreturn if you want the multiple-line edit box to support the ENTER key.
For an edit box created in a dialog template, its properties contain the above style, for example, the Multiline property corresponds to the Es_multiline style, and the Want return property corresponds to the Es_wantreturn style.
The other three parameters are similar to the parameters of the CREATE function for the static text box.
two. Main member functions of the CEdit class
The most important thing to use the edit box is to get and set the body in the edit box, and their corresponding member functions are GetWindowText and SetWindowText, both of which are inherited from the CWnd class, and the member functions are also You can also use the Getwindowtextlength function of the CWnd class to get the length of the body text 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 line that contains the characters for the specified index in the multiline edit box, and applies only to multiline edit boxes. 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 line from nline in the entire string of the edit box, and applies only to multiline edit boxes. Returns 1 if the specified row exceeds the maximum number of rows in the edit box, and if Nline is-1, returns the index of the starting character of the row in which the current caret is located. void GetSel (int& nstartchar,int& nendchar) const; Get the index range of the selected body. nStartChar returns the starting index of the selected body, nEndChar returns the ending index of the selected body (not included in the selection). If no body is selected, both are indexed by the current caret. void setsel (int nstartchar,int nendchar,bool bnoscroll=false); Select the body in the edit box. nStartChar is 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 the bodies are selected, and if nStartChar is 1, all selections are canceled. When Bnoscroll is false, scrolls the caret and makes it visible, not scrolling when true. void Replacesel (lpctstr lpsznewtext,bool bcanundo = FALSE); replaces the selected body with the string that Lpsznewtext points to. If Bcanundo is true, the substitution can be undone. int Getlinecount () const; Gets the number of lines in the body, only for multiline edit boxes. Returns 1 if the edit box has no body text. inT linelength (int nline = –1) const; Gets the byte length of the line where the specified character index is located (the carriage return and newline characters are not counted at the end of the line), and the parameter nline is described as a character index. If the value of nline is-1, the function returns the length of the current line (if no body is selected) or the total number of characters in the line occupied by the body minus the number of characters in the selected body (if any text is selected). For a single-line edit box, the function returns the length of the entire body. int getline (int nindex, LPTSTR lpszbuffer) const;
int getline (int nindex, LPTSTR lpszbuffer, int nmaxlength) const; Used to get the body of the specified line (not including carriage returns and line breaks at the end of the line), which applies only to multiline edit boxes. The parameter nindex is the line number, Lpszbuffer point to the buffer that holds the body, and nmaxlength the maximum number of bytes to copy. If the specified line number is less than the actual number of rows in the edit box, the function returns the number of bytes actually copied, and the function returns 0 if the specified row number is greater than the actual number of rows in the edit box. Note that the Getline function does not add a string terminator (NULL) to 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.
three. CEdit Class application Example
Here's a simple example of how to use several member functions of the CEdit class. The function of this example is to first display a line of text in the edit box, and then replace some of the characters with another string that contains a carriage return, which is eventually displayed as two lines of text. The following are simple steps to introduce:
1. Create an MFC program based on the dialog box named "Example21". 2. In the automatically generated dialog template Idd_example21_dialog, delete the static text box "Todo:place dialog controls here.", add an edit box with the ID set to Idc_multi_line_edit, Property multiline set to True.