[Original] WTL for MFC Programming Practice --- a custom ComboBox transplantation process (below)

zhaozj2021-02-08  261

"Programmer's way to practice" said that when you want to say this, it is often a mistake in the way you call.

We returned to the starting point to see if it is wrong. After studying the code carefully, how did the event pass to MSG_map? Do we pass the assignment to pass a form handle, is the MSG_map defined in this class automatically connects to this handle? This is obviously impossible.

So did not connect MSG_MAP to form handles, probably the cause of control classes could not receive any events. So how do you connect MSG_MAP to the form handle? An important function is mentioned in the original book, CWindowImpl :: SubclassWindow (). We change our control class again:

CCOMBOBOXEX & OPERATOR = (hwnd hwnd) {

CWindowImpl :: SubclassWindow (hwnd);

RETURN * THIS;

}

Under the test, it was shocked. Not only the heavy-in event is triggered, and there is no ATTACH DETACH in the connection function. It can also be deleted. Why is this so? Before exploring this problem, let's take a look at the DDX_Control Macro used by the original book - it only has a role in the derived class of CWINDOWIMPL - what is going on. The original code is as follows:

#define ddx_control (NID, OBJ) /

IF (nctlid == (uint) -1 || nctlid == NID) /

DDX_Control (NID, OBJ, BSAVEADATE);

// Full Control Subclassing (for CWindowImpl Derived Controls)

Template

Void DDX_Control (uint Nid, Tcontrol & Ctrl, Bool Bsave)

{

IF (! BSAVE && Ctrl.m_hwnd == NULL)

{

T * pt = static_cast (this);

Ctrl.subclassWindow (Pt-> Getdlgitem (NID));

}

}

As you can see from the original code, the DDX_Control Macro and DDX_Control_Handler macro implementation is just that the former uses SubclassWindow, and the latter uses operators "=". If we connect our code, call SubclassWindow in the handler "=" process function, it is equivalent to using the DDX_Control_Handler macro, but also implement the DDX_Control macro. It turned out to go out, and the result was a 3 lap around the back, this is really a big joke.

Why is this so? Do not use DDX_Control macro because ccomboBox has no subclasswindow function, but use ccomboBox because ccomboBoxex is derived from CCOMBOBOX. When transplantation, it will tend to choose the same name, not CCOMPL This strange statement method.

However, it is ignored that a basic WTL feature is ignored. Since the WTL is based on ATL, the design ATL is to separate the interface and implementation, so all classes that do not have an IMPL word in the WTL are not implemented, like CWindow, CButton, CCOMBOX, etc. They just include a handle, no incident, they just be responsible for transfer, packaging control events, etc. Operators like CCOMBOBOX "=" is just a assignment statement. DDX_CONTROL_HANDLER is for these class services, of course, if we pay attention to this macro comment, you may have discovered this problem early, remember? Relive here: // Full Control Subclassing (for CWindowImpl Derived Controls)

Template

Void DDX_Control (uint Nid, Tcontrol & Ctrl, Bool Bsave)

// Simple Control Attaching (for HWnd Wrapper Controls)

Template

Void DDX_Control_Handle (uint Nid, Tcontrol & Ctrl, Bool Bsave)

Ok, after a week around the Earth, we returned to the starting point. Although a lot of strength, I also figured out a lot of things, and I have summarized below:

1. The WTL class contains interface classes (encapsulation of form handles and events) and implementation classes (you can have your own events), and use it according to specific situations.

2. WTL does not automatically sell the form handle (of course, refers to the interface class), so it is necessary to remember Detach after ATTACH operation.

3. Note that there is a Handle's macro, class, function, which is often an interface class or an interface class service, as DDX_Control_Handle, and CDCHANDLE, and more.

4. DDX is implemented by the macro definition overload CWINDATAEXCHANGE :: DODATAEXChange () function

5. Message Reflections After the form handle of the access message, the message reflection is implemented by the corresponding message like it.

6. When you want to say this is impossible, it is often a mistake that you have in the way.

7. Take a look at the code and you will know more.

转载请注明原文地址:https://www.9cbs.com/read-1200.html

New Post(0)