如何利用Win7Aero特效来美化你的程序窗口

自从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)

评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值