1

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?

Eternal Learner
  • 3,800
  • 13
  • 48
  • 78

2 Answers2

5

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.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
  • Since the addition of buttons takes place on the fly I do not know before hand how many buttons I need to create. The reason I need to know is to associate the id's with ON_COMMAND_RANGE and to insert them in the resource file so that they are recognized as valid ID's. – Eternal Learner Aug 31 '12 at 16:33
  • @EternalLearner, if you're creating the buttons on the fly you don't need to have them in the resource file. Just define a min and max that you're allowed to use and keep track of the current one, incrementing each time you create a new button. – Mark Ransom Aug 31 '12 at 16:36
  • Everything worked except for GetDlgItemText, since the element I added was of type CMFCRibbonButton. I made use of the FindByID(uint ID) method. Thanks. – Eternal Learner Aug 31 '12 at 19:39
2

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.

Avi Berger
  • 2,068
  • 1
  • 18
  • 13