5

我正在为某些部分使用 QML 创建一个 Qt/C++ 应用程序。在 windows 下,我想使用 ExtendFrameIntoClientArea 的半透明窗口,如我的窗口类的这个片段中所示。

#ifdef Q_WS_WIN
    if ( QSysInfo::windowsVersion() == QSysInfo::WV_VISTA ||
         QSysInfo::windowsVersion() == QSysInfo::WV_WINDOWS7 )
    {
        EnableBlurBehindWidget(this, true);
        ExtendFrameIntoClientArea(this);
    }
#else

该代码运行良好,但有一个例外。如果关闭透明窗口系统,背景会变黑,并且作为我的 UI 的一部分是透明的,它也会变暗。登录到运行应用程序的远程计算机时也会发生同样的事情,即使透明窗口系统立即重新初始化,背景也会保持黑色,直到再次执行上述代码。这在这张图片中得到了展示:失败的渲染(在背景中)和正确的(在前面)的比较。

问题是找到一个信号来连接重新初始化透明窗口,或者更好地检测窗口何时被透明绘制并相应地绘制 UI。也欢迎任何替代解决方案。

4

2 回答 2

2

在研究了 Qt 和MSDN Aero 文档之后,我想出了一个两步解决方案。通过覆盖winEvent我的主窗口的方法,我能够接收到每次启用或禁用半透明窗口系统时触发的信号。

#define WM_DWMCOMPOSITIONCHANGED        0x031E

bool MainWindow::winEvent(MSG *message, long *result) {
    if ( message->message == WM_DWMCOMPOSITIONCHANGED ) {
        // window manager signaled change in composition
        return true;
    }
    return false;
}

这让我非常接近,但它并没有告诉我 DWM 当前是否正在绘制透明窗口。通过使用dwmapi.dll,我能够找到一个完全可以做到这一点的方法,并且可以像下面这样访问它:

// QtDwmApi.cpp
extern "C"
{
    typedef HRESULT (WINAPI *t_DwmIsCompositionEnabled)(BOOL *pfEnabled);
}

bool DwmIsCompositionEnabled() {
    HMODULE shell;

    shell = LoadLibrary(L"dwmapi.dll");
    if (shell) {
        BOOL enabled;
        t_DwmIsCompositionEnabled is_composition_enabled = \
              reinterpret_cast<t_DwmIsCompositionEnabled>(
                  GetProcAddress (shell, "DwmIsCompositionEnabled")
                  );
        is_composition_enabled(&enabled);

        FreeLibrary (shell);

        if ( enabled ) {
            return true;
        } else {
            return false;
        }
    }
    return false;
}

我的实现现在能够对 Aero 中的变化做出反应并相应地绘制 GUI。通过远程桌面登录时,窗口也会在可用的情况下使用透明度绘制。

于 2012-07-23T13:35:03.923 回答
0
The function should be written as follows to avoid the GPA failure

// QtDwmApi.cpp
extern "C"
{
    typedef HRESULT (WINAPI *t_DwmIsCompositionEnabled)(BOOL *pfEnabled);
}

bool DwmIsCompositionEnabled() {
    HMODULE shell;
    BOOL enabled=false;

    shell = LoadLibrary(L"dwmapi.dll");
    if (shell) {
        t_DwmIsCompositionEnabled is_composition_enabled = \
              reinterpret_cast<t_DwmIsCompositionEnabled>(
                  GetProcAddress (shell, "DwmIsCompositionEnabled")
                  );
        if (is_composition_enabled)
            is_composition_enabled(&enabled);

        FreeLibrary (shell);
   }
    return enabled;
}
于 2014-02-24T07:15:48.623 回答