How unique must the
uIdSubclass parameter be when I call
SetWindowSubclass function manages window subclassing in a safer way. When you use it to subclass a window, you provide four pieces of information:
- The window you want to subclass,
- The function you want to subclass it with,
uIdSubclassthat is passed to the subclass function,
dwRefDatathat is passed to the subclass function.
You get to pick two values to pass to the subclass function. What’s the difference?
A unique instance of a subclass managed by the
SetWindowSubclass function is identified by a triplet of the window handle, the subclass function, and the subclass ID (
uIdSubclass). The reference data (
dwRefData) is not part of the identity. It’s some extra data that the
SetWindowSubclass function remembers and passes back to your subclass function, but the
SetWindowSubclass doesn’t pay any attention to the value.
SetWindowSubclass function does pay attention to the
uIdSubclass. If you call
SetWindowSubclass function twice on the same window with the same function and the same
uIdSubclass, then the second one overwrites the first. (You can do this if you want to change the
uIdSubclass is handy if you intend to subclass the same window multiple times with the same function. For example, the Tooltip control has an option to subclass the window to which the tooltip is attached. If you ask for this, then the Tooltip control calls
SetWindowSubclass function with the attached window, an internal subclass function, and uses the handle of the Tooltip control itself as the
uIdSubclass. That way, it’s possible for two Tooltip controls to be attached to the same window without interfering with each other.
In these sorts of cases, where you may have multiple instances all subclassing the same window, the
uIdSubclass is typically some value that is unique to each instance (such as a pointer or handle).
Most of the time, though, you’re not writing a reusable component that can be applied multiple times to the same window. You’re just doing a one-shot subclass of a window with a specific function, and you have no intention of subclassing the window twice. In that case, the
uIdSubclass value is not interesting, as long as you are consistent in which value you use.
Most people just pass zero.