(HBRUSH) COLOR_WINDOW

I would like to know what the next line of code does...

(HBRUSH) COLOR_WINDOW

... and what is HBRUSH to COLOR_WINDOW and why is HBRUSH in brackets?
why is HBRUSH in brackets?

That is the C-style notation for a cast

(HBRUSH)(COLOR_WINDOW + 1)).

Casts can also be written using function style

(HBRUSH(COLOR_WINDOW + 1))

and using the more modern (and preferred) C++ casts: static_cast<>, reinterpret_cast<>, etc.

See this part of the tutorial for more info:

Type Casting
http://www.cplusplus.com/doc/tutorial/typecasting/

I would like to know what the next line of code does...

The code is casting a integer identifier (for a color) to an HBRUSH (the handle to a GDI brush).

These days you should only ever come across this when registering a window class; you set the hbrBackground member of the WNDCLASSEX struct (e.g. wcex)

wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);

The +1 must be used; it shifts the values by 1 so RegisterClassEx() can tell the difference between CTLCOLOR_MSGBOX (which has value 0) and NULL (which is also 0.)

RegisterClassEx() can tell the difference between a color ID (with a value of 0, 1, 2, ... up to 30 at the moment) and a brush handle (which you get when you create a brush using CreateSolidBrush(), etc) as actual handle values are much bigger, e.g. 0x72102280 (2299531404 as an unsigned int, or -1995435892 as a signed one.)

Andy
Last edited on
thank you so much
Topic archived. No new replies allowed.