II,
The winmain function is used to generate a window, namely:
Design window, registration window, construction window, display window, update window, message loop, and message processing.
1. cwinapp internal Running Mechanism
Cwinapp --- in appmodul. cpp
For details, refer to the cwinapp function.
1)
Pthreadstate-> m_pcurrentwinthread = this;
Here the this pointer is a pointer to the cwinapp subclass ctestapp object theapp;
2)
The cwinapp is running, that is, the base class constructor is completed, so that the base class ctestapp constructor is initialized, and then
Allocate memory space to theapp, a subclass ctestapp object. Next, enter the entry function winmain.
Cwinapp in appcore. cpp -- Global Object theapp -- subclass constructor call ctestapp () -- base class constructor call cwinapp () in appcore. cpp
2. winmain (_ twinmain () function)
Winmain function --- in appcore. cpp
Afxwinmain function --- in winmain. cpp
(Global functions starting with afx can be called by other objects)
The winmain function mainly completes the window generation process, namely:
Design window, registration window, construction window, display window, update window, message loop, and message processing.
1) initialization before the registration window
Completed by pthread-> initinstance ()
The following method of the afxwinmain function:
Cwinthread * pthread = afxgetthread ();
Cwinapp * PAPP = afxgetapp ();
Both afxgetthread () and afxgetapp () obtain the pthreadstate-> m_pcurrentwinthread object assigned to the constructor in the cwinapp.
This pointer, that is, the pointer to the ctestapp object theapp of the subclass, is obtained and stored in pthread and PAPP. So in fact, pthread and Papp
Are saved theapp pointers.
Pthread and PAPP call three methods in total, that is, the subclass object theapp calls three methods:
PAPP-> initapplication () MFC internal initialization manage the called Functions
Pthread-> initinstance () Because initinstance () in the cwinthread class is a virtual function based on the polymorphism principle, initinstance () in the subclass ctestapp is called here ()
Pthread-> Run () to complete the message loop
Where
After pthread-> initinstance () initialization is complete, the registration window starts.
Cwinapp in appcore. cpp -- Global Object theapp -- subclass constructor call ctestapp () -- base class constructor call cwinapp () in appcore. cpp
-- _ Twinmain () in appmodul. cpp -- pthread-> initinstance () in afxwinmain () -- call ctestapp: initinstance () in winmain. cpp in current project
-- Design the registration window
2) design window
Afxenddeferregisterclass () is completed (there are many if statements ).
Afxenddeferregisterclass () --- in wincore. cpp.
By default, several window classes are defined in MFC. You can complete the design window process by calling the afxenddeferregisterclass () function.
Different windows are designed based on different window types required by the application. In many if condition statements.
3) Registration window
In the afxenddeferregisterclass () function, afxregisterclass () completes.
Afxenddeferregisterclass () --- in wincore. cpp.
In the afxregisterclass () function
Afxctxgetclassinfo () determines whether the window to be registered has been registered.
Note:
The single document structure and multi-document structure have different order in the registration window.
Single document structure: Call afxenddeferregisterclass () for window Registration
Multi-document structure: Call
Subclass cmainframe: precreatewindow (createstruct & CS)
Then call the base class cframewnd: precreatewindow (createstruct & CS)
Call afxenddeferregisterclass () to register the window.
4) before the window is generated
(Single document structure)
Cwinapp in appcore. cpp -- Global Object theapp -- subclass constructor call ctestapp () -- base class constructor call cwinapp () in appcore. cpp
-- _ Twinmain () in appmodul. cpp -- pthread-> initinstance () in afxwinmain () -- call ctestapp: initinstance () in winmain. cpp in current project
-- Design window afxenddeferregisterclass () in wincore. cpp -- register afxregisterclass () in the afxenddeferregisterclass () window in wincore. cpp
-- Call the subclass cmainframe: precreatewindow (createstruct & CS) In mainfrm. CPP -- call the base class cframewnd: precreatewindow (createstruct & CS) In winfrm. CPP
-- The registration window afxdeferregisterclass (afx_wndframeorview_reg) is in wincore. cpp.
Bool cmainframe: precreatewindow (createstruct & CS) In mainfrm. cpp of the current project
Bool cframewnd: precreatewindow (createstruct & CS) In winfrm. cpp
Call afxdeferregisterclass (afx_wndframeorview_reg) to check whether the window type has been registered. The actual call is afxdeferregisterclass (fclass)
Afxdeferregisterclass (afx_wndframeorview_reg) -- In afximpl. h
# Define afxdeferregisterclass (fclass) afxenddeferregisterclass (fclass) is a macro
Two windows are generated: cmainframe and subwindow ctestview.
Cmainframe -- cframewnd -- cwnd all classes derived from cwnd are window classes
Subwindow ctestview -- cview -- cwnd subwindow is also a window class
The window generated by cmainframe is a frame window, which can also contain subwindows.
5) create a window
Createwindow Function
Createmediawex Function
Bool cwnd: createex (DWORD dwexstyle,
Lpctstr lpszclassname,
Lpctstr lpszwindowname, DWORD dwstyle,
Int X, int y, int nwidth, int nheight,
Hwnd hwndparent, hmenu nidorhmenu, lpvoid lpparam
)
In wincore. cpp
Bool cframewnd: Create (lpctstr lpszclassname,
Lpctstr lpszwindowname,
DWORD dwstyle,
Const rect & rect,
Cwnd * pparentwnd,
Lpctstr lpszmenuname,
DWORD dwexstyle,
Ccreatecontext * pcontext
)
In winfrm. cpp
Cwinapp in appcore. cpp -- Global Object theapp -- subclass constructor call ctestapp () -- base class constructor call cwinapp () in appcore. cpp
-- _ Twinmain () in appmodul. cpp -- pthread-> initinstance () in afxwinmain () -- call ctestapp: initinstance () in winmain. cpp in current project
-- Design window afxenddeferregisterclass () in wincore. cpp -- register afxregisterclass () in the afxenddeferregisterclass () window in wincore. cpp
-- Call the subclass cmainframe: precreatewindow (createstruct & CS) In mainfrm. CPP -- call the base class cframewnd: precreatewindow (createstruct & CS) In winfrm. CPP
-- The registration window afxdeferregisterclass (afx_wndframeorview_reg) is in wincore. cpp.
-- Create window call subclass bool cframewnd: Create () in winfrm. CPP -- create a window to call the base class bool cwnd: createex () in wincore. CPP (-- generate a loop window --)
Bool cwnd: createex () in wincore. cpp, call precreatewindow (CS) and: afxctxcreate1_wex (). In order to have the opportunity to modify the appearance style of the window before creating the window.
Precreatewindow is defined in afxwn. h:
Virtual bool precreatewindow (createstruct & CS );
So here we call the bool cmainframe: precreatewindow (createstruct & CS) of the subclass ),
Afxctxcreatepolicwex is a macro:
# Define afxctxcreateappswex afxctxcreateappswexw in afxcomctl32.h
In dimensions, dimensions (hwnd, create1_wexw, (DWORD dwexstyle, lpcwstr lpclassname, lpcwstr lpwindowname, DWORD dwstyle, int X, int y, int nwidth, int nheight, hwnd hwndparent, hmenu, hinstance, lpvoid lpparam), (dwexstyle, lpclassname, lpwindowname, dwstyle, X, Y, nwidth, nheight, hwndparent, hmenu, hinstance, lpparam), null)
Createmediawex is also a macro:
# Define createmediawex createmediawexw
Definitions of createstruct and createjavaswex:
Typedef struct tagcreatestruct {
Lpvoid lpcreateparams;
Hinstance;
Hmenu;
Hwnd hwndparent;
Int Cy;
Int CX;
Int y;
Int X;
Long style;
Lpctstr lpszname;
Lpctstr lpszclass;
DWORD dwexstyle;
} Createstruct;
Hwnd createmediawex (
DWORD dwexstyle,
Maid,
Lptstr lpwindowname,
DWORD dwstyle,
Int X,
Int y,
Int nwidth,
Int nheight,
Hwnd hwndparent,
Hmenu,
Hinstance,
Lpvoid lpparam
);
The member variables in createstruct are the same as those in createjavaswex, but the order is the opposite.
The createstruct struct is used to modify the appearance of the window before the window is generated.
6) display and update Windows
In test. cpp, bool ctestapp: initinstance.
After processshellcommand (cmdinfo) in test. cpp is successfully executed, the window creation is complete.
Then pass:
M_pmainwnd-> showwindow (sw_show );
M_pmainwnd-> updatewindow ();
To display and update Windows.
M_pmainwnd is a pointer to the cmainfrm frame window object
7) message loop
Cwinthread: Run in thrdcore. cpp.
Pumpmessage () ---> cwinthread: pumpmessage () --> bool afxapi afxinternalpumpmessage () Repeat pumpmessage () in do while ()
Cwinapp in appcore. cpp -- Global Object theapp -- subclass constructor call ctestapp () -- base class constructor call cwinapp () in appcore. cpp
-- _ Twinmain () in appmodul. cpp -- pthread-> initinstance () in afxwinmain () -- call ctestapp: initinstance () in winmain. cpp in current project
-- Design window afxenddeferregisterclass () in wincore. cpp -- register afxregisterclass () in the afxenddeferregisterclass () window in wincore. cpp
-- Call the subclass cmainframe: precreatewindow (createstruct & CS) In mainfrm. CPP -- call the base class cframewnd: precreatewindow (createstruct & CS) In winfrm. CPP
-- The registration window afxdeferregisterclass (afx_wndframeorview_reg) is in wincore. cpp.
-- Create window call subclass bool cframewnd: Create () in winfrm. CPP -- create a window to call the base class bool cwnd: createex () in wincore. CPP (-- generate a loop window --)
-- The display window calls bool ctestapp: m_pmainwnd-> showwindow (sw_show) in initinstance () in test. cpp (Application class;
-- The update window calls bool ctestapp: m_pmainwnd-> updatewindow () in initinstance () in test. cpp (Application class;
-- Pumpmessage () ---> cwinthread: pumpmessage () --> bool afxapi afxinternalpumpmessage () (-- repeat pumpmessage () --) in do while ()--)