Https://msdn.microsoft.com/en-us/library/eeah46xd.aspx
Turn from://http://blog.csdn.net/zhuce0001/article/details/9152515.
In a dialog-based program, one task is to change the background of the edit box control.
The implementation of the idea is as follows, when the child control to draw will be sent to the parent class (here is the main window) wm_ctlcolor
Message, in the parent class (here is the main window) in response to Wm_ctlcolor, according to the message passed over the
Parameters for the corresponding operation, the code is as follows:
[CPP]View PlainCopy
- CBrush M_brush;
[CPP]View PlainCopy
- M_brush. CreateSolidBrush (RGB (0, 0, 255));
[CPP]View PlainCopy
- Hbrush Cmy112dlg::onctlcolor (cdc* PDC, cwnd* pWnd, UINT nCtlColor)
- {
- hbrush HBR = Cdialog::onctlcolor (PDC, PWnd, nCtlColor);
- if (pwnd->getdlgctrlid () = = idc_edit1)
- {
- Pdc->setbkmode (TRANSPARENT);
- return m_brush;
- }
- return HBR;
- }
The code is interpreted as follows: Define a member variable M_brush in the main dialog class Cmy112dlg, and in the main dialog
Initializes the constructor in the box class. The Wm_ctlcolor is then written for the parent class, which is judged in the message response
Control is set to the edit box control (the edit box control ID idc_edit1 to set) sets the background mode
Set to Transparent, and sets the control background to blue.
What does this have to do with message reflection? Is there a problem with the above code? The above method is also a good way, but from
Software engineering Perspective There are some drawbacks, the child control of the message to the parent class to handle, which will lead to the parent class
The code in is bloated, and the subclass has nothing to do. On the other hand, what if other projects need to use similar functions?
We have to copy the above function code to another project and modify it slightly.
MFC4.0 started with a new way to solve this problem: message reflection, for example, when a child control is being drawn,
The message to be drawn is sent to the parent class, and after the parent receives the message, it is first reflected (then sent) to the child control, and the child control is seen
Whether the message has been processed, and if so, the parent class will no longer process it. If the child control is not processed, the
The parent class sends messages to the child control for processing.
Another way to look at the example above: Message Reflection method
Add a new class CYellowEdit for the project, which derives from the MFC CEdit class and adds three member variables to the class
[CPP]View PlainCopy
- COLORREF M_clrtext;
- COLORREF M_clrbkgnd;
- CBrush M_brbkgnd;
is the text color, background color, paint brush, in the CYellowEdit class constructor to initialize
[CPP]View PlainCopy
- Cyellowedit::cyellowedit ()
- {
- M_clrtext = RGB (0, 0, 0);
- M_clrbkgnd = RGB (255, 255, 0);
- M_brbkgnd.createsolidbrush (M_CLRBKGND);
- }
Add a reflection function for the CYellowEdit class (the message preceded by = is a reflection message, in this case, =WM_CTRLCOLR,
[CPP]View PlainCopy
- Hbrush Cyellowedit::ctlcolor (cdc* pDC, UINT nCtlColor)
- {
- Pdc->settextcolor (M_clrtext); //Text color
- Pdc->setbkcolor (M_CLRBKGND); //Background color
- return M_BRBKGND; //Control background
- }
Add an edit box to the main dialog box, and associate the variable with the variable type Cyelloedit, and the Run program can
To see that the background of the edit box has turned yellow.
This is one of the usefulness of message reflection, what is the difference between using message reflection and not using message reflection two examples? A
People think that the biggest difference is the reusability of code, after using message reflection, if you encounter similar applications, directly
CYellowEdit copy to other projects directly, no need to modify the code.
The above examples are described in more detail on MSDN, and the topic in MSDN is: On_notify_reflect,
There's an article inside
Tn062:message Reflection for Windows Controls
The article specifically describes the message reflection. There are a lot of articles on the Internet, but it's basically a reference to MSDN translation,
Here are some of the post addresses for the message reflection data
1 http://blog.csdn.net/gao_zilai/article/details/7417690
2 http://blog.csdn.net/cay22/article/details/6171616
3 Http://bbs.ednchina.com/BLOG_ARTICLE_1926378.HTM
Message Reflection for Windows Controls (messaging reflection mechanism)