简体   繁体   中英

Window Programming (C++): How to check if child window ID is occupied? and how to generate unique ID for child window?

How would I check if a specified integer is already occupied? I build myself a template to be used especially for creating window control. I'd need some way to keep track of child ID. For example, the user of the template accidentally put create a parameter with the same ID, then it should return error. So how do I check if child ID is repeated?

What would be the best way to generate unique ID for child window? or do I just #define them to certain iteration of integer starting at a certain number? Will it conflicts the window, if for example, I create another independent window within the application?

For what it is worth, you can enumerate child window handles using EnumChildWindows() :

http://msdn.microsoft.com/en-us/library/ms633494(v=VS.85).aspx

Then there is a function for getting the ID of a child window from its handle called GetDlgCtrlID() :

http://msdn.microsoft.com/en-us/library/ms645478(v=vs.85).aspx

That's only useful once you've instantiated a window from your template, though. If you want to check the template before you build the window then that's going to depend on the template format.

The IDs you use will not conflict between windows, because the messages which indicate the numbers will be sent to different "parents". It's only (parent, id) pairs that need to be unique.

I have solved this problem by implementing simple IDManager class with a predetermined range of IDs and a queue for reusing free'd IDs. Iterating all child windows seems rather heavy weight thing to do on each control creation.

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