Second, MFC application structure
With a wizard, you can quickly generate an SDI's Windows application. After using the wizard to generate an application, there will be several files, and the main code is as follows:
Cheloworldapp theApp;
Int Afxapi AFXWINMAIN (Hinstance Hinstance, Hinstance Hprevinstance,
LPTSTR LPCMDLINE, INT NCMDSHOW)
{
Cwinthread * pthread = AfxgetThread ();
CWINAPP * PAPP = AFXGetApp ();
// afX Internal Initialization
IF (! AFXWININIT (Hinstance, Hprevinstance, Lpcmdline, ncmdshow)
Goto Initfailure;
// APP Global Initializations (Rare)
IF (PAPP! = Null &&! PAPP-> InitApplication ())
Goto Initfailure;
// Perform Specific Initializations
IF (! pthread-> initInstance ())
{
IF (pthread-> m_pmainwnd! = null)
{
Trace0 ("Warning: Destroying Non-Null M_pmainWnd / N");
Pthread-> m_pmainwnd-> destroyWindow ();
}
NReturNCode = pthread-> exitInstance ();
Goto Initfailure;
}
NRETURNCODE = pthread-> run ();
Initfailure:
........................
Afxwinterm ();
Return nreturNCode;
}
Bool cwinapp :: initApplication ()
{
IF (CDOCManager :: PStaticDocmanager! = NULL)
{
IF (m_pdocmanager == null)
m_pdocmanager = cdocmanager :: PStaticDocManager;
CDOCManager :: pstaticdocmanager = null;
}
IF (m_pdocmanager! = NULL)
m_pdocmanager-> adddockemplate (null);
Else
CDOCMANAGER :: bstaticinit = false;
Return True;
}
Bool chelloworldapp :: initInstance ()
{
AFXENABLECONTROLCONTAINER ();
..................................
// Change The Registry Key Under Which Our Settings Are Stored.
// Todo: you shop modify this string to becomething appropriate
// Such as the name of your company or Organization.
SetRegistryKey (_T ("Local Appwizard-Generated Applications));
LoadStdprofileSettings (); // load Standard Ini File Options (Including MRU)
// register the application's document templates. Document Templates // Serve As The Connection Between Documents, Frame Windows And Views.
CSINGLEDOCTEMPLATE * PDOCTEMPLATE;
PDOCTEMPLATE = New CSINGLEDOCTEMPLATE
IDR_MAINFRAME,
Runtime_class (chelloworlddoc),
Runtime_class (cmainframe), // main SDI Frame WinDow
Runtime_class (chelloworldView);
AddDDOCTemplate (pdoctemplate);
// Parse Command Line for Standard Shell Commands, DDE, File Open
CCommandLineInfo cmdinfo;
Parsecommandline (CMDInfo);
// Dispatch Commands Specified on The Command Line
IF (! ProcessShellcommand (cmdinfo))
Return False;
// The one and only window has been initialized, so show and update it.
m_pmainwnd-> showwindow;
m_pmainwnd-> UpdateWindow ();
Return True;
}
Bool cwinapp :: InitInstance ()
{
Return True;
}
"Hello World" in the MFC application
At first glance, it seems that this program is not analyzed by it, and even the entry point of the program cannot be found. In fact, the above procedure is still so suitable, I just got a lot of effort to finish the above runtime, it is already clear ^ _ ^
At the beginning of the program, first define a global variable THEAPP, he represents the existence of the entire program, and then the program begins to enter the entry point. However, the entry point here is no longer C's main () or Winmain () in the SDK, and the except offewinmain (), if not, why, then it is the first to C language The main () function is the same, as long as the program will be executed from here! In fact, in AfxwinMain () for the execution of the driver, there are several functions, the execution path is: entry point ----> AFXGetThread () ------> AFXGetApp () -------- -àafxwininit () ------ àpapp-> initApplication () ----- àpthread-> initInstance () ------ àpthread-> run (). It can be seen that the program has an execution clue to follow, but it is now unrecognizable relative to SDK. Is the clear program structure of the past? The answer is yes, but their specific implementation has been packaged in MFC.