Qt 中打开玻璃化DWM效果

我在开发Qt Windows程序时,加入玻璃化效果


#ifdef _SAFE_BROWSER_ENABLE_GLASS_EFFECT
#include 
// WARNING: disable -O2, or application will CRASH at startup!!
void setGlassEffect(QWidget *w)
{
    typedef HRESULT (STDAPICALLTYPE *enableBlurBehindWindow)(HWND, const DWM_BLURBEHIND *);
    typedef HRESULT (STDAPICALLTYPE *extendFrameIntoClientArea)(HWND, MARGINS *);
    enableBlurBehindWindow f1 =
        (enableBlurBehindWindow)QLibrary::resolve("dwmapi", "DwmEnableBlurBehindWindow");
    extendFrameIntoClientArea f2 =
        (extendFrameIntoClientArea)QLibrary::resolve("dwmapi", "DwmExtendFrameIntoClientArea");

    DWM_BLURBEHIND bb = {0};
    MARGINS margins = {-1};
    bb.dwFlags = DWM_BB_ENABLE | DWM_BB_BLURREGION;
    if(f1)f1(w->winId(), &bb);
    if(f2)f2(w->winId(), &margins);
}
#endif

注意还需要在Widget中修改paintEvent

void SafeBrowser::paintEvent(QPaintEvent *e)
{
    QMainWindow::paintEvent(e);

    QPainter p(this);
#ifdef _SAFE_BROWSER_ENABLE_GLASS_EFFECT
    // this is used to enable glass effect.
    p.setCompositionMode(QPainter::CompositionMode_DestinationIn);
    p.fillRect(rect(), QColor(0, 0, 0, 0));
#endif
}

这样才能完全正常的显示玻璃化效果


这个代码我在Win7下debug模式运行完全没有问题

但是在release模式下就会崩溃

很奇怪,最后定位到了-O2的问题,优化发生了歧义,看汇编代码貌似是冲突了,进入死循环了…然后栈溢出,程序崩溃…

郁闷,找了半天是定义少了stdcall (STDAPICALLTYPE)

最后贴一个我做得浏览器效果图,还不错哈?


你可能感兴趣的:(Qt 中打开玻璃化DWM效果)