Relationship between the MFC program and the Win32 Program

Source: Internet
Author: User
Relationship between the MFC program and the Win32 Program

MFC encapsulates winmain internal operations with relatively fixed behaviors in cwinapp, and wndproc internal operations with relatively fixed behaviors in cframewnd.

It can be said that cwinapp is used to replace winmain's position in the SDK program, and cframewnd replaces the position of window functions in the SDK program.

First, the MFC program needs the following function library:

(1) Windows C Runtime function library: libc. lib/msvcrt. lib/msvcrtd. Lib

(2) DLL import function library: gdi32.lib/user32.lib/kernel32.lib

(3) MFC function library (afx function library): mfc42.lib/mfc42d. Lib ......

At the same time, the MFC program needs the following header file:

(1) stdafx. h: Pre-compiled header file, which only loads other MFC header files.

(2) afxwin. h: Each MFC program must load it because it and the file it loads declare all the MFC classes. This file contains afx. h. The latter loads afxvver _. h, the latter is loaded into afxv_w32.h, and the latter is loaded into windows. H (the header file required by the SDK program ).

(3) afxext. H: This file must be loaded by programs using the toolbar and status bar.

(4) afxdlgs. H: This file is required by the MFC program that uses the general-purpose dialog box and is loaded internally into commdlg. h.

(5) afxcen. H: This file must be loaded in the MFC program that uses the general-purpose control added by Win9x.

(6) afxcoll. H: The program that uses collections classes wants this file

(7) afxres. h: the RC file of the MFC program must be loaded.

[Explanation] pre-compiled header file: the pre-compiled header file stores the results after the first compilation of the. h file. It can be obtained directly from the disk during 2nd compilation.

The relationship between a specific MFC program and Win32:

The MFC program is also a Windows program, so it should have a winmain, but before the program enters the vertex, there is a global object theapp, which is called an application object. When the operating system loads and activates the program, the global object is configured, and its constructor is executed first, winmain is earlier.

Cmywinapp theapp;

Theapp is the application object of the program. Each MFC program has only one such object. When the program is executed, the global object is generated and the constructor is executed. Generally, cwinapp constructor is executed. The member variables in cwinapp will get the configuration and initial values because theapp is a global object.

After theapp configuration is complete, winmain came into play, and we did not write the winmain program code. This is why MFC has been prepared and the connector is directly added to the application code. Add the following code:

Extern "C" int winapi _ twinmain (hinstance, hinstance hprevinstance, lptstr lpcmdline, int ncmdshow)

{

Return afxwinmain (hinstance, hprevinstance, lpcmdline, ncmdshow );

}

Afxwinmain is defined as follows:

Int afxapi afxwinmain (hinstance, hinstance hprevinstance, lptstr lpcmdline, int ncmdshow)

{

Int nreturncode =-1;

Cwinapp * PAPP = afxgetapp ();

Afxwininit (hinstance, hprevinstance, lpcmdline, ncmdshow );

PAPP-> initapplication ();

PAPP-> initinstance ();

Nreturncode = PAPP-> Run ();

Afxwinterm ();

Return nreturncode;

}

The following describes four main operations of afxwinmain:

(1) afxwininit -- afx internal initialization operation. It is the first operation after the cwinapp constructor. Some members of theapp are initialized in this function, and the afxinitthread function is also called to increase the number of message queues to 96 as much as possible.

(2) cwinapp: initapplication. Initapplication is a virtual function of cwinapp. Generally, it does not need to be rewritten. The operations in this function are performed by MFC for internal management (docmanager-related ).

(3) cmywinapp: initinstance. Every MFC program should rewrite the cwinapp: initinstance function, because it is only an empty function in cwinapp. In this function, the Code is as follows:

Bool cmywinapp: initinstance ()

{

M_pmainwnd = new cmyframewnd ();

M_pmainwnd-> showwindow (m_ncmdshow );

M_pmainwnd-> updatewindow ();

Return true;

}

At the beginning, a cmyframewnd object is added and ready to be used as the C ++ object of the main box window. This triggers the constructor of cmyframewnd:

Cmyframewnd: cmyframewnd

{

Create (null, "Hello MFC", ws_overlappedwindow, rectdefault, null, "mainmenu ");

}

Create is a member function of cframewnd, which is not rewritten in cmyframewnd. Because the first parameter of create specifies the wndclass window class name, null indicates that a standard window is generated using the built-in window class of MFC.

In cframewnd: Create (...) The createex function is called in the function. Since the createex function is not rewritten in cframewnd, The cwnd: createex function is actually called. In the createex function, the precreatewindow and createmediawex functions are called. Since precreatewindow is rewritten in cframewnd, cframewnd: precreatewindow is called here. In precreatewindow, The afxdeferregisterclass macro is called. The macro is defined as follows:

