开发者

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 examp开发者_如何学JAVAle, 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