Summary of Win32 Encapsulation by the MFC Program (Registration of window classes, creation of windows, and selection of window procedures)

Source: Internet
Author: User

First, a class structure hierarchy chart

Then let's go to the discussion topic!

 

Cwinapp * PAPP = afxgetapp (). gfxgetapp is a function that gets the current cmyapp object. In this way, we use the current app object (derived class) to attach a value to the cwinapp pointer (parent class ). In this case, Papp. initinstance ()

The derived class is called because it is a virtual function. If the derived class is not overwritten, call the base class layer to locate it.

Marked as 3 or 4:

First, both initapplication and initinstance are virtual functions. Therefore, if the Sub-app is also its ownProgramIf these two functions are reloaded, the system actually runs the rewritten function defined by the app class. In general, we do not reload the initapplication function, but initinstance is an empty virtual function, so our program must reload it! So here our PAPP-> initinstance () is the cmywinapp-> initinstance () function called.

The initinstance () function initializes the window. In this example, the main window is initialized by calling the app class, And createwindow (...) is used in Win32 programs (...) creating a window, but we do not need to do this in MFC. As long as the instance of the corresponding window class (the base class is cwnd) is initialized, the related type of create () will be called, the initialization in the figure is the object of the cmyframewnd class. Of course, we can use another window class, such as the dialog box class.

In the Win32 program, we put the operations related to the "register window class" in the initapplication function, but this is not the case in MFC. Let's look at this function:

As you can see, this function does not register the window class. After the program runs, this function runs into initinstance and starts to initialize the window of the relevant type. In principle, createwindow () do you have to register the window class before? How can I create a window without a window class name? Or the window process? This is the key point of the problem:

The program enters initinstance () --> cmyframewnd object constructor --> the CREATE () function in this window ---> and then does not know how to specify it. Then it reaches cmyframewnd: createex () --> because this class has not rewritten this function, it reaches cwnd: createex (). In fact, this constructor constructs the window we want to create now! Let's take a look:

Here we can see that some things are attached to the window class structure CS, but the window class name and window process point to these two things are missing, then, because this function is rewritten in the subclass cframewnd, it enters the cframewnd: precreatewindow (CS), and xuanjicang is in this function, this function will assign the Class Name of the window not attached to CS and the value pointing to the window process. This process is cumbersome. Let me describe it here, which is detailed in a simple way.

In this function, check whether the CS window class has been registered. If it has been registered, nothing will be done, if the function is not registered, call a function to attach a value to the window type and Window Process of CS, and then register it. abbreviated as a function:

Bool afxapi afxenddeferregisterclass (short CS)
{
CS. lpfnwndproc = defwindowproc ;//Now defwindowproc is used in the window process and will be rewritten in afxhookwindowcreate (this ).
CS. hinstance = AfxGetInstanceHandle ();
CS. hcursor = afxdata. hcurarrow;
// Several Different window classes are registered based on the judgment of different types of Windows (for example, the toolbar seems to be different from other window classes //, write only how the WND class is registered
CS. Style = cs_dblciks | cs_hredraw | cs_vredraw;
CS. lpzclassname = _ afxwnd; // _ afxwnd
Bresult = afxregisterclass (& CS); // This is the afxregisterclass, which is successfully registered.
}
Now we have successfully registered the window type of the window to be created. Now we can use this window class to create the window! However, before createjavaswex, there is also the afxhookwindowcreate (this) function, which is also called afxhookwindowcreate (this). This is a hook function that uses setwindowlong to set the window to afxwndproc, then afxwndproc () --> afxcallwndproc --> windowproc. that's right! When a message of the window object is captured, it first enters windowproc!

Afxhookwindowcreate (this) is finished! Then the "foreplays" of creating a window will be OK! Next we will go to createmediawex to create this window ~!~

Note: This example describes the detailed process of creating the main window from the main function to its first window. The main window is a cframewnd class instance. I want to talk about the initialization of other WND classes. For example, when we create a dialog box cmydialog, it is also about the process described above, as soon as cmydialog is initialized, we will go to the cdialog: createex () function and register the specified class name and window procedure function of the window class in precreatewindow. Then, createmediawex .......... all of them are encapsulated! We only need to initialize it!

 

 

 

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.