# Define afxdeferregisterclass (fclass) \ (afxregisteredclasses & fclass) true: afxenddeferregisterclass (fclass ))

If the value of the afxregisteredclasses variable shows that the system has registered a window class such as fclass, MFC will do nothing. Otherwise, afxenddeferregisterclass (fclass) will be called to register it. The afxenddeferregisterclass function finally declares the wndclass object, calls the corresponding MFC window class, and registers the window class through the afxregisterclass (wndclass *) and registerwithicon functions. MFC has six built-in window classes. Registerwithicon uses the afxregisterclass function, while the afxregisterclass function calls the registerclass function to register the window class. The precreatewindow member functions of different classes are called at the moment before the window is generated. They are used to register the window class. If we specify that the window class is null, the default system class (built-in MFC) is used ).

After the window is created, the program process returns to cmywinapp: initinstance. Therefore, the showwindow function is called to display the window and the updatewindow function is called to send the wm_paint message.

(4) cwinapp: Run. Because run is not rewritten in cmywinapp (in most cases, it is not required to be rewritten), cwinapp: Run is called. In this function, cwinthread: Run, cwinthread: Run is called to call the pumpmesage () function, while the pumpmessage function calls the getmessage/translatemessag/dispatchmessage function in sequence. The program sends messages to the window function by calling the dispatchmessage function.

(5) The program judges the message based on the message ing table to process the corresponding message.

At this point, the relationship between the MFC program and the Win32 program is finally understood.

MFC encapsulates winmain internal operations with relatively fixed behaviors in cwinapp, and wndproc internal operations with relatively fixed behaviors in cframewnd.

It can be said that cwinapp is used to replace winmain's position in the SDK program, and cframewnd replaces the position of window functions in the SDK program.

First, the MFC program needs the following function library:

(1) Windows C Runtime function library: libc. lib/msvcrt. lib/msvcrtd. Lib

(2) DLL import function library: gdi32.lib/user32.lib/kernel32.lib

(3) MFC function library (afx function library): mfc42.lib/mfc42d. Lib ......

At the same time, the MFC program needs the following header file:

(1) stdafx. h: Pre-compiled header file, which only loads other MFC header files.

(2) afxwin. h: Each MFC program must load it because it and the file it loads declare all the MFC classes. This file contains afx. h. The latter loads afxvver _. h, the latter is loaded into afxv_w32.h, and the latter is loaded into windows. H (the header file required by the SDK program ).

(3) afxext. H: This file must be loaded by programs using the toolbar and status bar.

(4) afxdlgs. H: This file is required by the MFC program that uses the general-purpose dialog box and is loaded internally into commdlg. h.

(5) afxcen. H: This file must be loaded in the MFC program that uses the general-purpose control added by Win9x.

(6) afxcoll. H: The program that uses collections classes wants this file

(7) afxres. h: the RC file of the MFC program must be loaded.

[Explanation] pre-compiled header file: the pre-compiled header file stores the results after the first compilation of the. h file. It can be obtained directly from the disk during 2nd compilation.

The relationship between a specific MFC program and Win32:

The MFC program is also a Windows program, so it should have a winmain, but before the program enters the vertex, there is a global object theapp, which is called an application object. When the operating system loads and activates the program, the global object is configured, and its constructor is executed first, winmain is earlier.

Cmywinapp theapp;

Theapp is the application object of the program. Each MFC program has only one such object. When the program is executed, the global object is generated and the constructor is executed. Generally, cwinapp constructor is executed. The member variables in cwinapp will get the configuration and initial values because theapp is a global object.

After theapp configuration is complete, winmain came into play, and we did not write the winmain program code. This is why MFC has been prepared and the connector is directly added to the application code. Add the following code:

Extern "C" int winapi _ twinmain (hinstance, hinstance hprevinstance, lptstr lpcmdline, int ncmdshow)

{

Return afxwinmain (hinstance, hprevinstance, lpcmdline, ncmdshow );

}

Afxwinmain is defined as follows:

Int afxapi afxwinmain (hinstance, hinstance hprevinstance, lptstr lpcmdline, int ncmdshow)

{

Int nreturncode =-1;

Cwinapp * PAPP = afxgetapp ();

Afxwininit (hinstance, hprevinstance, lpcmdline, ncmdshow );

PAPP-> initapplication ();

PAPP-> initinstance ();

Nreturncode = PAPP-> Run ();

Afxwinterm ();

Return nreturncode;

}

The following describes four main operations of afxwinmain:

