示例代码:
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);