Click here to Skip to main content
11,647,170 members (70,617 online)
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 10:33am
Igor-84603
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 jyo.net 500
1 OriginalGriff 337
2 DamithSL 310
3 Sergey Alexandrovich Kryukov 265
4 CPallini 249
0 OriginalGriff 1,277
1 jyo.net 994
2 DamithSL 946
3 Sergey Alexandrovich Kryukov 853
4 CPallini 760


Advertise | Privacy | Mobile
Web04 | 2.8.150804.2 | Last Updated 15 Jun 2013
Copyright © CodeProject, 1999-2015
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