Subclassing dynamically created control (No ID)

When creating variable numbers of controls at run-time, I generally give them an ID of NULL. However, if I wish to sub-class these controls, then they need to have an ID. Is there any way to generate a unique ID at runtime?
Who is Participating?
Answers2000Connect With a Mentor Commented:
Say CWnd * (or your derived class from CWnd) to the control, called pWnd

Change the id using

SetWindowLong( pWnd->m_hWnd, GWL_ID, _id_value_here ) ;

Doing this is no different from using CreateWindow with a valid id in the first place.

Now if the question is how to work out the id, they are at least 3 methods :-

(a) Reserve a range of IDs that are never used for normal dialog controls( say 10000 to 10999)
Then use these up sequential.  Remember you can start again from the lowest value when the control is destroyed.

(b) On the Window, using EnumChildWindows to find all the other controls.  Get their id's using GetWindowLong.  Store these in a map or array.  Then set your ids to values not in the array/map

(c) If all your controls are dynamically created
Assign numbers sequential to your controls as they are created.  If you are worried that you might eventually run out of possible values, then keep a list of used (or unused) id's in a map (and when a windows is created/destroyed update the map), and then you re-use values when you need em.

murraycAuthor Commented:
b) Is the kind of thing I'm looking for, but what range of IDs will be acceptable?
Anything you're not using.

Lookup at the values in resource.h

I suggest you pick a very high value, then it's easy to ensure the resource.h always uses values less than that
All Courses

From novice to tech pro — start learning today.