Learn MFC process by writing the Serial Port Helper Tool--(vi) Add edit box control

Source: Internet
Author: User

learn the MFC process by writing a Serial port helper tool

Because it has been done several times MFC programming, each time the project is completed, MFC basic operation is clear, but too long time no longer contact with MFC project, again do MFC project, but also from the beginning familiar. This time by doing a serial assistant once again familiar with MFC, and made a record, in order to facilitate later access. The process of doing more is encountered problems directly Baidu and Google search, so many are superficial understanding, know it does not know why. In addition to do this tool just to get familiar with, many features are not perfect! (development tool VS2008)

(vi) Add edit box control

Property Description:

Auto HScroll Sets True when the text entered by the edit box is larger than the bounding rectangle when the horizontal direction is increased.

Auto VScroll set True to automatically wrap each line as it fills up.

The Vetrical Scroll property is set to true, and a vertical scroll bar appears when the input or display exceeds the size of the edit box.

The Multiline property is set to true, and multiple exercises can be used to wrap text when the edit box is larger than the border.

ReadOnly set true can only be displayed and read, cannot be edited as input, can be displayed using SetWindowText.

To add a control, modify the property ID, and add the variable part slightly, see Press the button section.

1,the basic function of edit:

SetWindowText () Setting text display

GetWindowText () Get text display

These two functions are API functions, which are supported by most controls.

2, realize special function: Automatic line wrapping, vertical scroll bar automatically move down

(1) Set its Multiline property to True,auto HScroll property settings False,auto VScroll Property set True so that each line fills up and wraps automatically (auto VScroll is set to Faile).

(2) The Vetrical Scroll property is set to true, and a vertical scroll bar appears when the input or display exceeds the size of the edit box.

(3) If it is input, the scroll bar will automatically move to follow the current input line, but if the settings will be a lot of content once let the edit box display, scroll bar will always be at the top, need to manually pull to the bottom to see the last line of content, in many cases this is not needed, but want the scroll bar at the bottom, The following code will implement this function:
THIS->SETDLGITEMTEXTW (idc_allmsg_show,allmsg);//display allmsg content to an edit box with ID idc_allmsg_show
M_showmsg. Linescroll (m_showmsg. Getlinecount ());//m_showmsg is the control variable name for the edit box
(use some member functions of the control variable to set the position of the scroll bar)

This function implementation method, the above content reference: http://blog.sina.com.cn/s/blog_5d2bad130100sysh.html

The method shown in this example is SetWindowText.

M_editrxdata.setwindowtext (Pccomser->m_strrxdata); Show receive

M_editrxdata.linescroll (M_editrxdata.getlinecount ());//editrxdata the name of the control variable for the edit box (use some member functions of the control variable to set the position of the scroll bar)

Cedit::linescroll
void Linescroll (int nline,int nchars = 0);
Parameters:

NLine

Specifies the number of rows to scroll vertically.

Nchars

Specifies the number of characters to scroll horizontally. If the edit control uses the Es_right or Es_center style, this value is not valid.

Description
Call this member function to scroll through the text of a multiple-line edit control.
This member function is used only for multi-line edit controls.
The vertical scrolling of an edit control cannot exceed the last line of the text, and if the current line number plus the number of rows specified by Nlines exceeds the total row count in the edit control, its value is adjusted so that the last line of text scrolls to the top of the Edit Control window.
This function can scroll horizontally through the last character of each line.

3. Implement special functions: Limit edit input code for specified content

Both the update event and the Change event can be

void Cserialtestdlg::onenupdateedit1 ()

{

TODO: If the control is a RichEdit control, it will not

Send this notification unless overriding CDialog::OnInitDialog ()

function to send a EM_SETEVENTMASK message to the control,

The enm_update flag "or" is also calculated into the lparam mask.

TODO: Add control notification Handler code here

/*

CString strtemp=_t ("");

Edit1. GetWindowText (strtemp);

int len = Strtemp.getlength ();

for (int i = 0; i < len; i + +)

{

if (Strtemp.getat (i) < ' 0 ' | | strtemp.getat (i) > ' 9 ')

{

strtemp = Strtemp.left (i);

Edit1. SetWindowText (strtemp);

Edit1. SetSel (i,i,true);

AfxMessageBox (_t ("Incorrect input number!"));

Return

}

}

*/

}

