1
votes

I have a menu-like MFC control which hosts a lot of menu-entries (with command IDs). The number of menu-entries as well as the structure changes dynamically during runtime. That means that I have to create controls and assign new IDs dynamically from time to time.

What I did so far is to reserve a large static range of IDs and assign them sequentially. Even though the range is pretty large I'm afraid I will end up at the point where there are no IDs left. I cannot start over at the beginning either because I do not know which of the previously assigned IDs have been released.

My first thought was to find the largest command ID in the current resource handle and start from there. But I don't know how to accomplish that.

Or is there a better way to manage this? I think I might not be the first person with this kind of problem.

1
"I do not know which of the previously assigned IDs have been released" - so is this something that's not under your control? What is the mechanism?Roger Rowland
At least I thought so because the host control and the menu-entry controls are from a proprietary library. But your question just kindled an idea in my head. Thanks! :-)koloman

1 Answers

0
votes

Hmm. It is not very possible to run out of IDs. You can start from WM_USER and increment the ID each time with 1. But if you really think that you can run out of IDs then you can use a stack or list keeping the already used IDs and reuse them the next time when you need ID. When you finish processing the message add the ID into the stack with push(ID) method (you can pass the ID with LPARAM of the ON_MESSAGE macro in MFC). Then when you need a new ID first check if the ID stack is empty, if not take the top ID with pop(). Only if the IDs stack is empty use the last available in the range ID.