One, Microsoft MFC-based programming method
MFC is the abbreviation for Microsoft Foundation Class Library . Unlike APIs, MFC is not part of the Windows operating system, but is a companion package developed by Microsoft for VC + +. Matching with VC6 is the MFC6.0. MFC uses inheritance and polymorphism techniques in object-oriented methods to encapsulate almost all of the standard parts involved in Windows applications, such as the architecture of Windows applications, the various standard graphical interface elements, and its core, which encapsulates windows in the form of C + +. Most functions of the API.
MFC is a very large class hierarchy (MFC6.0 contains more than 200 classes), it consists of a class called CObject as the base class, the rest of the class derives from this class, the whole class system can be roughly divided into:
Base class
The application Framework class. Includes: Application class, Command class, document/window class, Frame window class, document template class, etc.
The visual object class. Includes: Window class, dialog box class, control class, etc.
Drawing class. Includes: Drawing tool class, device Description table class and so on.
Simple data type class
Common classes include: Array class, Class table class, Image class
File and database classes include: File I/O classes, ODBC classes, ADO classes
Internet and network working classes include: Win32 Internet class, Windows socket class, etc.
Object Linking and Embedding (OLE, Object lingking and Embedding) classes
Debugging and Exception classes
In fact, MFC encapsulates every aspect of a Windows application operation, so by using MFC programming, programmers can focus on the parts of their programs that don't need to be done in person. This greatly reduces the number of code written by the programmer compared to the direct call API function, making programming easier, while the standard part of the program is provided by MFC's classes to make the program more standardized, more readable, and more efficient.
Second, the example explanation
Gossip not much, the following is the example of how to use VC + + implementation. The compiler I'm using is vc++6.0.
First know the steps:
There are usually four steps to developing an MFC project:
STEP.1 Building the project structure
STEP.2 Design Graphical User interface
STEP.3 event drivers for design objects
Compile, link, and run STEP.4 projects
The first step is to build the project structure,
The following series of default steps, finally appears:
The next step is to design the user graphical interface, which is to play the controls.
You can create (drag) the following controls on a dialog box:
1 edit box: For user input radius value;
5 static text boxes (static text boxes can only display text and cannot enter text), where: 2 static text boxes are used to display the circumference length and circle area respectively, and the remaining 3 static text boxes are used for text descriptions of the three text boxes.
2 command buttons, OK and exit: The former is used to determine the input values, calculate the circumference of the circle and the area of the circle, and then display the results on the corresponding output box; the latter is used to end the execution of the program.
Such as:
Make control property settings (that is, right-click the mouse-selected control, point properties):
1. main window: Set the caption value to "Calculate circle circumference and Circle area", other default.
2. Edit box: The default ID is: IDC_EDIT1. In the Styles tab, set the relevant property value to: left. The remaining properties take the default values.
3. Static text box:
ID Header Other Properties
---------------------------------------------------------------------------------
Idc_static1 Input RADIUS Center, center vertically
Idc_static2 circumference Long center, vertically centered
IDC_STATIC3 Circle area centered, vertically centered
Idc_static4 default left, vertical center, Client Edge
Idc_static5 default left, vertical center, Client Edge
4. Button:
ID Header
---------------------------------------
Idc_button1 OK
Idc_button2 exit
Now it's the third step to design the object's event driver.
Here's some knowledge. The event driver for an object is called a message processing member function in VC + +.
In real-world applications, reading or updating data on a control is a very important task for a dialog box. MFC provides a standard set of methods for this: DDX (data exchange) and DDV (data Validation, checksum) technology. DDX implements the data passing between the dialog box and the control through the member variable (member variable). If you want to access the data on the control in the message handler of the dialog box, you must first add (declare) a member variable for the control in the dialog class (Ccase1dlg Class) , each of which binds a control. The control is accessed through member variables in the message-handler function.
A member variable is a public data member of a dialog box. There are two types of member variables:
Value class member Variable: Represents the value of the variable for the control's data.
Control class member variable: means that the member variable represents the control itself, that is, it is an object of the control, and its value is actually a handle to the control.
What is a handle?
The Visual studio system assigns a handle (handle) to each object, which is a 4 -byte long integer value that is the unique internal number of the object . The application accesses the corresponding object through a handle. Each object, in addition to the handle, has an object identifier ID that can be thought of as the external name of the object. the difference between an ID and a handle is that the ID can be set by the user, and the handle is system-default and transparent to the user, which prevents the application from accessing the object's internal information directly.
DDV is used for data validation, such as automatic checking of string lengths and range of values.
The process for using MFC DDX/DDV is:
① first defines the member variables that are used to receive control data. For example, a member variable of type double of the value class is defined for the edit box idc_edit1, which is used to receive the RADIUS value entered by the user. Defining variables can also set the range of data values and provide a checksum.
② then invokes the MFC function in the corresponding message-handling member function to pass the data. The simplest related MFC functions are:
UpdateData () //Update member variable to pass data on control to member variable
UpdateData (FALSE) //Update the control to pass the value of the member variable to the control
For this example, you can design the following member variables and message handler functions:
Defines a double-type member variable m_r for the edit box idc_edit1 to receive the RADIUS value entered by the user in the edit box;
Define a CString member variable M_girth and m_area for static text boxes Idc_static4 and idc_static5 to pass the calculation results to the static text box. static text boxes can only handle Cstring (string) type data .
Note: The prefix "m_" of the member variable name is the style of MFC. MFC also has its own data type, but is similar to C + + and is easy to understand.
Establish a click (bn_clicked) Message handler function for the OK button to calculate the perimeter and circle area of the circle and display the results of the calculation.
Set up a "click" Message handler function for the Exit button to end the run of the program.
Create a dialog initialization function to initialize the member variables M_r, M_girth, M_area.
Operation Steps:
(1) Adding member variables
Opens the MFC Class Wizard dialog box. You can do this by selecting the Create Class Wizard option from the View menu on the VC6 window's menu bar, or by right-clicking the form, choosing the Create Class Wizard from the popup shortcut menu, or pressing the shortcut key ctrl+w. Select the Member Variables (member variable) tab. From the Control IDs list box, you can see the IDs of each control object that you have established.
To add a member variable to the edit box: Select Idc_edit1, click on the "Add Variable ..." button on the right, and the Add Member Variable dialog box pops up.
Enter the member variable name in the first text box (Member variable name), such as M_r, select the type of member variable in the second drop-down box (category), Value , in the third drop-down box (variable Type) Select the data type of the member variable, double. Click the OK button. Go back to the Member Variables tab and set the range of values for the variable m_r, such as a minimum value of 0 and a maximum value of 1000.0. Add CString member variables M_girth and M_area in the same way for static text boxes Idc_static4 and idc_static5, with a maximum number of characters of 10.
See:
(2) Writing message processing member functions
① the initialization function of the main window
Switch to the "message map" tab of the Class Wizard. Select the object name of the main window in the "Object IDs" list box Ccase1dlg; Select a message in the "Messages" (Message) list box Wm_initdialog; The corresponding MFC-defined virtual function OnInitDialog is automatically indicated in the Member functions (member function) list box (the main Window object is generated by MFC by default).
Click the Edit Code button to pop up the edit window for the Case1Dlg.cpp file and display the code listing for the OnInitDialog () function to find the comment prompt:
// Todo:add Extra Initialization here (Add the code you initialized below)
For this example, you can add the following code:
M_r =0.0; M_girth=m_area="0.000"// Pass data to the control and display
That
BOOL Ccase1dlg::oninitdialog () { cdialog::oninitdialog (); ... ...... // todo:add Extra initialization here M_r =0.0; M_girth=m_area="0.000"; // pass data to the control and display return TRUE; // return TRUE unless you set the focus to a control}
② the message handler function for the OK button
Similar to the above method, select Idc_button1 in the "Object IDs" list box , Select the message bn_clicked in the "Messages" list box , and click "Add Function" (Add function) button, a new function name OnButton1 appears in the Member Functions list box, indicating that a new member function was generated for the dialog box (figure); Click the Edit Code button to eject Case1Dlg.cpp The edit window of the file and displays the code listing for the OnButton1 () function, and find the comment prompt:
// Todo:add your control notification handlercode here (add the processing codes for the control messages below)
For this example, you can add the following code:
UpdateData (); // double girth=2 * 3.1416 *m_r; // calculate circle perimeter double area=// calculates the circle area M_girth. Format ("%10.3f ", girth); // write member variable m_area in format. Format ( %10.3f " ,area); Updatedtata (FALSE); // pass data to the control and display
Note: the function Fornat () is a member function of the CString class that is used to convert the data into a string of type CString.
③ Message handler function for "Exit" button
Set up the Idc_button2 bn_clicked message handler function as described above, and enter the code at the comment prompt:
void Ccase1dlg::onbutton2 () { // Todo:add your control notification handler codehere
EndDialog (1); // Close the dialog box }
Following the final step, the project compiles, links, and runs.
First MFC instance: calculating the perimeter and circle area of a circle