Since windows 8, WS_EX_LAYERED is available to use on child controls, (so says MSDN) However I've been unable to make it work. In the following code, I'm trying to make a child control semi transparent but when WS_EX_LAYERED is used on the control, nothing draws.
int APIENTRY wWinMain(_In_ HINSTANCE hInst, _In_opt_ HINSTANCE hPrevInst, _In_ LPWSTR lpCmdLine, _In_ int nCmdShow)
{
WNDCLASSEX wc = {};
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WndProc;
wc.hInstance = hInst;
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wc.lpszClassName = _T("main");
wc.hCursor = LoadCursor(0, IDC_ARROW);
RegisterClassEx(&wc);
HWND MWhwnd = CreateWindowEx(NULL, _T("main"), _T(""),
WS_OVERLAPPEDWINDOW| WS_CLIPCHILDREN,
CW_USEDEFAULT, 0, CW_USEDEFAULT,0, NULL, NULL, hInst, NULL);
wc.lpfnWndProc = WndProcPanel;
wc.lpszClassName = _T("CPanel");
wc.style = NULL;
RegisterClassEx(&wc);
HWND Panelhwnd = CreateWindowEx(WS_EX_LAYERED, _T("CPanel"), _T(""),
WS_VISIBLE | WS_CHILD | WS_CLIPSIBLINGS| WS_CLIPCHILDREN,
100, 10, 400, 400, MWhwnd, NULL, hInst, NULL);
COLORREF crefKey = RGB(0, 255, 0);
SetLayeredWindowAttributes(Panelhwnd, crefKey, 155, LWA_ALPHA);
ShowWindow(MWhwnd, nCmdShow);
In this example, I'm using a custom control but I've tried with a WC_BUTTON
with same result. The control fails to draw. But I can make the main window transparent without a problem.
Using WINDOWS 10 and VS2015 and plain win32 (no MFC, ATL etc)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…