tags:

views:

28

answers:

2

In the winAPI, how do I change the window background colour?

For example,

wc.hbrBackground = ....;

is for setting the window background initially, but how do I change it there after?

Thanks.

+1  A: 

Use the SetClassLongPtr function with the GCLP_HBRBACKGROUND argument:

SetClassLongPtr(windowHandle, GCLP_HBRBACKGROUND, brushHandle);

http://msdn.microsoft.com/en-us/library/ms633589%28VS.85%29.aspx

PigBen
A: 

Be wary of using the technique described by PigBen. It will change the background color for all instances of that window class, unless they implement WM_PAINT/WM_ERASBKGND that overrides the windows' default background.

I would make the extra effort of implementing WM_ERASEBKGND for your window, and draw the background explicitly. That way you have full control of the background color, and you can have different colors in different window instances.

Jörgen Sigvardsson
Thankyou, but this solution is less practical for my project, as I will only have one instance of each class.
Alexander Rafferty