QWidget 添加边框阴影

21 篇文章 0 订阅

      示例代码:  

WindWMAPI.h

#ifndef WINDWMAPI_H
#define WINDWMAPI_H

#include <windows.h>

//********************************************窗口阴影类***************************************

typedef struct _MARGINS
{
    int cxLeftWidth;      // width of left border that retains its size
    int cxRightWidth;     // width of right border that retains its size
    int cyTopHeight;      // height of top border that retains its size
    int cyBottomHeight;   // height of bottom border that retains its size
} MARGINS, *PMARGINS;

class WinDwmapi
{
public:
    WinDwmapi();
    ~WinDwmapi();

    typedef HRESULT (WINAPI* DwmIsCompositionEnabledPtr)(BOOL* pfEnabled);
    typedef HRESULT (WINAPI* DwmExtendFrameIntoClientAreaPtr)(HWND hWnd, const MARGINS *pMarInset);

    HRESULT DwmIsCompositionEnabled(BOOL* pfEnabled) const;
    HRESULT DwmExtendFrameIntoClientArea(HWND hWnd, const MARGINS *pMarInset) const;

    static const WinDwmapi* instance();

private:
      DwmIsCompositionEnabledPtr      dwm_is_composition_enabled_;
      DwmExtendFrameIntoClientAreaPtr dwm_extendframe_into_client_area_;
      HMODULE dwmapi_dll_;
};

#endif // WINDWMAPI_H

WindWMAPI.cpp

#include "windwmapi.h"

WinDwmapi::WinDwmapi()
    : dwmapi_dll_(LoadLibraryW(L"dwmapi.dll"))
    , dwm_is_composition_enabled_(NULL)
{
    if (dwmapi_dll_) 
    {
        dwm_is_composition_enabled_       = reinterpret_cast<DwmIsCompositionEnabledPtr>(GetProcAddress(dwmapi_dll_, "DwmIsCompositionEnabled"));
        dwm_extendframe_into_client_area_ = reinterpret_cast<DwmExtendFrameIntoClientAreaPtr>(GetProcAddress(dwmapi_dll_, "DwmExtendFrameIntoClientArea"));
    }
}

WinDwmapi::~WinDwmapi()
{
    if (dwmapi_dll_) 
    {
        FreeLibrary(dwmapi_dll_);
    }
}

HRESULT WinDwmapi::DwmIsCompositionEnabled(BOOL *pfEnabled) const
{
    if (dwm_is_composition_enabled_) 
    {
        return dwm_is_composition_enabled_(pfEnabled);
    }

    return E_NOTIMPL;
}

HRESULT WinDwmapi::DwmExtendFrameIntoClientArea(HWND hWnd, const MARGINS *pMarInset) const
{
    if (dwm_extendframe_into_client_area_) 
    {
        return dwm_extendframe_into_client_area_(hWnd, pMarInset);
    }

    return E_NOTIMPL;
}

const WinDwmapi *WinDwmapi::instance()
{
    static const WinDwmapi s_dwmapi;
    return &s_dwmapi;
}

用法:

      HWND hwnd = (HWND)this->winId();
        DWORD style = ::GetWindowLong(hwnd, GWL_STYLE);
        // 此行代码可以带回Aero效果,同时也带回了标题栏和边框,在nativeEvent()中再次去掉标题栏
        ::SetWindowLong(hwnd, GWL_STYLE, style | WS_THICKFRAME | WS_CAPTION);
        const MARGINS shadow = { 1, 1, 1, 1 }; //边框保留1px以绘制阴影
        WinDwmapi::instance()->DwmExtendFrameIntoClientArea(HWND(winId()), &shadow);
        setFixedSize(frameDlgWidth - 16, frameDlgHeight - 39);

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值