Window class properties apply to all windows that belong to the class

Raymond Chen


Window class properties apply to all windows that belong to the class. That’s why they’re called class properties. This seems like an obvious thing to say when put in so many words, but I see many “solutions” that lose sight of this simple fact.

All the properties that you set in the WNDCLASS (or WNDCLASSEX) are window class properties, as are the properties that you can access via Get/SetClassWord/Long/LongPtr. This means that when you change those properties, they affect the entire class. For example, if you write

SetClassLongPtr(hwnd, GCLP_HCURSOR, (LONG_PTR)hcurNew);

then you aren’t just changing the cursor for the window specified by hwnd. You’re changing the cursor for all windows of the same class as hwnd. For example, if hwnd is an edit control, then you changed the default cursor for all edit controls.

But what if you want to change a class property for just one particular window instead of for all windows of a class?

If you want to change the menu, background, cursor, or icon for a particular window, you can override the class default on a per-window basis:

MenuSetMenu(hwnd, hmenuNew) + destroy the old menu
BackgroundOverride WM_ERASEBKGND
CursorOverride WM_SETCURSOR
IconSendMessage(hwnd, WM_SETICON, iconSize, (LPARAM)hiconNew)


Comments are closed. Login to edit/delete your existing comments