Win32 STATIC controls background

This is my first time trying to build an interface with pure winapi, everything is going fine except for an issue with static controls backgrounds.
I have several STATIC controls, I am able to set the background transparent for all of them with
1
2
3
4
5
6
7
8
case WM_CTLCOLORSTATIC:

			HDC hdcStatic = (HDC) WParam; 
			SetTextColor(hdcStatic, RGB(0,0,0));  
			SetBkMode (hdcStatic, TRANSPARENT);

			return (LRESULT)GetStockObject(NULL_BRUSH);
		}
The problem is, in one of the controls I am displaying a counter which changes during run time. When this controls background is transparent the text I send to it bleeds over the previous text http://imageshack.us/photo/my-images/403/genslabelissue.png/

I tried invalidating the rect with
1
2
3
4
GetClientRect(lblGensCount, &rect);
InvalidateRect(lblGensCount, &rect, true);
MapWindowPoints(lblGensCount, Window, (POINT *) &rect, 2);
RedrawWindow(Window, &rect, NULL, RDW_ERASE | RDW_INVALIDATE);
which seems to reduce the problem slightly but does not solve it. Ideally I would like to have a transparent background without the bleed over, but am willing to settle for having the one control not have the transparency. So the question is, how do I either prevent the bleed or keep the specific controls background from being transparent. Any thoughts/suggestions/ideas are appreciated.
Last edited on
You are returning a null brush. You need to return the correct background brush.
Ah, thank you, that does definitely help. Now I have
1
2
3
4
5
6
7
8
9
10
WindowClass.hbrBackground = reinterpret_cast<HBRUSH>(COLOR_BACKGROUND);
...
case WM_CTLCOLORSTATIC:

			HDC hdcStatic = (HDC) WParam; 
			
			SetTextColor(hdcStatic, RGB(0,0,0));  
			SetBkMode (hdcStatic, COLOR_BACKGROUND);

			return (LRESULT)GetStockObject(COLOR_BACKGROUND);
the color is close but not quite right but looks a lot better then it did before.
That first line doesn't look right. I guess it is for class registration. What is COLOR_BACKGROUND? If it isn't a brush, that code is wrong, I would say. If COLOR_BACKGROUND is a system color, the assignment needs to be (COLOR_BACKGROUND + 1);, but I don't think that's a system color. That's more likely a color of yours. If it is a color of yours, you first need to create the brush using CreateSolidBrush() and then assign it as background.

You can reuse the brush if you save it in a global or otherwise accessible variable.
Thank you that solved the problem perfectly. I started with a minimal window code I found online and they where using the COLOR_BACKGROUND which I also saw on this page http://msdn.microsoft.com/en-us/library/windows/desktop/ms724371%28v=vs.85%29.aspx so I thought it was a valid color. I now have
1
2
3
4
5
6
7
8
9
10
11
hBrush = CreateSolidBrush(RGB(230,230,230));
...
WindowClass.hbrBackground = hBrush;
...
case WM_CTLCOLORSTATIC:
			
	HDC hdcStatic = (HDC) WParam; 
	SetTextColor(hdcStatic, RGB(0,0,0));  
	SetBkMode (hdcStatic, TRANSPARENT);

	return (LRESULT)hBrush;
and it works great.
COLOR_BACKGROUND is listed as a system color, so I guess you can do (COLOR_BACKGROUND + 1). In any case, I guess you are all set up now.
Topic archived. No new replies allowed.