Background color of your controls

The place for threads about TimoSoft ButtonControls.
Post Reply
Lt. Commander
Posts: 54
Joined: 17 Feb 2009, 01:41

Background color of your controls

Post by TickTick »

I want to make a label that works with your controls, but I am unable to get the background color in a trustworthy way.

I tried this

Dim lBackColor&
If UserControl.ContainerHwnd = UserControl.Parent.hWnd Then 'we are sitting right on top of the form
lBackColor = UserControl.Parent.BackColor
Else 'we are inside a container
Dim lDC&
lDC = GetDC(UserControl.ContainerHwnd)
lBackColor = GetBkColor(lDC)
ReleaseDC UserControl.ContainerHwnd, lDC
End If
UserControl.BackColor = lBackColor

... and it works fine for the TabControl, but for your frame, I am also getting a white-background color, so I am having a white label on a grey-like frame...

Can you give me a hint how to retrieve the "correct" background color?

Thank you!
User avatar
Posts: 832
Joined: 28 Sep 2004, 21:10
Location: München

Re: Background color of your controls

Post by TiKu »

TickTick wrote:... and it works fine for the TabControl
It doesn't, at least it does not if the tab control is themed. Themed tab controls (actually that's true for any themed control) do not necessarily have a solid background. Take the Luna theme of Windows XP: With this theme the tab control has a gradient background, not a single solid back color.

To support themes properly, you must use the DrawThemeParentBackground API function.
My CheckBox control uses this code to react to the WM_CTLCOLORSTATIC message:

Code: Select all

LRESULT CheckBox::OnReflectedCtlColorStatic(UINT /*message*/, WPARAM wParam, LPARAM lParam, BOOL& /*wasHandled*/)
	SetBkColor(reinterpret_cast<HDC>(wParam), OLECOLOR2COLORREF(properties.backColor));
	SetTextColor(reinterpret_cast<HDC>(wParam), OLECOLOR2COLORREF(properties.foreColor));
	if(IsComctl32Version600OrNewer()) {
		if(flags.invalidBackBrush) {
			WTL::CRect clientRectangle;
			::GetClientRect(reinterpret_cast<HWND>(lParam), &clientRectangle);

			CDC memoryDC;
			CBitmap memoryBitmap;
			memoryBitmap.CreateCompatibleBitmap(reinterpret_cast<HDC>(wParam), clientRectangle.Width(), clientRectangle.Height());
			HBITMAP hPreviousBitmap = memoryDC.SelectBitmap(memoryBitmap);

			CTheme themingEngine;
			if(flags.usingThemes) {
				themingEngine.OpenThemeData(*this, VSCLASS_BUTTON);
			if(themingEngine.IsThemeNull()) {
				if(hBackColorBrush) {
					memoryDC.FillRect(&clientRectangle, hBackColorBrush);
				} else {
					memoryDC.FillRect(&clientRectangle, GetSysColorBrush(properties.backColor & 0x0FFFFFFF));
			} else {
				memoryDC.FillRect(&clientRectangle, GetSysColorBrush(COLOR_3DFACE));
			DrawThemeParentBackground(reinterpret_cast<HWND>(lParam), memoryDC, &clientRectangle);

			if(!themedBackBrush.IsNull()) {
			flags.invalidBackBrush = FALSE;
		return reinterpret_cast<LRESULT>(static_cast<HBRUSH>(themedBackBrush));
	if(hBackColorBrush) {
		return reinterpret_cast<LRESULT>(hBackColorBrush);
	} else {
		return reinterpret_cast<LRESULT>(GetSysColorBrush(properties.backColor & 0x0FFFFFFF));
Crunching for Fab36_Folding-Division at Folding@Home. Join Fab36/Fab30! - Folding@Home and BOINC
Boycott DRM! Boycott HDCP!
Post Reply