Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
I created a window using CreateWindowEx() function.
On this window I place static control which was created like that:
C++
HWND hStatic;
hStatic = CreateWindowEx( WS_EX_TRANSPARENT ,
TEXT("Static"),
TEXT("text"),
WS_CHILD ,
10,
80,
250,
70,
CtrlsWndHnd, //parent
NULL,
hInstance,
NULL);
ShowWindow(hStatic,SW_SHOW);
UpdateWindow(hStatic);

Control has been successfully created.
And now I try to make transparent background using this code:
C++
//inside parent window procedure:
case WM_CTLCOLORSTATIC:
{
HDC hdcStatic = (HDC) wParam;
SetBkColor(hdcStatic,TRANSPARENT);
return (INT_PTR)(HBRUSH)GetStockObject(NULL_BRUSH);
}
break;

But result is black background of static control.
What I did wrong? Where is mistake?
Posted

My initial guess is that you need to handle the WM_ERASEBKGND message for the control, and return non-zero to indicate that the background doesn't need to be erased.

It also looks as if you are probably setting the background colour to black yourself (SetBkColor takes a COLORREF, so "TRANSPARENT" is probably interpreted as a black, or near black, colour). You need to set the background mode to transparent instead:
C++
SetBkMode (hdcStatic, TRANSPARENT);


However, it seems you shouldn't be returning a null brush, according to the following link, where exactly the same question was asked, and seems to be solved: http://www.cplusplus.com/forum/windows/73999/[^]

Regards,
Ian.
 
Share this answer
 
Ian A Davidson, great thanks! The problem was here:
1. instead of:
C++
SetBkColor(hdcStatic,TRANSPARENT);

needed to write:
C++
SetBkMode(hdcStatic,TRANSPARENT);

2. here I returned null brush:
C++
return (INT_PTR)(HBRUSH)GetStockObject(NULL_BRUSH);

but needed to return valid background color:
C++
HBRUSH BGColorBrush = CreateSolidBrush(RGB(230,230,230));
//...
return (LRESULT)BGColorBrush;
 
Share this answer
 
Comments
thepen 28-Sep-22 10:31am    
Actually, NULL_BRUSH return worked for me, not the other.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900