VS2010-MFC (Common Controls: Edit box editor control)

Source: Internet
Author: User

Transferred from: http://www.jizhuomi.com/software/181.html

The edit box is a very common control, 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.

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 by right-clicking on the edit box in the dialog template, selecting "Add Event Handler", and adding a messaging function to 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 after the body of the edit box is refreshed.
En_errspace: The edit box control cannot request sufficient dynamic memory to meet the needs
En_hscroll: The user clicks on the horizontal scroll bar
En_killfocus: edit box loses input focus
En_maxtext: The character entered exceeds the specified maximum number of characters. In an edit box without es_autohscroll or es_autovscroll: The message is also emitted when the body 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

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: Centering the text in a multiline edit box
Es_left: Left-justified 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 multiline editor will wrap, and if you do not specify Es_autovscroll, the multi-line editors are filled in when the body of the window is fullmake a warning sound
Es_nohidesel: By default, the selected body is hidden when the edit box loses input focus and is displayed when the input focus is obtained. Setting this style can prohibit this default behavior
Es_number: Allow only numbers to be entered in the edit box
Es_oemconvert: Enables 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-justified body
Es_uppercase: Converts the letters 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 editor 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 the edit box created in the dialog template, its properties contain the style described above, 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 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 line of the character in the specified index in the multiline edit box, which applies only to the multiple-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. 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 that contains the current caret.

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,bool bnoscroll=false);

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

int getlinecount () const;

Gets the number of lines in the body for the multiple-line edit box. Returns 1 if the edit box does not have a body.

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 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 (excluding carriage returns 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.

CEdit Class Application Example

Here is 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 containing a carriage return, which is eventually displayed as two lines of text.

Here are a few simple steps to be introduced:

1. Create a dialog-based MFC program with the name "Example21".

2. In the Auto-generated dialog template Idd_example21_dialog, delete the static text box "Todo:place dialog controls here.", add an edit box with ID set to Idc_multi_line_edit, The Multiline property is set to true.

3. Add the CEdit type control variable m_editmultiline for the edit box Idc_multi_line_edit.

4. Modify the Cexample21dlg::oninitdialog () function (PS: find the TODO prompt and modify it on the next line ):

BOOL Cexample21dlg::oninitdialog () {cdialogex::oninitdialog (); //Add "About ..." menu item to System menu. //Idm_aboutbox must is in the System command range. ASSERT ((Idm_aboutbox &0xfff0) ==Idm_aboutbox); ASSERT (Idm_aboutbox<0xf000); CMenu* Psysmenu =GetSystemMenu (FALSE); if(Psysmenu! =NULL)           {BOOL bnamevalid;           CString Straboutmenu; Bnamevalid=straboutmenu.loadstring (Ids_aboutbox);           ASSERT (Bnamevalid); if(!Straboutmenu.isempty ()) {Psysmenu-AppendMenu (Mf_separator); Psysmenu-AppendMenu (mf_string, Idm_aboutbox, Straboutmenu); }       }         //Set The icon for this dialog. The framework does this automatically//When the application ' s main window was not a dialogSetIcon (M_hicon, TRUE);//Set Big iconSetIcon (M_hicon, FALSE);//Set Small Icon//Todo:add Extra Initialization hereM_editmultiline.setwindowtext (_t ("Chicken Peck Rice Blog/software"));//set the edit box body to "Chicken Peck Rice Blog. com"M_editmultiline.setsel (3,5);//Select a starting index of 3, terminate the body of index 5 (not included in the selection), i.e. "blog"M_editmultiline.replacesel (_t ("\r\nwww.jizhuomi.com"));//Replace the selected "blog" with "\r\nwww.jizhuomi.com"      returnTRUE;//return TRUE Unless you set the focus to a control   }

5. Compile and run the program, the result dialog box is as follows:

But the result of my program running is this, and I don't know why:

Set the property multiline to false and make it this way ...

Can be said to be very puzzling ...

VS2010-MFC (Common Controls: Edit box editor control)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.