MFC: A simple example

Source: Internet
Author: User
The Code is as follows:

# Include <afxwin. h>

Class sample: Public cframewnd
{
Public:
Sample ()
{
Create (null, "MFC window ");
MessageBox ("My MFC window", "cframe constructor", mb_ OK );
}
};

Class app: Public cwinapp
{
Public:
Bool initinstance ();
Bool exitinstance ();
};

Bool app: initinstance ()
{
MessageBox (0, "My MFC window", "initinstance", mb_ OK | mb_iconasterisk );
Sample * OBJ;
OBJ = new sample;
M_pmainwnd = OBJ;
OBJ-> showwindow (sw_showmaximized );
Return true;
}

Bool app: exitinstance ()
{
MessageBox (0, "my window", "exitinstance", mb_ OK | mb_iconhand );
Return true;
}

APP;

You only need to copy the above Code and build a window32 project in the VC ++ 6.0 compiler, and compile it using the MFC Link Library.

Procedure: Open VC ++ 6.0 and click File (File)> new in the main menu to bring up a dialog box.

Win32 application (Win32 Application). In the project text box, name mymfc and click OK. Confirm

Click OK in the Information dialog box. In this way, a Win32 application project is created. Below we

Create a C ++ file in this project. Click File> New to bring up a dialog box. Select

C ++ source file (C ++ source file), and then name mymfc in the file text box. Click OK.

Compile the link. You will find three errors.

Nafxcwd. Lib (thrdcore. OBJ): Error lnk2001: unresolved external symbol _ endthreadex
Nafxcwd. Lib (thrdcore. OBJ): Error lnk2001: unresolved external symbol _ beginthreadex
Debug/mymfc.exe: Fatal error lnk1120: 2 unresolved externals

This is because the MFC class library is not used. Now we import. Click Project> setting settings.

In a dialog box, there is a drop-down list box, which is not using MFC. We will change her to using MFC in

Static library, click "OK", and then compile and run the file. Such a form appears. The following is an explanation of the program.

In the above program, only two classes cframewnd and cwinapp are used. Let's first look at the first class:

Class sample: Public cframewnd
{
Public:
Sample ()
{
Create (null, "MFC window ");
MessageBox ("My MFC window", "cframe constructor", mb_ OK );
}
};

The first class sample inherits the cframewnd class. The cframe class is a class in the MFC class library that represents the form framework. We first use the sample class to inherit it, when the constructor calls the create function, The cframewnd class function called during the create function enables the function written by MFC, cframewnd encapsulates the createwindow API function as its member function create (). Their parameters are similar. But you will ask, createwindow has 11 parameters, and here the create function only uses two parameters. It should be that the create here has two parameters as required, and the following parameters have default values.

According to the definition of msdn

Bool create (maid, DWORD dwstyle = empty, const rect & rect = rectdefault, cwnd * pparentwnd = NULL, maid = NULL, DWORD dwexstyle = 0, ccreatecontext * pcontext = NULL );

The following parameters all carry an equal sign and a default value.

Let's look at the second class, which inherits the app class of the cwinapp class.

Class app: Public cwinapp
{
Public:
Bool initinstance ();
Bool exitinstance ();
};

This class only overrides two functions, one initinstance () and one exitinstance (). This class controls the entire application, so it is called the cwinapp class, is an indispensable class. In addition, to run a program, you need to instantiate the class. Instantiation automatically calls the constructor and calls the initinstance () function (this function is written by MFC) because it is a virtual function, therefore, when instantiating an app class that inherits the cwinapp class, the APP: initinstance () will be automatically called (if you do not understand it, review the virtual functions of C ++ ), in this way, the process of an application instance is started. APP: initinstance () function

Bool app: initinstance ()
{
MessageBox (0, "My MFC window", "initinstance", mb_ OK | mb_iconasterisk );
Sample * OBJ;
OBJ = new sample;
M_pmainwnd = OBJ;
OBJ-> showwindow (sw_showmaximized );
Return true;
}

In this function, a message box is first used to use the MessageBox function, and then a sample class pointer obj is declared. In the third row, memory is allocated for the OBJ, that is, the object is instantiated, class instantiation calls the initialization of the constructor. The control point of the Program reaches the sample function of the sample class,

Sample ()
{
Create (null, "MFC window ");
MessageBox ("My MFC window", "cframe constructor", mb_ OK );
}

The form is created here, and a message box appears, and the control point of the program is returned to the m_pmainwnd = OBJ; position of APP: initinstance ().

This is a difficult part. At the beginning, I didn't understand m_pmainwnd. Where did you come from and what did you do.

He comes from the class cwinthread, and his definition is cwnd * m_pmainwnd;

Why is it used directly? Class cwinapp: Public cwinthread because the cwinapp class in MFC inherits from the member variables of the parent class in the cwinthread subclass, the son can use his father's money, of course, to pull, so he can directly use

What is his use? Let's see.

Sample * OBJ;
OBJ = new sample;

These two are declared in the initinstance () member function. That is to say, this function is over, and this pointer variable must be analyzed, and this pointer represents the form framework, the pointer is released, and the form disappears. Therefore, if we leave this address, we will give the m_pmainwnd pointer, which is in the Thread class, when a thread exists, the program ends, and no thread exists, the form framework ends.

Now we have created a form in the memory and it is not displayed.

OBJ-> showwindow (sw_showmaximized );

Through this sentence, we use pointers to call the class member functions. In cframewnd, we also encapsulate the showwindow API function, which is used in the same way as the API function.

After return true;, the function ends.

The program enters the running state. When the program is closed, it will call

Exitinstance () is a function that outputs only one message box and ends. So this simple MFC program will be mentioned here.

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.