Message Reflection for Windows Controls (messaging reflection mechanism)

Source: Internet
Author: User

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
    1. CBrush M_brush;

[CPP]View PlainCopy
    1. M_brush. CreateSolidBrush (RGB (0, 0, 255));

[CPP]View PlainCopy
  1. Hbrush Cmy112dlg::onctlcolor (cdc* PDC, cwnd* pWnd, UINT nCtlColor)
  2. {
  3. hbrush HBR = Cdialog::onctlcolor (PDC, PWnd, nCtlColor);
  4. if (pwnd->getdlgctrlid () = = idc_edit1)
  5. {
  6. Pdc->setbkmode (TRANSPARENT);
  7. return m_brush;
  8. }
  9. return HBR;
  10. }


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
    1. COLORREF M_clrtext;
    2. COLORREF M_clrbkgnd;
    3. CBrush M_brbkgnd;

is the text color, background color, paint brush, in the CYellowEdit class constructor to initialize

[CPP]View PlainCopy
    1. Cyellowedit::cyellowedit ()
    2. {
    3. M_clrtext = RGB (0, 0, 0);
    4. M_clrbkgnd = RGB (255, 255, 0);
    5. M_brbkgnd.createsolidbrush (M_CLRBKGND);
    6. }

Add a reflection function for the CYellowEdit class (the message preceded by = is a reflection message, in this case, =WM_CTRLCOLR,

[CPP]View PlainCopy
    1. Hbrush Cyellowedit::ctlcolor (cdc* pDC, UINT nCtlColor)
    2. {
    3. Pdc->settextcolor (M_clrtext); //Text color
    4. Pdc->setbkcolor (M_CLRBKGND); //Background color
    5. return M_BRBKGND; //Control background
    6. }

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)

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.