How to keep aero glass when handling WM_NCPAINT WinEvent

1.5k Views Asked by At

I’m handling the WM_NCPAINT WinEvent for draw a pushbutton at the non client area of my window. But as you can see at the following image, the aero glass border has disappeared, and my window has no border. So I've found this answer: Handling WM_NCPAINT "breaks" DWM glass rendering on Vista/Aero and I’ve tried to apply the solution proposed on that page on my project with no result. What I’m doing wrong?

Screenshot of my window:

enter image description here

On my .pro file I’ve added:

LIBS += -lGdi32
LIBS += -lUser32
LIBS += -lDwmApi
LIBS += -lUxTheme

And here my code:

#include <windows.h>
#include <dwmapi.h>
#include <Uxtheme.h>
#include <tmschema.h>
#include <Strsafe.h>
#include <limits.h>

HTHEME m_htheme;

bool MainWindow::winEvent(MSG *pMessage, long *result)
{
    UINT m = pMessage->message;
    if(m == WM_NCPAINT || m == WM_ACTIVATE)
    {
        draw();
        return true;
    }
    return QWidget::winEvent(pMessage, result);
}

void MainWindow::draw()
{
    HWND id = winId();

    DWMNCRENDERINGPOLICY policy = DWMNCRP_ENABLED;
    HRESULT hr = DwmSetWindowAttribute(id, DWMWA_NCRENDERING_POLICY, (void*)&policy, sizeof(DWMNCRENDERINGPOLICY));

    if(SUCCEEDED(hr))
    {
        m_htheme = OpenThemeData(id, L"Button");
        if (m_htheme)
        {
            HDC hDeviceContext = GetWindowDC(id);
            RECT rc = {10, 10, 100, 32};
            RECT rcContent;

            LPCWSTR caption = TEXT("text");
            size_t cch;
            StringCchLength(caption, MAX_PATH, &cch);

            hr = DrawThemeBackground(m_htheme, hDeviceContext, BP_PUSHBUTTON, CBS_NORMAL, &rc, 0);

            if (SUCCEEDED(hr))
                hr = GetThemeBackgroundContentRect(m_htheme, hDeviceContext, BP_PUSHBUTTON,
                                                   CBS_NORMAL, &rc, &rcContent);

            if (SUCCEEDED(hr))
                hr = DrawThemeText(m_htheme, hDeviceContext, BP_PUSHBUTTON, CBS_NORMAL,
                                   caption, cch, DT_CENTER | DT_VCENTER | DT_SINGLELINE, 0, &rcContent);
        }
    }
}
0

There are 0 best solutions below