2017-03-03 46 views
2

由于Windows 8,WS_EX_LAYERED可用于子控件,(如此说MSDN)但是我一直无法使它工作。在下面的代码中,我试图让子控件半透明,但是在控件上使用WS_EX_LAYERED时,什么都不绘制。如何在子控件上使用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等)

+1

考虑从[已知良好的代码(HTTPS工作的GUID:// github.com/Microsoft/Windows-classic-samples/tree/master/Samples/DirectCompositionLayeredChildWindow) –

+1

传递一个颜色键值到'SetLayeredWindowAttributes'是没有意义的,除非你也传递'LWA_COLORKEY'标志。 – IInspectable

回答

3

感谢链接@Hans建议我已经找到了答案。清单是必需的,指定了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 application support for Windows Developer Preview --> 
     <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> 

为了完整起见,我已经包括了整个文件,但相关的标签是<compatibility>指定为Windows 8

相关问题