The basis of WTL is ATL. The WTL frame window is inheritance of the ATL window class. Therefore, first introduce the ATL package to the Windows window.
The Windows application described by the first part can know the logic of creating a window and window work:
1 Register a window class
2 Create this class window
3 Display and activate the window
4 The message processing logic of the window is in the window function. This function is specified when the window class is registered.
As can be seen from the above logic, the package window mainly needs to solve how to encapsulate window message processing mechanisms.
There are two problems with the package of the window message processing mechanism.
First, in order to make the window function of the encapsulated class is transparent, we will think that the message to be forwarded to the different classes to the instance of different classes. So how do you forward messages in a window function to a category of a package? Because there is only one window function of all encapsulated class windows, that is, a class window has only one window function. And we want to send messages to an instance of a class. The problem is that the window function does not know which instance. It only knows HWnd, not the handle of an instance of the class. Therefore, there must be an approach that can find an example of a class corresponding to it.
Second, suppose that the above problem has been solved. So how do you pass messages to an instance of the corresponding class. The usual way is to use virtual functions. Generate a virtual function corresponding to each message. This way, in the window processing function, for each message, it is called to call its corresponding virtual function.
But this will have a lot of virtual functions, making the virtual function table of the class very much.
To this end, the package window is to solve the above two basic problems. For the second question, ATL is by defining a virtual function. Then, by using the macro, the message processing function is generated. For the first question, ATL is implemented by using dynamic changes HWND parameter method.
ATL to the window package
The illustrated inheritance diagram of the class of the ATL package. From the figure, you can see two most basic classes. One is CWindow, the other is CMessageMap.
CWindows is a package for Windows window API. It encapsulates a Windows handle and provides an API of the API of the window represented by the handle.
An instance of CWindow is an object in the C language. It contacts the window handle with the actual Windows window. Creating a CWindow instance does not create a corresponding Windows window, you must call CWINDOW.CREATE () to create a Windows window. You can also create an instance of a CWindow and then mount it with the already existing Windows window.
CMessageMap only defines an abstract virtual function --ProcessWindowMessage (). All windows with message processing mechanisms must implement this function.
Usually use ATL development programs, are derived from the CWindowIMPLT class. As can be seen from the inheritance drawings of the class, the class has the operation function and message processing mechanism of the general window.
When developing applications, you must define "message mapping" in your class.
Begin_MSG_MAP (CMAINFRAME)
Message_handler (WM_CREATE, ONCREATE)
Command_id_handler (ID_APP_EXIT, ONFILEEXIT)
Command_id_handler (id_file_new, onfilenew)
Command_id_handler (id_view_toolbar, onviewtoolbar)
Command_id_handler (id_view_status_bar, onviousstatusbar)
Command_id_handler (ID_APP_ABOUT, ONAPPABOUT)
CHAIN_MSG_MAP (CUPDATEUI
CHAIN_MSG_MAP (CFrameWindowImpl
END_MSG_MAP () We know that a window function is usually a lot of case statements. ATL uses a macro to form a code of the window function.
The message mapping is implemented with macro. By defining a message mapping and implementing a message in the message map, the compiler is compiled, which will generate processWindowMessage () for you.
Message mapping macro contains message processing macro and message mapping control macro.
BEGIN_MSG_MAP () and END_MSG_MAP ()
Each message mapping begins with a begin_msg_map () macro. Let's take a look at this macro:
#define begin_msg_map (theclass) /
PUBLIC: /
Bool ProcessWindowMessage (HWND HWND, UINT UMSG, WPARAM WPARAM,
LPARAM LPARAM, LRESULT & LRESULT, DWORD DWMSGMAPID = 0) /
{/
Bool bhandled = true; /
HWND; /
UMSG; /
WPARAM; /
LPARAM; /
LRESULT; /
Bhandled; /
Switch (dwmsgmapid) /
{/
Case 0:
At a glance, this is the implementation of the function processWindowMessage (). How simple ATL is compared to MFC's message mapping. Remember that the more simple and more attractive.
It should be noted that dwmsgmapid. Each message mapping has an ID. The following will introduce why it uses this.
Thus, it can be inferred that the message processing macro should be a part of the function of the function. It is also possible to conclude that end_msg_map () should define the function of the function.
Let's verify:
#DEFINE END_MSG_MAP () /
Break; /
DEFAULT: /
Atltrace2 (AtltraceWindowing, 0, _T ("INVALID Message Map ID (% i) / N"),
DWMSGMapID; /
Atlassert (false); /
Break; /
} /
Return False; /
}