Analysis of a simple MFC Program Framework

Source: Internet
Author: User

First, create a Win32 Application in VC (I use vc2008 ).ProgramAnd select an empty project. After the project is created, select "Project Properties"> "configuration properties"> "general"> "use of MFC ".

Select to use MFC in the shared DLL. Currently, this empty project does not contain any files.

Create a new. h file: MyApp. H, and add the followingCode:

// Application class
Class Cmyapp: Public Cwinapp
{
Public :
Virtual Bool initinstance ();
};

// Frame window class
Class Cmyframe: Public Cframewnd
{
Public :
Cmyframe ();
Protected :
// "Afx_msg" indicates that the next two functions are part
// Of the MFC Library message dispatch system
Afx_msg Void Onlbuttondown (uint nflags, cpoint point );
Afx_msg Void Onpaint ();
Declare_message_map ()
};

Create a MyApp. cpp file and add the following code:

# Include < Afxwin. h >   // MFC Library header file declares base classes
# Include " MyApp. h "
 
Cmyapp theapp; // The one and only cmyapp object
 
Bool cmyapp: initinstance ()
{
M_pmainwnd =   New Cmyframe ();
M_pmainwnd -> Showwindow (m_ncmdshow );
 
M_pmainwnd -> Updatewindow ();
Return True;
}

Begin_message_map (cmyframe, cframewnd)
On_wm_lbuttondown ()
On_wm_paint ()
End_message_map ()

Cmyframe: cmyframe ()
{
Create (null, _ T ( " MyApp Application " ));
}
 
Void Cmyframe: onlbuttondown (uint nflags, cpoint point)
{
Afxmessagebox (_ T ( " Left button clicked! " ));
/*
Trace ("entering cmyframe: onlbuttondown-% lx, % d, % d \ n ",
(Long) nflags, point. X, point. y );
*/
}
 
Void Cmyframe: onpaint ()
{
Cpaintdc DC ( This );
DC. textout ( 0 , 0 , " Hello, world! " );
}

CTRL + F5 run the program, which is the simplest window and draws a string: "Hello World !", At the same time, click the left mouse button to bring up a MessageBox. This is a self-built MFC application without the Application Wizard, But we inherit the basic class of MFC, that is, the program under the application framework of MFC.

 

The following describes the program framework:
1, Winmain Function : Each windows application requires an entry function, namely, the winmain function. But it is already hidden in the MFC application framework.

2,Cmyapp: The object of this class represents this application and defines the Global Object theapp. The basic cwinapp determines theapp's main behavior.

3,Application startup: When starting an application, you first need to construct the Global Object theapp, enter the winmain function, and winmain looks for a global application object derived from cwinapp.

4,Member function cmyapp: initinstance: A virtual function. This function is also available in the base class cwinapp. When the winmain function finds the application object theapp, it calls its virtual member function initinstance. This function is called to construct and display the main window of the application. The derived application class (cmyapp) must overwrite the initinstance function. The function in the base class (cwinapp) does not know what kind of main window we want.

5,Member function cwinapp: Run: This function is a member of the cwinapp class. It is used to send application messages to its window, which ensures that the application runs continuously. After winmain calls initinstance, it calls the run function.

6,Cmyframe class: An object of this class represents the main framework window of the application. When the constructor calls the member function create of the base class cframewnd, Windows creates the actual window structure. Showwindow and updatewindow functions are also member functions of the base class. They are called to display the window.

7,Cmyframe: onlbuttondown Function: The response function when the left-click message on_wm_lbuttondown is triggered. It is a member function of cmyframe (note that it is not a member function of the base class, that is, it is not a virtual function, it uses a macro to map a specific message to a member function of the derived class. The reason why MFC does not use virtual functions for Windows messages is that only cwnd requires more than 100 messages, and each message defines a virtual response function, therefore, each derived class must have a virtual function scheduling table, which is too complicated even if most messages are not used ).

8,Cmyframe: onpaint Function: Each time the window needs to be re-painted, the application framework calls the onpaint function of cmyframe. Cpaintdc is a class related to GDI. Finally, hello World! is displayed !.

9,Close the program: Close the main window and close the program. Related Events: Analysis of cmyframe, exit of run function, exit of winmain function, and destructor of cmyapp object.

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.