Alexander Rafferty
Alexander Rafferty

Reputation: 6233

Changing window background colour

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.

Upvotes: 4

Views: 9974

Answers (2)

Jörgen Sigvardsson
Jörgen Sigvardsson

Reputation: 4887

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.

Upvotes: 4

Benjamin Lindley
Benjamin Lindley

Reputation: 103693

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

Upvotes: 7

Related Questions