Search code examples
c++visual-studiomfcvisual-studio-2017message-map

MFC: Expanding during runtime


I have a GUI project at work that uses MFC.

The widgets (controls) have message processing in a compile-time message map table.

I want to conditionally add controls to the form during runtime, but I'm stuck on how to append message handlers to the message map during runtime.

How do I add message handlers to the MFC message map during runtime?

Is there an alternate process that I should use?

See MFC Message Maps documentation for information about message maps.

Environment:
Windows 7 or Windows 10 (The application works on both OS)
Visual Studio 2017


Solution

  • If you know the range of the "ID" values you give to your added controls (as you should), then you can use the ON_CONTROL_RANGE entry in your message map, rather than the ON_CONTROL (which is typically used for a specific, single control). For example, for a button click on one of your controls (which has an ID in the range IDC_FIRST thru IDC_LAST, you can add the following message map entry:

        ON_CONTROL_RANGE(BN_CLICKED, IDC_FIRST, IDC_LAST, OnButtonClick)
    

    The message handler, OnButtonClick, has a very similar format to that for a single control, but with a UINT parameter that is the ID of the control that sent the message:

    void MyDialog::OnButtonClick(UINT nID)
    {
        int button_number = static_cast<int>(nID - IDC_FIRST);
        // .. do something
        return;
    }
    

    Feel free to ask for further clarification and/or explanation.