Windows Programming _ sun Xin C ++ lesson3 "Analysis of the MFC Program Framework"

Source: Internet
Author: User

 

Windows Programming _ sun Xin C ++ lesson3 "Analysis of the MFC Program Framework"

Highlights of this section:

1. Understand the execution sequence of global variables, global objects, and main functions.

2. It corresponds to the winmain program in lesson1, and follows up with the single-document program of MFC to understand the encapsulation of the Windows program and the framework of the MFC program.

3. Understand the relationship between classes in MFC and the cwnd class. Add a button to understand the framework and creation process of a single document program.

4. Understand the class encapsulation.

//************************************** **************************************** **************************************** **

1. The program code is as follows:

# Include <iostream. h>
// Int A = 6; // The main function has allocated memory for the global variable and assigned the initial value before loading.
Class cpoint
{
Public:
Cpoint ()
{
 
}
};
Cpoint pt; // The Global Object also calls the constructor and allocates memory space before calling the main function.
Void main ()
{
// Cout <A <Endl;
}

//************************************** **************************************** **************************************** **

2. MFC corresponds to the winmain experiment window creation process in lesson1.

Window creation process:
(1) generate the Global Object theapp
(2) Enter winmain
(3) Call the virtual function initinstance to complete window initialization, including registering, creating, and updating the display.
(4) display and update the main frame window
(5) message loop

The main functions or processes of the entire process are as follows:

//************************************** **************************************** **************************************** **

// Appmodul. cpp
Extern "C" int winapi
_ Twinmain (hinstance, hinstance hprevinstance,
Lptstr lpcmdline, int ncmdshow)
{
// Call shared/exported winmain
Return afxwinmain (hinstance, hprevinstance, lpcmdline,

Ncmdshow );
}

//************************************** **************************************** **************************************** **

// Winmain. cpp
Int afxapi afxwinmain (hinstance, hinstance hprevinstance,
Lptstr lpcmdline, int ncmdshow );
Run (! Pthread-> initinstance () Jump to the initinstance () process of the subclass.

//************************************** **************************************** **************************************** **

// Test. cpp
Bool ctestapp: initinstance ()

//************************************** **************************************** **************************************** **

// Wincore. cpp
Bool afxapi afxenddeferregisterclass (long ftoregister );
Bool afxapi afxregisterclass (wndclass * lpwndclass)
Wndclass wndcls is designed and registered.

//************************************** **************************************** **************************************** **

// Winfrm. cpp
Bool cframewnd: Create (...);
// Wincore. cpp
Bool cwnd: createex (...);
// Mainfrm. cpp
Bool cmainframe: precreatewindow (createstruct & CS );
The subclass first calls the precreatewindow of the parent class and then calls the precreatewindow of the subclass;

Createstruct gives us the opportunity to modify the window createstruct & CS reference form call in the base class

Is embodied.
First, check whether the window has been registered.

//************************************** **************************************** **************************************** **

// Test. cpp
M_pmainwnd-> showwindow (sw_show );
M_pmainwnd-> updatewindow ();
Display and update window.

//************************************** **************************************** **************************************** **

// Threadcore. cpp
In the int cwinthread: Run () method, call pumpmessage and peekmessage to Implement Message loops.

//************************************** **************************************** **************************************** **

3. Understand the single-document program framework of cwnd and MFC

(1) Main classes contained in ctestapp:

Ctestapp Application
Ctestframe ctestview window class
Cdocument document class
Csingledoctemplate
Csingledoctemplate * pdoctemplate;
Pdoctemplate = new csingledoctemplate (
Idr_mainframe,
Runtime_class (ctestdoc ),
Runtime_class (cmainframe ),

(2) The cwnd class has a public data member m_hwnd to save the handle of the window

(3) Key Points for creating a button:
1. For the Life Cycle Problem of local variable member variables, refer to cmainframe: oncreate to define cbutton BTN and fill in the following code;

//************************************** **************************************** **************************************** **

BTN. Create ("sdiex", ws_child | bs_pushbutton, crect (100,100,), this, 1); // cwnd * pparentwnd cannot be m_hwnd
// BTN. showwindow (sw_shownormal );

Button cannot be displayed, because the partial cbutton lifecycle has reached the window resource to be destructed. Declare BTN as the member variable cbutton m_btn;

2. display the button to create a button, but two methods are required to be displayed: showwindow and ws_visible.

M_btn.create ("sdiex", ws_child | ws_visible | bs_auto3state, crect (100,100,), getparent (), 1 );
3. Add code at oncreate in cframewnd; add Message Processing wm_create in view class first

And then add the Code. In the View class, place the button on the main framework and use the getparent function to obtain

Take the master frame pointer.

Window Effect of Button creation:

4. Class Encapsulation

The experiment code is as follows. The Code cannot be executed, but it is used for instructions.

//************************************** **************************************** **************************************** **

# Include <windows. h>
Class cwnd // custom class cwnd, but function of Win32 SDK platform is encapsulated in method implementation
{// The Code cannot be executed as encapsulation only
Public:
Bool createex (...);
Bool showwindow (INT ncmdshow );
Bool updatewindow ();
Public:
Hwnd m_hwnd;
);
Bool cwnd: createex (...)
{
M_hwnd =: createmediawex (dwexstyle, lpclassname, lpwindowname, dwstyle, x, y,
Nweight, nheight, hwndparent, hmenu, hinstance, lpparam );
If (m_hwnd! = NULL)
Return true;
Esle
Return false;
}
Bool cwnd: showwindow (INT ncmdshow)
{
Return: showwindow (m_hwnd, ncmdshow); // Win32 API function reference: Avoid class Function Name Conflict
}
Bool cwnd: updatewindow ()
{
Return: updatewindow (m_hwnd );
}
Int winapi winmain (
Hinstance, // handle to current instance
Hinstance hprevinstance, // handle to previous instance
Lpstr lpcmdline, // command line
Int ncmdshow // show state
)
{
Wndclass wndcls;
Wndcls. Style = cs_hredraw | cs_vredraw;
.....
Registerclass (& wndcls );
Cwnd WND;
WND. createex (...);
WND. showwindow ();
WND. updatewindow ();
... MSG dealer
// The lifecycle of a window and an object is different. The link between a window and an object is that the handle stores the address of the window resource.

// After the window is destroyed, the object can exist, but the window must be recycled and destroyed when the object is destroyed.

Hwnd;
Hwnd =: createwindow ();
: Showwindow (hwnd, sw_shownormal );
: Updatewindow (hwnd );
}

Summary:

In this section, we track and debug program power outages. We can see the internal encapsulation of MFC and understand the windows application processing process under the MFC framework; in addition, I learned the framework of the single-document program, some features of the cwnd class, And the encapsulation of the Win32 SDK platform class.

Related Article

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.