我正在处理 WM_NCPAINT WinEvent 以在窗口的非客户区绘制一个按钮。但如下图所示,航空玻璃边框消失了,我的窗口没有边框。所以我找到了这个答案:Handling WM_NCPAINT "breaks" DWM glass rendering on Vista/Aero,我尝试在我的项目中应用该页面上提出的解决方案,但没有结果。我做错了什么?
我的窗口截图:
在我的 .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);
}
}
}