版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/duiplus/article/details/8032315
自从Windows Vista系统引入窗口管理器(WDM)的概念后,Aero效果几乎成了Windows界面的灵魂。对于程序来说如何利用好这个天然的美化特效呢?
微软提供一套API用于窗口管理相关的开发,均以Dwm开头,下面分别介绍一下:
1、判断是否开启Aero特效
HRESULT WINAPI DwmIsCompositionEnabled(
_Out_ BOOL *pfEnabled
);
使用例子:
BOOL IsCompositionEnabled()
{
HMODULE hMod = ::LoadLibrary(L"dwmapi.dll");
BOOL bRet = FALSE;
if (NULL != library)
{
if (NULL != ::GetProcAddress(hMod, "DwmIsCompositionEnabled"))
{
BOOL bEnabled = FALSE;
bRet = SUCCEEDED(::DwmIsCompositionEnabled(&bEnabled)) && bEnabled;
}
VERIFY(::FreeLibrary(hMod));
}
return bRet;
}
2、启用Aero特效:
HRESULT WINAPI DwmEnableComposition(
UINT uCompositionAction
);
使用例子:
HRESULT hr = S_OK;
// Disable DWM Composition
hr = DwmEnableComposition(DWM_EC_DISABLECOMPOSITION);
if (SUCCEEDED(hr))
{
// ...
}
3、启用合成特效:
HRESULT WINAPI DwmEnableBlurBehindWindow(
HWND hWnd,
_In_ const DWM_BLURBEHIND *pBlurBehind
);
typedef struct _DWM_BLURBEHIND {
DWORD dwFlags; // 标识DWM_BB_ENABLE|DWM_BB_BLURREGION
BOOL fEnable; // 是否启用
HRGN hRgnBlur; // 合成RGN区域
BOOL fTransitionOnMaximized;
} DWM_BLURBEHIND, *PDWM_BLURBEHIND;
使用例子:
HRESULT EnableBlurBehind(HWND hwnd)
{
HRESULT hr = S_OK;
// Create and populate the Blur Behind structure
DWM_BLURBEHIND bb = {0};
// Enable Blur Behind and apply to the entire client area
bb.dwFlags = DWM_BB_ENABLE;
bb.fEnable = true;
bb.hRgnBlur = NULL;
// Apply Blur Behind
hr = DwmEnableBlurBehindWindow(hwnd, &bb);
if (SUCCEEDED(hr))
{
// ...
}
return hr;
}
附录:启用Aero特效以后,窗口上绘制的文字会看不清,需要用下面的函数来替换DrawText和TextOut函数
void DrawAeroText(HDC hDC, LPCTSTR pstrText, int nLen, RECT rcText, DWORD dwTextFlags, int iGlowSize)
{
//获取主题句柄
HTHEME hThm = OpenThemeData(GetDesktopWindow(), L"TextStyle");
DTTOPTS dttopts = {0};
dttopts.dwSize = sizeof(DTTOPTS);
dttopts.dwFlags = DTT_GLOWSIZE | DTT_COMPOSITED;
dttopts.iGlowSize = iGlowSize; //发光的范围大小
//绘制文本
HRESULT hr = DrawThemeTextEx(hThm, hDC, TEXT_LABEL, 0, pstrText, -1, dwTextFlags , &rcText, &dttopts);
if(FAILED(hr)) return;
CloseThemeData(hThm);
}
By Troy
联系我:QQ(656067418)