VC enhanced Edit Control for date input control

Source: Internet
Author: User

The components that MFC provides can already do a lot of things, but sometimes we need these controls to do what we want them to do. For example, edit control, although we can set the edit control to accept only numeric properties, but if we still need it to receive the number of unexpected characters, such as the need for a control can only receive "2004-02-20" in the format of the date characters? We need to handle the input characters in the WM_CHAR message. However, when you enter a character, Windows sends a WM_CHAR message to the edit control, and the application invokes the Windows Default Edit Control window handler function WndProc to handle the control. At this point we need to connect the window object with its own edit class by subclasses. In this way, the message handler function of the class replaces the original message handler function, the window message can be mapped by its own class, and the message processing function of its own class is first invoked, and its own edit class is used to handle the WM_ Char message. Subclasses can be statically associated with macros DDX_Control macros, so that they can be completed by using a function SubclassWindow () or SubclassDlgItem ().

Now let's talk about the date Input box control Implementation section, the program runs as shown in Figure A:

Figure A program run interface

To define a WM_CHAR message handler for the control yourself, you must first derive your own new class Cmyedit from the CEdit class, which can be done by ClassWizard. This class mainly completes the processing of the WM_CHAR and Wn_keydown messages for the edit box class to control the input format. The edit box initially displays the "--" time input format, requiring that the date be entered in the format "Year-month-day". So you set the control format when you initialize the code as follows:

void CMyEdit::Initial()
{
   SetLimitText(10);
   SetWindowText("  - - ");
}

Second, then the key message processing function, because we need to filter the character class (including number and backspace key) and control class two kinds of keystroke messages (mainly including the processing of delete). When the user enters or deletes characters and updates the window, make sure that "-" is in the 5th and 8th positions of the string, and the main idea is to trim the string in the edit box by adding "" Before the character is displayed, so that the string displayed will meet the required requirements. The main processing functions are as follows:

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.