Create and destroy windows and MFC Application Frameworks in VC/MFC

Source: Internet
Author: User

Before explaining the process of window destruction, you must first understand the window classes, window objects, window concepts, and their differences and relationships.

Window

That is, the application interface we see on the desktop, the system maintains a WNDCLASS struct for each window to describe various properties of the window (such as the window style, icon, painter, menu, and Window Process ). The system also saves the location, size, and status of the window for the window. If you want to query and operate on these attributes, you cannot directly operate on them because you do not know their addresses. They are maintained by the system. However, the system provides many functions for you to operate on them. The system maintains the data structure for each window. You must specify the window to operate. Therefore, the window handle is used to identify a window (and the data structure maintained by the system for this window ). So Windwos
In the SDK, almost every operation window function requires a window handle as a parameter. A window is a series of structures maintained by the interface and system on the desktop.

 

Window Type

Create a window in Windows SDK

Speaking of the window class, we have to review the process of creating a window during Windows SDK programming.

  • Design window class (set various properties. Note that this window class is only a struct, not the window class we will discuss)
  • Registration window class
  • Create window (more designed window class struct creation window)
  • Display window
  • Update window
  • Message Loop
  • Window Process (in the window process, you can call different functions based on the received message, and each message has the default processing process DefWindowProc (hwnd, message, wParam, lParam ), defWindowProc calls different processing functions based on the message .)

In the above process, after the window is created, we can call various functions to operate the window. Most of them are serial ports during the window process. Many messages have default function calls.

MFC uses window classes to encapsulate windows and the preceding complex processes in a C ++ class. It also provides several default window classes based on different windows. The window handle is the public member variable m_hWnd of the window class, and the window operation function is the member method of the window class. The macro definition is also used to associate the message with the default member method.

Method (function) classification in window class:

  • Window operation functions: these functions are the re-encapsulation and upgrade of the functions provided by the SDK, but you do not need to pass the window handle to them. Because they can directly invoke the member variable m_hWnd of the category class. Some of them are automatically called. Of course, they can all be called. Some of them can also be reloaded.
  • Message response functions: These functions all have message ing (it seems that VC uses macro implementation ). Different messages have different message response functions. These response functions call the above functions for window operations. To change the default Processing Method of these messages. These functions are all set to virtual functions. You can reload them.

To sum up, the window class is a C ++ class, which encapsulates the window and the operation functions of the window.

 

Window class object:

Is the instance object generated by the window class above.

 

 

Differences and links between window objects and windows in MFC:

According to the previous descriptions, a member variable m_hWnd of the window class object is associated with a window. M_hWnd is the bond between them.

Window class objects are created or destroyed by window class constructor or destructor. The window is created by calling the Create, CreateEx, or DoModa member functions of the window class object. Mark it with m_hWnd after creation. The window is destroyed by calling the DestroyWindow member function of the window class object. After the DestroyWindow is destroyed, the window class object still exists. However, when the delete operation destroys a window object, the window attached to it is also destroyed.

 

 

 

 

 

 

Create an MFC window

MFC not only encapsulates the window class. In addition, the window creation, initialization, and message loop are encapsulated with a WinApp class. The WinApp class has a major member function, InitInstance (), which is used to create and initialize windows. Encapsulate the message loop mechanism in the Run member function. The following describes how to start the MFC program.

An MFC program defines a global winapp variable, and then obtains the pointer papp of the global winapp variable in the entry function.

Call the winapp: initinstance () member function through PAPP to create and initialize the window. (Our window class is used here. winapp has a member variable of the window class. In this way, the winapp is associated with the window)

Call the run member function through PAPP to enter the message loop. This is the same as the framework of our Win32 SDK program. It seems that MFC only encapsulates the original Win32 SDK framework.

 

 

 

 

 

// To be continued...

 

 

 

 

 

 

 

 

 

 

 

 

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.