首页 > 解决方案 > 带有 WS_EX_LAYERED 的 Qt 透明子窗口总是失败

问题描述

我通过创建子窗口并将窗口句柄传递给 libVLC 以呈现视频,将 libVLC 集成到 Windows 上的 Qt Quick 应用程序中。

这工作正常。

但是,我正在尝试使用该WS_EX_LAYERED样式创建子窗口,以便对其应用一些透明度。我根本无法让它工作。

这是代码:

    const auto parentWindow = quickWindow()->winId(); //The native window handle

    const auto windowClassName = QString("VLCWindow-").append(QString::number(reinterpret_cast<int64_t>(this), 16)).toStdWString();
    WNDCLASSEXW wcx = {};
    wcx.cbSize = sizeof(wcx);
    wcx.style = CS_HREDRAW | CS_VREDRAW;
    wcx.lpfnWndProc = DefWindowProcW;
    wcx.hbrBackground = static_cast<HBRUSH>(::GetStockObject(BLACK_BRUSH));
    wcx.hInstance = ::GetModuleHandle(nullptr);
    wcx.lpszClassName = windowClassName.data();
    if (!::RegisterClassExW(&wcx)) {
        const auto lastError = ::GetLastError();
        if (lastError != ERROR_CLASS_ALREADY_EXISTS) {
            qCritical() << "RegisterClassEx failed:" << lastError;
            return;
        }
    }
    const auto nativeWindowHandle = ::CreateWindowExW(WS_EX_LAYERED, wcx.lpszClassName, L"", WS_CHILD | WS_CLIPSIBLINGS, 0, 0, 0, 0, reinterpret_cast<HWND>(parentWindow), nullptr, ::GetModuleHandle(nullptr), nullptr);
    if (nativeWindowHandle) {
        qInfo() << "Created native window for VLC rendering; handle:" << nativeWindowHandle;
    } else {
        auto err = ::GetLastError();
        qCritical() << "CreateWindowEx failed:" << err;
        return;
    }

以上CreateWindowExW总是失败并GetLastEror()返回 0!一旦我删除WS_EX_LAYERED,一切都很好,但我需要WS_EX_LAYERED. 我在 Windows 10 上运行代码,因此WS_EX_LAYERED对 pre-Windows 8 的限制不适用。

我尝试了以下方法:

非常感谢!

标签: c++qtwinapilibvlcwin32gui

解决方案


推荐阅读