简体   繁体   中英

Handling dynamically populated CMenu messages

I'm implementing a right click menu in an MFC app whose contents are not known until runtime.

While it's simple enough to add menu items to a CMenu object, I haven't figured out a way to handle the messages sent by each menu item.

One technique I was investigating was assigning the same resource ID to each dynamically created menu item and handling all the messages by one callback which would contain the logic to handle each message appropriately. The problem with this approach is that, apart from being kludgey, CMenu menuitem messages are caught using the ON_COMMAND macro which requires a parameterless callback, so no information is passed to it other than than the implicit information that it was called by a resource with a certain resource ID.

I tried to trap the messages using the ON_MESSAGE macro instead as the callbacks it uses receive WPARAM and LPARAM arguments, but the callback is never invoked, so that option seems to be out.

It seems to me that there has to be a way to implement what I'm trying to do but I can't figure out it thus far. Any help is much appreciated.

Look at this answer:

https://stackoverflow.com/a/3673672/2287576

As for the message map, assuming all your menu item IDs are within a certain range, you can use ON_COMMAND_RANGE to map the entire range to a single function. This function will receive the ID as a parameter, and within the function, you can perform different operations based on the ID.

Assuming you can set aside a range of ID values this method will work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM