How do I get the main window's background HBRUSH to work

I have this global variable called hBrushBackground.

In my WinMain: I have put hBrushBackground = CreateSolidBrush(0) and set the wndclass.hbrbackground = hBrushBackground.

Now I have a scroll bar child window where it changes the color of the main window's background by:
DeleteObject(hBrushBackground)
hBrushBackground = CreateSolidBrush( RGB(value, 0, value) ) // value being between 0-255
InalidateRect(hwnd, NULL, true)

The window starts out black but when I change the scroll bar position, the main window's background goes white and stays white. Why is the background not turning slowly to purple when I increase value?

This is my code as a reference: http://codepad.org/NcrCkUp3
Last edited on
It looks like you should probably be doing your own background handling?

Use NULL for the brush when you register your class, then handle WM_ERASEBKGND (using you brush to fill the window rect)

As it stands, you're deleting and creating brushes but never tell the window to use the new brush. You can do it with SetWindowLongPtr + GCLP_HBRBACKGROUND, but I would not use this approach if I was swapping the color as freq as you seem to want to.

Andy

P.S. If you are using routine colors, rather than creating a brand new one using CreateSolidBrush you could use a "stock" object:

1
2
3
4
5
// for system colors
hBrushStatic = GetSysColorBrush(COLOR_BTNHIGHLIGHT);

// for black, white, and gray brushes
hBrushBlack = (HBRUSH)GetStockObject(BLACK_BRUSH);
Last edited on
Topic archived. No new replies allowed.