Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 759
Thomas Matthews Avatar asked Sep 11 '26 15:09

Thomas Matthews


1 Answers

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.

like image 134
Adrian Mole Avatar answered Sep 13 '26 04:09

Adrian Mole



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!