How to pass value to MFC message map function on_command

3.7k views Asked by At

I have a function that performs a specific action depending on the std::string value it receives.

Eg:

void performTaskOnStringValue(std::string);

Now I have a set of Ribbon Buttons (CMFCRibbonButton) that need to call this function by passing their respective text as values on button click.

I have mapped the id's of these buttons to a message map macro on_command with the button id's. All the buttons share one common id - Eg -

ID_RIBBON_BUTTON_ID

The message map is as follows

ON_COMMAND(ID_RIBBON_BUTTON_ID, &MyClass::performTaskOnStringValue);

How do I pass the button text as parameter to this function on ButtonClick?

2

There are 2 answers

3
Mark Ransom On BEST ANSWER

You can't make the buttons do different operations when they all have the same ID. The command handler gets no indication of which button was pressed.

If you can assign consecutive IDs to the buttons you can use ON_COMMAND_RANGE instead. This will pass you the ID of the button, which you can pass to GetDlgItemText to get the text from the button.

0
Avi Berger On

First, give each button different ids in a contiguous range.

Second, use ON_COMMAND_RANGE to map all the buttons to one handler that receives the id as a parameter.

Third, in that handler use the id to get the button text. You can then call your performTaskOnStringValue method.