(1) afxwininit -- afx internal initialization operation. It is the first operation after the cwinapp constructor. Some members of theapp are initialized in this function, and the afxinitthread function is also called to increase the number of message queues to 96 as much as possible.

(2) cwinapp: initapplication. Initapplication is a virtual function of cwinapp. Generally, it does not need to be rewritten. The operations in this function are performed by MFC for internal management (docmanager-related ).

(3) cmywinapp: initinstance. Every MFC program should rewrite the cwinapp: initinstance function, because it is only an empty function in cwinapp. In this function, the Code is as follows:

Bool cmywinapp: initinstance ()

{

M_pmainwnd = new cmyframewnd ();

M_pmainwnd-> showwindow (m_ncmdshow );

M_pmainwnd-> updatewindow ();

Return true;

}

At the beginning, a cmyframewnd object is added and ready to be used as the C ++ object of the main box window. This triggers the constructor of cmyframewnd:

Cmyframewnd: cmyframewnd

{

Create (null, "Hello MFC", ws_overlappedwindow, rectdefault, null, "mainmenu ");

}

Create is a member function of cframewnd, which is not rewritten in cmyframewnd. Because the first parameter of create specifies the wndclass window class name, null indicates that a standard window is generated using the built-in window class of MFC.

In cframewnd: Create (...) The createex function is called in the function. Since the createex function is not rewritten in cframewnd, The cwnd: createex function is actually called. In the createex function, the precreatewindow and createmediawex functions are called. Since precreatewindow is rewritten in cframewnd, cframewnd: precreatewindow is called here. In precreatewindow, The afxdeferregisterclass macro is called. The macro is defined as follows:

# Define afxdeferregisterclass (fclass) \ (afxregisteredclasses & fclass) true: afxenddeferregisterclass (fclass ))

If the value of the afxregisteredclasses variable shows that the system has registered a window class such as fclass, MFC will do nothing. Otherwise, afxenddeferregisterclass (fclass) will be called to register it. The afxenddeferregisterclass function finally declares the wndclass object, calls the corresponding MFC window class, and registers the window class through the afxregisterclass (wndclass *) and registerwithicon functions. MFC has six built-in window classes. Registerwithicon uses the afxregisterclass function, while the afxregisterclass function calls the registerclass function to register the window class. The precreatewindow member functions of different classes are called at the moment before the window is generated. They are used to register the window class. If we specify that the window class is null, the default system class (built-in MFC) is used ).

After the window is created, the program process returns to cmywinapp: initinstance. Therefore, the showwindow function is called to display the window and the updatewindow function is called to send the wm_paint message.

(4) cwinapp: Run. Because run is not rewritten in cmywinapp (in most cases, it is not required to be rewritten), cwinapp: Run is called. In this function, cwinthread: Run, cwinthread: Run is called to call the pumpmesage () function, while the pumpmessage function calls the getmessage/translatemessag/dispatchmessage function in sequence. The program sends messages to the window function by calling the dispatchmessage function.

(5) The program judges the message based on the message ing table to process the corresponding message.

At this point, the relationship between the MFC program and the Win32 program is finally understood.

MFC encapsulates winmain internal operations with relatively fixed behaviors in cwinapp, and wndproc internal operations with relatively fixed behaviors in cframewnd.

It can be said that cwinapp is used to replace winmain's position in the SDK program, and cframewnd replaces the position of window functions in the SDK program.

First, the MFC program needs the following function library:

(1) Windows C Runtime function library: libc. lib/msvcrt. lib/msvcrtd. Lib

(2) DLL import function library: gdi32.lib/user32.lib/kernel32.lib

(3) MFC function library (afx function library): mfc42.lib/mfc42d. Lib ......

At the same time, the MFC program needs the following header file:

(1) stdafx. h: Pre-compiled header file, which only loads other MFC header files.

(2) afxwin. h: Each MFC program must load it because it and the file it loads declare all the MFC classes. This file contains afx. h. The latter loads afxvver _. h, the latter is loaded into afxv_w32.h, and the latter is loaded into windows. H (the header file required by the SDK program ).

(3) afxext. H: This file must be loaded by programs using the toolbar and status bar.

(4) afxdlgs. H: This file is required by the MFC program that uses the general-purpose dialog box and is loaded internally into commdlg. h.

(5) afxcen. H: This file must be loaded in the MFC program that uses the general-purpose control added by Win9x.

(6) afxcoll. H: The program that uses collections classes wants this file

(7) afxres. h: the RC file of the MFC program must be loaded.

[Explanation] pre-compiled header file: the pre-compiled header file stores the results after the first compilation of the. h file. It can be obtained directly from the disk during 2nd compilation.

