Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QT Translucent window and remote desktop

I'm creating a Qt/C++ application using QML for some parts. Under windows I would like to make use of the translucent windowing using ExtendFrameIntoClientArea as seen in this snippet from my window class.

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

The code is working nicely with one exception. If the transparent window system is turned off the background becomes black, and as part of my UI is transparent it becomes dark as well. The same thing happens when logging in to a remote computer running the application, even if the transparent windowing system is re-initialized immediately the background stays black until the above code is executed again. This is demonstrated in this image: Comparison of failed rendering (in background) and correct (in front).

The problem is to find a signal to connect to re-initializing the transparent window, or better yet to detect when windows are drawn transparently and draw the UI accordingly. Any alternative solutions are also welcome.

like image 924
hg. Avatar asked Nov 04 '22 20:11

hg.


1 Answers

After digging around in both Qt and MSDN Aero documentation I came up with a two step solution. By overriding the winEvent method of my main window, I was able to receive the signal which is triggered every time the translucent window system is enabled or disabled.

#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;
}

That took me quite close, but it didn't tell me whether DWM was currently drawing transparent windows or not. By using dwmapi.dll I was able to find a method which does exactly that, and it can be accessed like below:

// 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;
}

My implementation is now able to react to changes in Aero and draw the GUI accordingly. When logging in over remote desktop the window is drawn using transparency as well where available.

like image 136
hg. Avatar answered Nov 12 '22 18:11

hg.