从windows 8开始,WS_EX_LAYERED可以在子控件上使用(MSDN是这样说的),但是我一直无法使它工作。在下面的代码中,我试图使子控件变得半透明,但是当在控件上使用WS_EX_LAYERED时,没有绘制任何内容。
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); 在本例中,我使用的是自定义控件,但我尝试使用具有相同结果的WC_BUTTON。控件无法绘制。但我可以使主窗口透明,没有问题。
使用WINDOWS 10和VS2015以及普通win32 (无MFC、ATL等)
发布于 2017-03-03 03:43:29
“多亏了这个链接,”汉斯建议我找到了答案。需要至少指定Windows 8兼容性的清单条目(子层支持仅从Windows 8开始)。对于希望使用分层子窗口的任何人,都应将以下内容作为清单文件。
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
<application>
<!--The ID below indicates app support for Windows 8 -->
<supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}"/>
</application>
</compatibility>
<dependency>
<dependentAssembly>
<assemblyIdentity
type="win32"
name="Microsoft.Windows.Common-Controls"
version="6.0.0.0"
processorArchitecture="*"
publicKeyToken="6595b64144ccf1df"
language="*"
/>
</dependentAssembly>
</dependency>
</assembly>为了完整起见,我已经包含了整个文件,但相关的标记是指定GUID 8的<compatibility>元素。
您也可以声明其他操作系统版本的兼容性,如docs页面"针对Windows的应用程序“中所描述的。
https://stackoverflow.com/questions/42569348
复制相似问题