Depth analysis message reflex mechanism

zhaozj2021-02-16  64

Summary: In the previous, we analyzed the control notification message WM_NOTIFY, and WM_NOTIFY closely contacted another MFC new feature: message reflection. In this article, I want to make a comprehensive discussion on this issue. If there is a mistake, I also hope that all the prawn criticism is correct.

What is a message reflection? In Windows, subfeatures often send messages to parent controls, such as many sub-controls to draw their own background, may send messages to the parent window WM_CTLColor. For messages from sub-control, the parent control may return the message to the child control process before processing, so that the message wants to be reflected back from the parent window, so the name: message reflection.

The source of the message reflected in Windows and MFC4.0, the parent window (usually a dialog) will process these messages, in other words, these message processing from the control must be in the parent window class, whenever we add When the child control, you must copy these code in the parent window class. We can imagine how complicated this, how bloated the code! We can imagine that if these news makes the parent window classes, the parent window has become a universal god, a bloated code machine, whether the process of the message is concentrated in the parent window class, will make the parent window Unparalleled, but the child control can be done, and the code cannot be reused. What pains are there in a programmer? ! In the old version of the MFC, the designer also realized this problem, they use virtual mechanisms for some messages, such as WM_DRAWITEM, such a child control, has the opportunity to control their own actions, and the reusability of the code has been improved. But this has not reached most of the requirements, so in high versions of MFC, a more convenient mechanism is proposed: message reflection. Through the message reflex mechanism, the sub-control window can process some messages related to itself, enhance the encapsulation, and improve the reusability of the sub-control window class. However, it is necessary to pay attention to: Message Reflections are implemented by MFC, not Windows implementation; to make your message reflex mechanism work, you must derive from the CWND class.

Processing in Message-Map If you want to process a message reflection, you must understand the corresponding Message-Map macro and function prototype. Generally speaking, Message-Map has a certain law, usually she adds an ON_ in front of the message, and then the last addition _Reflect. For example, the WM_CTLCOLOR mentioned earlier has become ON_WM_CTLCOLOR_REFLECT; WM_MEASUREITEM becomes ON_WM_MEASUREITEM_REFLECT. Everything always exceptions, here, too, and there are three exceptions: (1) WM_COMMAND converted ON_CONTROL_REFLECT; (2) WM_NOTIFY converted ON_NOTIFY_REFLECT; (3) ON_UPDATE_COMMAND_UI converted ON_UPDATE_COMMAND_UI_REFLECT; for function prototypes must be based afx_msg beginning.

Add a message Reflection with ClassWizard (1) In ClassWizard, open the Select Issage Maps; (2) Select the class you want to control in the drop-down list Class Name; (3) In Object IDs, select the corresponding class name; (4) In the Messages column, find a message with = tag, which is a reflection message; (5) Double-click the mouse or click the Add button, then the OK! Message Process (1) The sub-window sends a notification message to the parent window. Stimulate the parent window to call its virtual function CWnd :: ONNOTIFY. The rough structure is Bool CWnd :: Onnotify (WParam WParam, LParam LParam, LRESULT * PRESULT) {if (REFLECTLASTMSG (HWNDCTRL, PRESULT)) File: // hwndctrl, for the send window return true; file: // If the child window has processing this message, returns AFX_NOTIFY notify; notify.pResult = pResult; notify.pNMHDR = pNMHDR; return OnCmdMsg (nID, MAKELONG (nCode, WM_NOTIFY) notify:? NULL);} (2) ReflectLastMsg following statement: static BOOL PASCAL ReflectLastMsg (HWND HWNDCHILD, LRESULT * PRESULT = NULL); its main task is to call sendChildNotifyLastMsg for the sending window. (3) SendChildNotifylastmsg declarations as follows: BOOL SendChildNotifyLastmsg (LRESULT * PRESULT = NULL); call the virtual function onChildNotify function to send the window for processing. If the send window does not perform overload processing, the message mapping process of the ReflectChildNotify (...) function is called for standard reflection messages.

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

New Post(0)