In Win32 SKD application programming, the simple process of creating a window program is as follows: 1. design a window class; 2. register the window class; 3. create a window based on the class; 4. display and update window; 5. enter the message loop. In addition, you also need to write Window Process functions. In MFC programming, these steps are actually hidden. Create an MFC single-document application as an example.
P66 for a Single-document application, there is a cmainframe class, a class named "C + project name + app, A class named "C + project name + Doc" and a class named "C + project name + View. If you create a project named test, it will certainly include the following classes: cmainframe class, ctestapp class, ctestdoc class, And ctestview class. As shown in:
Double-click the class name on the classview tab, and the header file defining the class will be opened in the right workspace pane.
The ctestview class is derived from the cview class, And the cview class is derived from the cwnd class.
The cmainframe class is derived from the cframewnd class, while the cframewnd class is derived from the cwnd class.
The p68 winmain () function is in the appmodul. cpp file.
The p69 ctestapp class is derived from the cwinapp class, which indicates the application class.
P73 Why define a global object theapp so that it can be executed before the winmain () function? What is the role of this object? This object represents the application itself.
When you use win32sdk to write an application, the application handle is identified by the instance handle. When you use MFC to write an application, the application handle is uniquely identified by an application class object.
The p75 afxwinmain () function is in the winmain. cpp file.
Functions with the afx prefix represent application framework functions. In MFC, functions prefixed with afx are global functions and can be called anywhere in the program.
P76 cwinapp class derived from cwinthread class
The afxgetthread () function is in the thrdcore. cpp file.
The p77 afxgetapp () function is located in afxwin1.inl.
The afxcurrentwinapp macro is defined in the afxwin. h file.
1. Design a window class
P78 MFC has predefined some default standard window classes for us. You only need to select the required window class and then register the window class. Therefore, you do not need to design the window class.
2. register the window class
The registration of the p78 window class is completed by the afxenddeferregisterclass () function, which is defined in the wincore. cpp file.
The function above P80 calls the afxregisterclass () function internally to register the window class. The function is defined in the wincore. cpp file.
The p81 afxregisterclass () function will eventually call the registerclass () function to register the window class. This is the function in Win32 SDK.
So far, the window has been registered.
3. Create a window: involves the cwnd class, cframewnd class, And cmainframe class.
The cframewnd class declaration is in the afxwin. h file: Class cframewnd: Public cwnd
Defined in the winfrm. cpp file: cframewnd ()
The cmainframe class declaration is in the afxwin. h file: Class cmainframe: Public cframewnd
Defined in the mainfrm. cpp file: cmainframe ()
The cwnd class has a function member createex:
Class cwnd: Public c0000target <br/>{< br/> ............ <Br/> Public: <br/> // advanced creation (allows access to extended styles) <br/> bool createex (DWORD dwexstyle, lpctstr lpszclassname, <br/> lpctstr lpszwindowname, DWORD dwstyle, <br/> int X, int y, int nwidth, int nheight, <br/> hwnd hwndparent, hmenu nidorhmenu, lpvoid lpparam = NULL); <br/> ............ <Br/>}
The cframewnd class has two function members: Create and loadframe:
Class cframewnd: Public cwnd <br/>{< br/> ............ <Br/> Public: <br/> bool create (lpctstr lpszclassname, <br/> lpctstr lpszwindowname, <br/> DWORD dwstyle = ws_overlappedwindow, <br/> const rect & rect = rectdefault, <br/> cwnd * pparentwnd = NULL ,//! = NULL for popups <br/> lpctstr lpszmenuname = NULL, <br/> DWORD dwexstyle = 0, <br/> ccreatecontext * pcontext = NULL ); <br/> // dynamic creation-load frame and associated resources <br/> virtual bool loadframe (uint nidresource, <br/> DWORD dwdefaultstyle = ws_overlappedwindow | fws_addtotitle, <br/> cwnd * pparentwnd = NULL, <br/> ccreatecontext * pcontext = NULL); <br/> ............ <Br/>}
The loadframe () function of the cframewnd class calls the CREATE () function, while the CREATE () function calls the createex () function of the cwnd class.
The p82 Window Creation function is implemented by the createex function of the cwnd class.
P81 the MFC application test we created actually has two windows.
The p81 cmainframe () class has a precreatewindow () function.
P81 this function will first call the cframewnd precreatewindow function, which is in the winfrm. cpp file.
4. Display and update window: The cwinthread class, cwinapp class, ctestapp class, And cwnd class are involved.
The cwinthread class declaration is in the afxwin. h file: Class cwinthread: Public c0000target
Defined in the thrdcore. cpp file: cwinthread (afx_threadproc pfnthreadproc, lpvoid pparam)
The cwinapp class declaration is in the afxwin. h file: Class cwinapp: Public cwinthread
Defined in the appcore. cpp file: cwinapp (lpctstr lpszappname)
The ctestapp class declaration is in the test. h file: Class ctestapp: Public cwinapp
Defined in the test. cpp file: ctestapp ()
A variable Member of the cwinthread class is:
Class cwinthread: Public c0000target <br/>{< br/> ............ <Br/> Public: <br/> cwnd * m_pmainwnd; <br/> ............ <Br/>}
The type of this variable is cwnd.
The cwnd class declaration is in the afxwin. h file: Class cwnd: Public c0000target
Defined in the wincore. cpp file: cwnd ()
The two function members of the cwnd class are:
Class cwnd: Public c0000target <br/>{< br/> ............ <Br/> Public: <br/> bool showwindow (INT ncmdshow); <br/> void updatewindow (); <br/> ............ <Br/>}
The cwinapp class inherits the cwinthread class in the public way, And the ctestapp class inherits the cwinapp class in the public way. Therefore, the ctestapp class can still access this variable member, in addition, the external access attribute of the m_pmainwnd variable member is still public. M_pmainwnd is of the cwnd type, so this variable has two attributes: showwindow () and updatewindow. The following code is used in the initinstance function of the ctestapp class:
// The one and only window has been initialized, so show and update it. <br/> m_pmainwnd-> showwindow (sw_show); <br/> m_pmainwnd-> updatewindow ();
5. Enter the message loop: involves the cwinthread class, cwinapp class, And ctestapp class
The cwinthread class has two function members:
Class cwinthread: Public c0000target <br/>{< br/> ............ <Br/> Public: <br/> virtual int run (); <br/> virtual bool pumpmessage (); // low level message pump <br/> ............ <Br/>}
After the cwinapp class inherits the cwinthread class in the public mode, it overwrites the run () function, but does not overwrite the pumpmessage () function.
Class cwinapp: Public cwinthread <br/>{< br/> ............ <Br/> Public: <br/> virtual int run (); <br/> ............ <Br/>}
The run () function of the cwinthread class is used to complete the message loop task. This function is called in the afxwinmain () function: nreturncode = pthread-> Run ();.
Although the ctestapp class inherits the cwinapp class in the public mode, the ctestapp class does not overwrite the run () function. Therefore, the run () function called here is a function member in the cwinapp class. In fact, the run () function of the cwinapp class also calls the run () function of the cwinthread class. The run () function of the cwinthread class also calls a pumpmessage () function, which includes the getmessage (), translatemessage (), and dispatchmessage () functions in win32sdk.
Reference: http://blog.csdn.net/subfly/archive/2010/08/11/5805283.aspx
Http://blog.csdn.net/huahuamoon/archive/2008/01/02/2010269.aspx