处理WM_NCPAINT WinEvent时如何保持空气玻璃

时间:2013-05-20 17:17:51

标签: c++ qt button dwm nonclient-area

我正在处理WM_NCPAINT WinEvent,用于在窗口的非客户区域绘制按钮。但正如您在下图所示,航空玻璃边框消失了,我的窗户没有边框。所以我找到了这个答案:Handling WM_NCPAINT "breaks" DWM glass rendering on Vista/Aero我尝试将该页面上提出的解决方案应用于我的项目而没有结果。我做错了什么?

我的窗口截图:

enter image description here

在我的.pro文件中,我添加了:

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

这是我的代码:

#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 个答案:

没有答案