June 15th, 2006

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

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)
Topics
Code

Author

Raymond has been involved in the evolution of Windows for more than 30 years. In 2003, he began a Web site known as The Old New Thing which has grown in popularity far beyond his wildest imagination, a development which still gives him the heebie-jeebies. The Web site spawned a book, coincidentally also titled The Old New Thing (Addison Wesley 2007). He occasionally appears on the Windows Dev Docs Twitter account to tell stories which convey no useful information.

0 comments

Discussion are closed.