The relationship between a specific MFC program and Win32:

The MFC program is also a Windows program, so it should have a winmain, but before the program enters the vertex, there is a global object theapp, which is called an application object. When the operating system loads and activates the program, the global object is configured, and its constructor is executed first, winmain is earlier.

Cmywinapp theapp;

Theapp is the application object of the program. Each MFC program has only one such object. When the program is executed, the global object is generated and the constructor is executed. Generally, cwinapp constructor is executed. The member variables in cwinapp will get the configuration and initial values because theapp is a global object.

After theapp configuration is complete, winmain came into play, and we did not write the winmain program code. This is why MFC has been prepared and the connector is directly added to the application code. Add the following code:

Extern "C" int winapi _ twinmain (hinstance, hinstance hprevinstance, lptstr lpcmdline, int ncmdshow)

{

Return afxwinmain (hinstance, hprevinstance, lpcmdline, ncmdshow );

}

Afxwinmain is defined as follows:

Int afxapi afxwinmain (hinstance, hinstance hprevinstance, lptstr lpcmdline, int ncmdshow)

{

Int nreturncode =-1;

Cwinapp * PAPP = afxgetapp ();

Afxwininit (hinstance, hprevinstance, lpcmdline, ncmdshow );

PAPP-> initapplication ();

PAPP-> initinstance ();

Nreturncode = PAPP-> Run ();

Afxwinterm ();

Return nreturncode;

}

The following describes four main operations of afxwinmain:

(1) afxwininit -- afx internal initialization operation. It is the first operation after the cwinapp constructor. Some members of theapp are initialized in this function, and the afxinitthread function is also called to increase the number of message queues to 96 as much as possible.

(2) cwinapp: initapplication. Initapplication is a virtual function of cwinapp. Generally, it does not need to be rewritten. The operations in this function are performed by MFC for internal management (docmanager-related ).

(3) cmywinapp: initinstance. Every MFC program should rewrite the cwinapp: initinstance function, because it is only an empty function in cwinapp. In this function, the Code is as follows:

Bool cmywinapp: initinstance ()

{

M_pmainwnd = new cmyframewnd ();

M_pmainwnd-> showwindow (m_ncmdshow );

M_pmainwnd-> updatewindow ();

Return true;

}

At the beginning, a cmyframewnd object is added and ready to be used as the C ++ object of the main box window. This triggers the constructor of cmyframewnd:

Cmyframewnd: cmyframewnd

{

Create (null, "Hello MFC", ws_overlappedwindow, rectdefault, null, "mainmenu ");

}

Create is a member function of cframewnd, which is not rewritten in cmyframewnd. Because the first parameter of create specifies the wndclass window class name, null indicates that a standard window is generated using the built-in window class of MFC.

In cframewnd: Create (...) The createex function is called in the function. Since the createex function is not rewritten in cframewnd, The cwnd: createex function is actually called. In the createex function, the precreatewindow and createmediawex functions are called. Since precreatewindow is rewritten in cframewnd, cframewnd: precreatewindow is called here. In precreatewindow, The afxdeferregisterclass macro is called. The macro is defined as follows:

# Define afxdeferregisterclass (fclass) \ (afxregisteredclasses & fclass) true: afxenddeferregisterclass (fclass ))

If the value of the afxregisteredclasses variable shows that the system has registered a window class such as fclass, MFC will do nothing. Otherwise, afxenddeferregisterclass (fclass) will be called to register it. The afxenddeferregisterclass function finally declares the wndclass object, calls the corresponding MFC window class, and registers the window class through the afxregisterclass (wndclass *) and registerwithicon functions. MFC has six built-in window classes. Registerwithicon uses the afxregisterclass function, while the afxregisterclass function calls the registerclass function to register the window class. The precreatewindow member functions of different classes are called at the moment before the window is generated. They are used to register the window class. If we specify that the window class is null, the default system class (built-in MFC) is used ).

After the window is created, the program process returns to cmywinapp: initinstance. Therefore, the showwindow function is called to display the window and the updatewindow function is called to send the wm_paint message.

(4) cwinapp: Run. Because run is not rewritten in cmywinapp (in most cases, it is not required to be rewritten), cwinapp: Run is called. In this function, cwinthread: Run, cwinthread: Run is called to call the pumpmesage () function, while the pumpmessage function calls the getmessage/translatemessag/dispatchmessage function in sequence. The program sends messages to the window function by calling the dispatchmessage function.

(5) The program judges the message based on the message ing table to process the corresponding message.

At this point, the relationship between the MFC program and the Win32 program is finally understood.

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.