简体   繁体   中英

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM