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:

Property Method
Menu SetMenu(hwnd, hmenuNew) + destroy the old menu
Background Override WM_ERASEBKGND
Cursor Override WM_SETCURSOR
Icon SendMessage(hwnd, WM_SETICON, iconSize, (LPARAM)hiconNew)

0 comments

Discussion is closed.

Feedback usabilla icon