Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ Windows
I created a window using CreateWindowEx() function.
On this window I place static control which was created like that:
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:
//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 15-Jun-13 11:33am
Igor-84487
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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:
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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Ian A Davidson, great thanks! The problem was here:
1. instead of:
SetBkColor(hdcStatic,TRANSPARENT);
needed to write:
SetBkMode(hdcStatic,TRANSPARENT);
2. here I returned null brush:
return (INT_PTR)(HBRUSH)GetStockObject(NULL_BRUSH);
but needed to return valid background color:
HBRUSH BGColorBrush = CreateSolidBrush(RGB(230,230,230));
//...
return (LRESULT)BGColorBrush;
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 325
1 DamithSL 265
2 CPallini 235
3 Maciej Los 190
4 Sergey Alexandrovich Kryukov 189
0 OriginalGriff 5,455
1 DamithSL 4,422
2 Maciej Los 3,860
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,010


Advertise | Privacy | Mobile
Web01 | 2.8.141216.1 | Last Updated 15 Jun 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100