"MFC" User Login with dialog box paging

Source: Internet
Author: User
Tags strcmp

The so-called dialog paging is the click of a dialog box button, switch to another dialog box,

This dialog box is used for some software that requires the user to log in to operate,

The following is a dialog box paging to implement the user login system


I. BASIC OBJECTIVES

Like the next program, enter the user name and password, if the user name is admin, password is 123456, then the successful login, switch to a "Welcome login" and "Close" button dialog box


If the user name or password is entered incorrectly, the prompt appears,


Click Close to close this program without popping the user Login dialog box.


Second, the production process

1, first as the previous "MFC" For the dialog box Program optimization and for the program to create the icon (click the Open link) The article, create a simple WIN32 program and resource script, like the "MFC" Basic dialog box program-adder (Click the Open link) Article Insert two dialog boxes in the created resource script idd_dialog1, IDD_DIALOG2, are the system default names


2, wherein the IDD_DIALOG1 as shown:


Put two "user name" and "password" static text, change the title of the dialog box to "User Login", and tick the "center" attribute to have the dialog box open and centered, with the "OK" and "Cancel" buttons changed to "login" and "close", and use the control alignment tool below to put the control's layout in place.

where "password" next to the edit box, to change to a password entry:


Double-click the edit box and tick "password" on the "Styles" tab

And, in order to open the program, the cursor is placed in the edit box next to "User name", after placing the control position, press CTRL+D, the following interface appears:


The numbers on the graph indicate that the default is placed above the "login" button, and the keyboard is cut to the "close" button by a tab.

At this point, still click on the "user name" of the static text and its next to the edit box, "password" the static text and its next to the edit box, "Login" button, "close" button, the number is changed to the following:



3, in the resource script to insert the second dialog box idd_dialog2, change the title, drag into the static text, remove the "OK" button, "Cancel" to "close", as shown, you can switch WinMain function to write code.



4. The code for the WinMain function is as follows:

Dialogbox.cpp:Defines the entry point for the application.//#include "stdafx.h"// At first remember to introduce resource.h, because our dialog box is created in the resource script # include "resource.h"//This is the callback function of the Login dialog box bool CALLBACK Dlg1 (HWND Hwnddlg,//Handle of Dialog Boxuint umsg,//Messagewparam WParam,//First message Parameterlparam LParam//Second message parameter) {//if clicked button if (Umsg==wm_command) {//If you click the IDCANCEL button, which is the "close" button, it returns a IDCANCEL value, which is 2, and closes the dialog if (LoWord (WParam) ==idcancel) { EndDialog (Hwnddlg,idcancel);} If you click on the IDOK button, that is, "OK" button if (LoWord (WParam) ==idok) {//with string username; no difference, just char username[20] qualify this string length 20char Username[20];char pwd[20];//takes the value of idc_edit1, puts it in the username string, and intercepts only the first 20 characters GetDlgItemText (Hwnddlg,idc_edit1,username, sizeof (username));//Remove the Idc_edit2 value, place it in the PWD string and intercept only the first 20 characters GetDlgItemText (hwnddlg,idc_edit2,pwd,sizeof (PWD));// The comparison string please use strcmp (A, b) is equal to 0 of the function, direct = = is not possible, do not make the C language common mistakes if (strcmp ("admin", username) ==0 && strcmp ("123456", pwd) = = 0) {//If the user name and password entered are equal to admin and 123456, close this dialog and return a IDOK value, which is 1EndDialog (Hwnddlg,idok);} else{//If you enter a user name and passwordis not the two values, then the MB_OK type of window is played. This window is not closed here. MessageBox (Hwnddlg, "username or password is wrong!") "," User Login ", MB_OK);}}} Return 0;};/ /This is the Welcome Login dialog Box callback function bool CALLBACK Dlg2 (HWND Hwnddlg,//Handle of dialog Boxuint umsg,//Messagewparam WParam,//First MESSAG E Parameterlparam LParam//Second message parameter) {//logic is very simple, if the user clicked the "Close" button, then close this dialog box if (Umsg==wm_command) {if (LoWord ( WParam) ==idcancel) {EndDialog (hwnddlg,idcancel);}} Return 0;};/      /This is the main entrance of the program int apientry WinMain (hinstance hinstance, hinstance hprevinstance, LPSTR lpCmdLine, int ncmdshow) {//Todo:place code Here.//dialogbox calls out Idd_dialog1, and its handler function is DLG1 callback Number, after the DLG1 callback function is called, the return value int dret = DialogBox (hinstance, (LPCSTR) IDD_DIALOG1,NULL,DLG1) is generated, or if the return value is IDCANCEL, the user clicks the " Close button, you can return 1 directly in WinMain, do not execute all of the following code if (dret==idcancel) {return-1;} Popup idd_dialog2, and its handler function is the DLG2 callback function DialogBox (HINSTANCE, (LPCSTR) idd_dialog2,null,dlg2); return 0;}

(1) This time because it is a two dialog box, so there will be two callback functions, that is, the dialog box processing function, and note that there will be two WinMain fourth parameter different DialogBox call statement.

(2) compared with the "MFC" Basic dialog Box Program-Adder (click to open the link) The code of the article, improved: EndDialog (Hwnddlg,idcancel), and then no need to return anything, this return does not have any meaning, The second argument to EndDialog is the value of the DialogBox invocation statement, and IDCANCEL is a global macro # define for an int that has a value of 2.

(3) if (Umsg==wm_command) {} More than one if condition structure can be changed to write switch-case-break; conditional structure, this look at personal programming habits.

(4) This time the value of the edit box, use GetDlgItemText instead of the "MFC" Basic dialog Box Program-Adder (click Open link) article of the Getdlgitemint, because this time the input is not a number.

(5) The MessageBox function has been in the "MFC" Helloworld (click to open the link) article and "MFC" For the dialog box Program optimization and for the program create icon "(click Open link) described in the article, here no longer repeat

"MFC" User Login with dialog box paging

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.