How unique must the uIdSubclass parameter be when I call Set­Window­Subclass?

Raymond Chen

The Set­Window­Subclass 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,
  • A uIdSubclass that is passed to the subclass function,
  • A dwRefData that 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 Set­Window­Subclass 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 Set­Window­Subclass function remembers and passes back to your subclass function, but the Set­Window­Subclass doesn’t pay any attention to the value.

The Set­Window­Subclass function does pay attention to the uIdSubclass. If you call Set­Window­Subclass 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 dwRefData.)

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 Set­Window­Subclass 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.