void Cserialtestdlg::onenchangeedit1 ()

{

TODO: If the control is a RichEdit control, it will not

Send this notification unless overriding CDialog::OnInitDialog ()

function and call CRichEditCtrl (). SetEventMask (),

The Enm_change flag "or" is also calculated into the mask.

TODO: Add control notification Handler code here

CString strtemp=_t ("");

TCHAR txtmp;

Edit1. GetWindowText (strtemp);

int len = Strtemp.getlength ();

for (int i = 0; i < len; i + +)

{

Txtmp = Strtemp.getat (i);

if ((txtmp>=_t (' 0 ') && txtmp <= _t (' 9 ')) | | (txtmp>=_t (' a ') && txtmp <= _t (' f ')) \

|| (txtmp>=_t (' A ') && txtmp <= _t (' F ')) | | (Txtmp = = _t (')))

{

;

}

Else

{

strtemp = Strtemp.left (i); Take a string from the left

Strtemp.delete (i,1); It's not perfect yet.

M_edittxdata.setwindowtext (strtemp);

M_edittxdata.setsel (i,i,true);

AfxMessageBox (_t ("Please enter the correct format 0-9 a-f a-f!"));

Return

}

}

}

The two functions are implemented exactly the same way, except for different event types, one for update updates and one for change events, both of which can be implemented, either.

The function is implemented by getting the text content from the edit box, checking for numbers by character (this feature is used to send a 16-digit number, prohibiting the entry of non-numeric characters), after detecting this character, deleting the character, and re-displaying the remaining string, and setting the cursor position in the edit box.

This function involves several string CString types of operations, this is the VC data type, related operations please refer to the API, will be directly introduced in the future. It is also convenient to manipulate strings with this type in VC operations, similar to the Java string type. Here is a brief introduction to some of the functions used here.

TCHAR txtmp;//tchar is related to the environment, the Unicode character set is WCHAR, and multibyte character sets are char types

CString strtemp=_t (""); Initializes the strtemp to an empty string.

int len = Strtemp.getlength (); Gets the length of the string. Empty characters that do not contain endings.

Txtmp = Strtemp.getat (i);

TCHAR GetAt (int nIndex) const;
Returns the character labeled Nindex, same as the [] usage of the string

strtemp = Strtemp.left (i); From the left-hand string, the function here is to modify a character in the middle of a string or to insert a character that is not a number, from this character to the subsequent string will be deleted, apparently not reasonable.

Strtemp.delete (i,1); It's not perfect yet.

int Delete (int nIndex, int ncount = 1)
Delete the characters and delete the ncount characters starting with the subscript nindex.

The function here is to remove a character from the middle of the string, if you insert a character that is not a number, delete the character directly, but if you modify a character in the string to make it a non-numeric character, delete the character directly so that the original character is deleted, although it is slightly better than the function above. But it's not perfect.

M_edittxdata.setsel (i,i,true); Place the cursor position at the insertion character

Cedit::setsel
void SetSel (DWORD dwselection, BOOL bnoscroll = FALSE);
void SetSel (int nstartchar, int nendchar, BOOL bnoscroll = False);
Parameters:

Dwselection

The low word specifies the starting position, and the high word is the end position. If the low is 0 and the high is-1, all the text in the edit control is selected, and if the low word is-1, any current selection is removed from the selected state.

Bnoscroll

Indicates whether the caret is displayed as scrolling visible. If the value is False, the display is true.

nStartChar

Indicates the starting position of the currently selected section. If Nstartchar=0 and Nendchar=-1, the text of the edit control is selected all, and if nstartchar=-1, any current selection is removed from the selected state.

nEndChar

Indicates the end position.

Description
Call this member function to select a range of characters in an edit control.

AfxMessageBox (); Functions in MFC, pop-up information boxes are used to display. There are some differences with the WIN32 API's MessageBox (), which can be used to search for relevant information.

Learn MFC process by writing the Serial Port Helper Tool--(vi) Add edit box 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.