VS2010/MFC Programming Primer IV (MFC Application Framework Analysis)-Software development-chicken peck rice
Http://www.jizhuomi.com/software/145.html
On a chicken peck rice is VS2010 the composition of the file in the application engineering, it may be that the operation of the principle of the project is still very vague, not a clue, after all, with the introduction of C + + programming in the series of routine differences too large. This section of Chicken Peck Rice for you to analyze the operation of the MFC application framework process.
I. SDK application vs. MFC application running Process
The program runs with the entry function, which is the main function in the previous C + + tutorial, and the entry function of the Windows application is the WinMain function, and the MFC program starts with the WinMain function. Below the chicken peck rice gives a "HelloWorld" program written in the Windows SDK, which contrasts with the application framework to better understand how the framework works. The Windows SDK developer does not use the MFC class Library to develop software directly with Windows API functions. Chicken Peck rice is not to explain the SDK development, just for comparison and simple introduction, as for the SDK development can be after you learn MFC choose whether to study, generally have a simple understanding on it.
SDK Application
First, give the source code for the Windows SDK application "HelloWorld":
#include <windows.h>LRESULT CALLBACK Mywndproc (HWND Hwindow, UINT msg, WPARAM WPARAM, LPARAM LPARAM); intWINAPI WinMain (hinstance hinstance, hinstance hprevinstance, PSTR szCmdLine,inticmdshow) { Const StaticTCHAR appname[] = TEXT ("Hello World"); Wndclassex Mywin; Mywin.cbsize=sizeof(Mywin); Mywin.style= Cs_hredraw |Cs_vredraw; Mywin.lpfnwndproc=Mywndproc; Mywin.cbclsextra=0; Mywin.cbwndextra=0; Mywin.hinstance=hinstance; Mywin.hicon=0; MYWIN.HICONSM=0; Mywin.hcursor=0; Mywin.hbrbackground= (Hbrush) (Color_window +1); Mywin.lpszmenuname=0; Mywin.lpszclassname=AppName; //Registerif(! RegisterClassEx (&mywin))return 0; ConstHWND Hwindow =CreateWindow (AppName, AppName, Ws_overlappedwindow, Cw_usedefault, Cw_ Usedefault, Cw_usedefault, Cw_usedefault,0, 0, HINSTANCE,0); ShowWindow (hwindow,icmdshow); UpdateWindow (Hwindow); {msg msg; while(GetMessage (&msg,0,0,0) {translatemessage (&msg); DispatchMessage (&msg); } return(int) Msg.wparam; }} LRESULT CALLBACK Mywndproc (HWND Hwindow, UINT msg, WPARAM WPARAM, LPARAM LPARAM) {if(msg==wm_paint) {paintstruct ps; ConstHDC hdc = BeginPaint (hwindow,&PS); Rect rect; GetClientRect (Hwindow,&rect); DrawText (Hdc,text ("HELLO World"),-1, &rect, Dt_singleline | Dt_center |dt_vcenter); EndPaint (Hwindow,&PS); return 0; } Else if(msg==Wm_destroy) {PostQuitMessage (0); return 0; } returnDefWindowProc (Hwindow,msg,wparam,lparam); }
The process of running the above program is: Enter WinMain function, initialize wndclassex, call the RegisterClassEx Function Registration window class, call ShowWindow and UpdateWindow function display and update window, Enter the message loop. About message loops Simply put, a Windows application is message-driven, a system or user sends a message when the application does something or completes a task, enters the program's message queue, and then the message loop takes the message out of the message queue and hands it to the corresponding window process. The window procedure function of this program is the Mywndproc function, which completes an operation or task when the window procedure function finishes processing the message. In this example, to display the "Hello World" string, the UpdateWindow function sends a WM_PAINT message, but the message is sent directly to the window procedure processing without a message queue, and the "Hello World" string is eventually drawn in the window procedure function.
MFC applications
The following is a running process for an MFC application that is parsed through the code in the MFC library:
First, define the global object Theapp:chelloworldapp Theapp in HelloWorld.cpp. After calling the constructors for CWinApp and Chelloworldapp, enter the WinMain function (located in Appmodul.cpp).
extern " C " int WINAPI _tWinMain (hinstance hinstance, hinstance hprevinstance, int ncmdshow) #pragma{ //return afxwinmain (HINSTANCE, hPrevInstance, lpCmdLine, ncmdshow); }
In TCHAR.h, there is this definition: #define _tWinMain WinMain, so the _twinmain here is the WinMain function. It calls the Afxwinmain function (located in WinMain.cpp).
intAFXAPI Afxwinmain (hinstance hinstance, hinstance hprevinstance,lptstr lpCmdLine,intncmdshow) { ............. Slightly//App global initializations (rare)if(Papp! = NULL &&!papp->initapplication ())Gotoinitfailure; if(!pthread->InitInstance ()) { ......... slightly}//The run function is in THRDCORE.cpp, and this function enters the message loopNreturncode = pthread->Run (); .............. SlightlyreturnNreturncode; }
The code for the above InitInstance function is as follows:
BOOL ctestapp::initinstance () {....... Slightly csingledoctemplate*pdoctemplate; Pdoctemplate=Newcsingledoctemplate (Idr_mainframe, Runtime_class (Ctestdoc), Runtime_class (CMainFrame),//main SDI frame windowRuntime_class (Ctestview)); if(!pdoctemplate)returnFALSE; AddDocTemplate (pdoctemplate); //Parse command line to standard shell commands, DDE, File openCCommandLineInfo Cmdinfo; ParseCommandLine (Cmdinfo); //ProcessShellCommand in AppUI2.cpp, registering and creating Windowsif(!ProcessShellCommand (cmdinfo))returnFALSE; m_pMainWnd-ShowWindow (sw_show); m_pMainWnd-UpdateWindow (); returnTRUE; }
The ProcessShellCommand function in InitInstance called CMainFrame's LoadFrame function to register and create the window, and after the ProcessShellCommand function was executed, M_ was called. The pMainWnd ShowWindow and UpdateWindow functions display and update the frame window. Is this similar to the SDK program above?
Next is the message loop, which calls the Pthread run function (located in THRDCORE.cpp) in the Afxwinmain function above, which contains the message loop in run. The code for the run function is as follows:
intCwinthread::run () {....... Slightly//phase2:pump messages while available Do { //pump message, but quit on Wm_quitif(!pumpmessage ())returnExitInstance (); //Reset "No idle" state after pumping "normal" messageif(Isidlemessage (&m_msgcur)) {Bidle=TRUE; Lidlecount=0; } } while(::P eekmessage (&m_msgcur, NULL, NULL, NULL, pm_noremove)); .............. slightly} BOOL CWinThread::P umpmessage () {returnAfxinternalpumpmessage (); } BOOL AFXAPI Afxinternalpumpmessage () {_afx_thread_state*pstate =AfxGetThreadState (); if(!::getmessage (& (pstate->m_msgcur), NULL, NULL, NULL)) {..... A little bit} ....... Slightlyif(Pstate->m_msgcur.message! = Wm_kickidle &&! Afxpretranslatemessage (& (pstate->m_msgcur))) {:: TranslateMessage (& (pstate->m_msgcur)); ::D Ispatchmessage (& (pstate->m_msgcur)); } returnTRUE; }
We see PumpMessage in the GetMessage, TranslateMessage, DispatchMessage, and so on to establish a message loop and post messages.
The window procedure function Afxwinproc in the following form:
LRESULT CALLBACK afxwndproc (HWND hwnd,uint nmsg,wparam WPARAM, LPARAM LPARAM) { ... CWnd*pwnd=cwnd::fromhandlepermanent (hWnd); Returnafxcallwndproc (Pwnd,hwnd,nmsg,wparam,lparam); }
Comparison of the two running processes
By contrast, you can find that the MFC application runs in a similar way to the SDK program, starting with some initialization procedures, registering and creating the window, then displaying, updating, and finally entering the message loop, which is handled by the window procedure function. Now, do you think there's a clue? There is a basic grasp of the running process.
two. the relationship between the main classes of the MFC application Framework
In the second lecture, we show you how to use the Application Wizard to generate a single document application framework, you can see that the basic framework of the program and the necessary code are automatically generated, the previous talk about the composition of the file structure, In fact, the more important classes in the previously auto-generated framework include the following: Chelloworldapp, CMainFrame, Chelloworlddoc, and Chelloworldview, as well as other analogies such as Cclassview, Cfileview such as panels created on the frame window (CMainFrame) are not necessary.
Chicken Peck Rice On the relationship of four main classes simply speaking, the Chelloworldapp class processes the message and distributes the received message to the appropriate object. CMainFrame is the parent window of the view Chelloworldview, and the view Chelloworldview is displayed in the CMainFrame client area. The view class Chelloworldview is used to display the data in the document class Chelloworlddoc and to modify the data for the document class based on the operation of the views class. A view class can only be associated with one document class, and a document class may be associated with more than one view class. The relationship between the view class and the document class is explained in more detail later.
This section VC++/MFC programming Getting Started tutorial content is more, mainly to let everybody on the MFC application operation principle has the approximate understanding. There are many benefits to future MFC development. If there is a problem, please chat with the Chicken Peck Rice Blog. Thank you.
Unless otherwise noted, chicken Peck rice articles are original
Reprint please mark this article address: http://www.jizhuomi.com/software/145.html
VS2010/MFC Programming Primer IV (MFC application Framework Analysis)