"MFC" Basic dialog Box program--Adder

Source: Internet
Author: User

All agreed to be VC6 MFC, this adder is certainly different from the number of electric and SCM Adder, ^_^ a joke,

Formally transferred to the subject,

The first two MFC, even MFC is not, just Win32 program, are just a normal pop-up window program,

The simple Win32 program that continues to bring you this time,

But already has the basic function, can realize two edit box inside the data to add.


I. BASIC OBJECTIVES

Create the following program:


There are two systems with the button, originally "OK" and "Cancel", now instead of "calculate" and "close"

The Calculate button adds the numbers from the two editable dialog boxes and outputs them to the Third dialog box.

The Close button will naturally close the dialog box.


Second, the production process

1, first create a simple Win32 program, and create a resource script inside, this How to create please refer to my previous "MFC" For the dialog box Program optimization and create a program icon "(click Open link) article, the only difference is that this time in the resource script is inserted in the dialog box, not the icon, of course , for a more beautiful program, you can refer to the above to add an icon to it.


Dialog resources There are sub-options, there is no need to select, select the parent Resource dialog, select "New" to complete the creation


2. After selecting "new" to complete the creation, the following interface will appear:


First drag the "OK" and "Cancel" buttons that come with the system to the bottom, click the buttons and change their caption to "OK" and "Cancel"

In this interface, through the Control Toolbox on the right, you can drag out the controls you need, with the control toolbar below to arrange the position of the control, click on any of the controls to set the corresponding properties,

For example, click on a static text, by editing its caption, you can change the "static" to "+"


At the same time, please record the ID of each control, in a moment when writing code, it is possible to use, need to control by ID

After dragging the wrong control, select, press DELETE

When you are done, click on the ClassView tab at the bottom left and go back to the WinMain function, as in "MFC" Helloworld (click the Open link) to write the code.


3. Finally, the controls inside the dialog box are arranged as shown:


Remember:

(1) The system comes with the two button caption from "OK" to "calculation", from "Cancel" to "off",

(2) for three EditField edit boxes, change the style of the edit box on the right to "read Only"

Click on this edit box, on the Style tab, put the "read only" tick on the


(3) For the entire large dialog box, modify the caption in the general to have it titled "Addition Calculator" or something else you like, and check the "center" attribute in "more styles" so that you open the program, the dialog will appear in the center of the screen instead of the upper left corner.



4, through the ClassView tab, go back to the WinMain function to write the following code:

Dialogbox.cpp:Defines the entry point for the application.//#include "stdafx.h"// First remember to introduce resource.h, because our dialog box is created in the resource script include "resource.h"//This callback function Mainproc is the function of the dialog box, not the real entrance, is called by the DialogBox statement in the WinMain,//4 form parameters, the return value bool CALLBACK are system requirements, cannot change bool CALLBACK mainproc (HWND Hwnddlg,//Handle of Dialog Boxuint umsg,//Messagewparam WParam,//First message Parameterlparam LParam//Second message parameter) {//If there is a button Click if (umsg==wm_command) {//If the IDCANCEL button is clicked, the id "IDCANCEL" is the "Close button" if (LoWord (WParam) ==idcancel) {//Then we end the dialog box. EndDialog (hwnddlg,idcancel);//This true is not much different from false, at least it has no effect on what the program shows, and true is that we write code to deal with the following things, false is the system to process the subsequent system. return true;} If you click on the IDOK button, the id "IDOK" is the "Calculate button" if (LoWord (WParam) ==idok) {//Get idc_edit1 edit box value, upload to reshape edit1,getdlgitemint the rest of the parameters for the specified action , it is said that Hwnddlg is a window handle, and true is to take a signed number, not an unsigned number, but do you know what that means? int edit1 = Getdlgitemint (hwnddlg,idc_edit1,null,true);//Get idc_edit2 edit box value, upload to reshape edit2,getdlgitemint the remaining parameters for the specified action int Edit2 = Getdlgitemint (hwnddlg,idc_edit2,null,true);//Set Idc_edit3 the value in the Read-only edit box is Edit1+edit2 value SetdlgiTemint (hwnddlg,idc_edit3,edit1+edit2,true); return true;}} 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.//call the DialogBox parameter, the second value must be the dialog box we want to pop up idd_d IALOG1, that is, just in the resource script to draw the dialog box, forced conversion to (LPCSTR) to normal compilation,//The fourth parameter means that the dialog box is processed by the function of Mainprocdialogbox (HINSTANCE, (LPCSTR) idd_ DIALOG1,NULL,MAINPROC); return 0;}

The logic of the whole program is to start into the WinMain function, then return the value of the bool callback type Mainproc is called, after processing a lot of logic, complete the program, it is so simple.

"MFC" Basic dialog Box program--Adder

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.