"MFC" Re-login with dialog box switch implementation

Source: Internet
Author: User

The Last "MFC" with the dialog page implementation of user login (click to open the link) is a Realization Dialog page login Win32 program, and MFC a little relationship, although the WIN32 program is the basis of MFC program, but in MFC how to Implement dialog box switch it? At the same time, this program to re-login, can be flexible in different dialog boxes to achieve the switch.


I. BASIC OBJECTIVES

1, this program point exit can successfully quit, it is of course


2, the user in the user name Input admin password Input 123 to be able to log in normally, otherwise pop-up dialog box prompt


3, after login, click the re-login button to return to the login box, and, the function of this login box as usual, click "Login" first to determine whether the user name and password have entered an error and then log in, click "Exit" and then quit the whole program



Second, the production process

1, in MFC Create a new dialog box, and set the following layout, and the focus is placed in order, that is, the dialog box default option is to enter the user name, rather than the login button. Change the original cancellation button to exit and change the original confirmation button to login. Also change the title to "User Login". The edit box style for the password is "password". This part of the specific how to operate in the "MFC" with the dialog page implementation of user login "(click Open link) has been discussed, no longer repeat, the process is completely identical to the production of Win32 program.


2, press CTRL+W or click "View" and "New Class Wizard" to create a class for this dialog box, the New Class Wizard will start with a dialog box, click OK after the following screen popup:


Enter the name of the field in the title of the dialog box, such as Clogindlg, generally Cxxdlg as a dialog box name, and XX in the first letter to uppercase, and later in the program to use this dialog box, then use the Clogindlg to do the class declaration. Therefore, in order for your program to do complex, but also without the use of annotations, realize that this dialog box, please do not name this dialog box A.

3. After you create a new class for the Login dialog box, On the left side of the FileView view can be sourcefiles under the LoginDlg.cpp, and under the Headerfiles more LoginDlg.h two files, later if you want to use the Login dialog box, in addition to using CLOGINDLG to make class declarations, but also in the corresponding file Quote # include "LoginDlg.h", otherwise error, because Clogindlg is in LoginDlg.h inside


4, after switching back to ResourceView in the Login dialog box, double-click the login button, for the login button new class mapping function, how to operate, in the "MFC" Student information management, the implementation of the list control node additions and deletions (click Open link) has been mentioned.

For the login button, the function void Cloingdlg::onok () is as follows:

void Clogindlg::onok () {//Todo:add extra validation herecstring sid,spwd; GetDlgItemText (IDC_EDIT1,SID); GetDlgItemText (IDC_EDIT2,SPWD); if (sid== "admin" &&spwd== "123")//close this dialog box and return DoModal () to Idokcdialog::onok ( ); Elseafxmessagebox ("Username or password is wrong! ");}

Check out the values of the two dialog boxes first. If the condition is met, close the dialog box and send the Idok return value to the Clogindlg DoModal () method in the other file.

5. The Cancel button is changed because it comes with the exit button, so no action is required. After entering the main dialog editor, for the main dialog, do not delete all the objects so quickly, for the inside of the OK and Cancel button, we have to add a message map function, in case the user with the ESC on the keyboard or click on any part of the dialog box to close the dialog box. At the same time, the Close button of this dialog is also deleted, the "system menu" that controls the button's control in the style of the dialog box.


6, start by double-clicking on the "OK" or "Cancel" button of any one, may pop-up wizard, then, in the pop-up Class wizard, click Cancel, and then double-click the two buttons you can create a new message map function for them, in essence, not to say new, mainly overwrite:


The default two functions are Cdialog::oncancel (), and Cdialog::onok (), two functions, if you do not add the corresponding message mapping function for them, that is, to rewrite their message mapping function, their default message function is also the case, The user can close this dialog box by using ESC on the keyboard or by clicking any part of the dialog box. Put these two lines commented out or deleted to go back to the resoureview below layout, delete all the things that come with, the title of this dialog box to "Control Panel", pull out the four buttons, where the main realization is "re-login" and "Exit", the other functions of the future to open the article again, as to how to pull, Is too basic, can not see my previous "MFC" Basic dialog Box Program-Adder (click to open the link):


7, this dialog box is the start of the new project has a main dialog box, so there is no need to add the Class Wizard, by default there are two classes, in ClassView attempt to discover. Double-click the Exit button to add the following function to it:

void Cisysdlg::onbutton4 () {//Todo:add your control notification handler code here//is an exit without any value EndDialog (-1);}
Remember that you have to use EndDialog (-1) to end the program, not Cdialog::oncancel (), because this is enough to completely destroy the program, using Cdialog::oncancel (); the dialog box is missing, but the entire program remains in the thread list, That is, there is no exit.

8. The message map function of the re-login button is as follows:

#include "LoginDlg.h" void Cisysdlg::onbutton1 () {//First hide this dialog box ShowWindow (sw_hide);//Popup Login dialog box Clogindlg dlg;// Finally re-display this dialog box, if the user points to "cancel" if not logged in successfully, then because the Login dialog box returns Idcancel,enddialog (-1); be able to exit the entire program if (Idcancel==dlg. DoModal ()) EndDialog ( -1); Elseshowwindow (sw_show);}
As mentioned above because of the use of the Login dialog box, it is necessary to declare a so-called "handle" of this dialog box with Clogindlg, and introduce the header file of this dialog box, in order to completely manipulate the dialog box, such as the following dlg. DoModal () operation. The rest of the operation you do not write is the dialog box, the default is this dialog box.

So far, the whole program is done!


"MFC" Re-login with dialog box switch implementation

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.