设置窗口透明效果
设置窗口透明一般都能用分层窗体来实现,这样的代码到网上一搜一大把的,我在这里简单地封装一下以供以后自己方便地使用。
#ifndef _WINDOW_TRANSPARENT_H_
#define _WINDOW_TRANSPARENT_H_
#ifndef WS_EX_LAYERED
#define LWA_COLORKEY 0x00000001
#define LWA_ALPHA 0x00000002
#define WS_EX_LAYERED 0x00080000
#endif
//分层窗口只在WIndows 2000 及以后版本有效,故用 _WIN32_WINNT >= 0x0500做判断
class CWindowTransparent
{
typedef BOOL (WINAPI *LPFNSETLAYEREDWINDOWATTRIBUTES)(HWND hWnd, COLORREF crKey, BYTE bAlpha, DWORD dwFlags);
private:
HMODULE m_hModUser32;
LPFNSETLAYEREDWINDOWATTRIBUTES lpfnSetLayeredWindowAttributes;
public:
CWindowTransparent( )
{
#if _WIN32_WINNT >= 0x0500
//获取user32.dll模块
m_hModUser32 = ::GetModuleHandle( _T("user32.dll") );
ATLASSERT( ( m_hModUser32 != NULL ) && ( _T("Get user32.dll Module Failed!")) );
//获取SetLayeredWindowAttributes函数地址
lpfnSetLayeredWindowAttributes = (LPFNSETLAYEREDWINDOWATTRIBUTES)::GetProcAddress( m_hModUser32, "SetLayeredWindowAttributes" );
ATLASSERT( ( lpfnSetLayeredWindowAttributes != NULL ) && ( _T("Get SetLayeredWindowAttributes Function Failed!")) );
#endif
}
/*
* 通过RGB值设置窗口透明
* hWnd: 要设置透明的窗口句柄
* clrTran: 透明色
*/
void SetWindowTransparentRGBStyle( HWND hWnd, COLORREF clrTran = RGB(255,0,255))
{
ATLASSERT( ::IsWindow( hWnd ) );
//设置窗口的扩展属性
LONG lStyle = ::GetWindowLong( hWnd, GWL_EXSTYLE );
::SetWindowLong( hWnd, GWL_EXSTYLE, lStyle|WS_EX_LAYERED );
#if _WIN32_WINNT >= 0x0500
lpfnSetLayeredWindowAttributes( hWnd, clrTran, 255, LWA_COLORKEY);
#endif
}
/*
* 通过Alpha值设置窗口透明
* hWnd: 要设置透明的窗口句柄
* bAlpha: Alpha透明值
*/
void SetWindowTransparentAlphaStyle( HWND hWnd, BYTE bAlpha = 255 )
{
ATLASSERT( ::IsWindow( hWnd ) );
//设置窗口的扩展属性
LONG lStyle = ::GetWindowLong( hWnd, GWL_EXSTYLE );
::SetWindowLong( hWnd, GWL_EXSTYLE, lStyle|WS_EX_LAYERED );
#if _WIN32_WINNT >= 0x0500
lpfnSetLayeredWindowAttributes( hWnd, RGB(0,0,0), bAlpha, LWA_ALPHA );
#endif
}
};
#endif
#define _WINDOW_TRANSPARENT_H_
#ifndef WS_EX_LAYERED
#define LWA_COLORKEY 0x00000001
#define LWA_ALPHA 0x00000002
#define WS_EX_LAYERED 0x00080000
#endif
//分层窗口只在WIndows 2000 及以后版本有效,故用 _WIN32_WINNT >= 0x0500做判断
class CWindowTransparent
{
typedef BOOL (WINAPI *LPFNSETLAYEREDWINDOWATTRIBUTES)(HWND hWnd, COLORREF crKey, BYTE bAlpha, DWORD dwFlags);
private:
HMODULE m_hModUser32;
LPFNSETLAYEREDWINDOWATTRIBUTES lpfnSetLayeredWindowAttributes;
public:
CWindowTransparent( )
{
#if _WIN32_WINNT >= 0x0500
//获取user32.dll模块
m_hModUser32 = ::GetModuleHandle( _T("user32.dll") );
ATLASSERT( ( m_hModUser32 != NULL ) && ( _T("Get user32.dll Module Failed!")) );
//获取SetLayeredWindowAttributes函数地址
lpfnSetLayeredWindowAttributes = (LPFNSETLAYEREDWINDOWATTRIBUTES)::GetProcAddress( m_hModUser32, "SetLayeredWindowAttributes" );
ATLASSERT( ( lpfnSetLayeredWindowAttributes != NULL ) && ( _T("Get SetLayeredWindowAttributes Function Failed!")) );
#endif
}
/*
* 通过RGB值设置窗口透明
* hWnd: 要设置透明的窗口句柄
* clrTran: 透明色
*/
void SetWindowTransparentRGBStyle( HWND hWnd, COLORREF clrTran = RGB(255,0,255))
{
ATLASSERT( ::IsWindow( hWnd ) );
//设置窗口的扩展属性
LONG lStyle = ::GetWindowLong( hWnd, GWL_EXSTYLE );
::SetWindowLong( hWnd, GWL_EXSTYLE, lStyle|WS_EX_LAYERED );
#if _WIN32_WINNT >= 0x0500
lpfnSetLayeredWindowAttributes( hWnd, clrTran, 255, LWA_COLORKEY);
#endif
}
/*
* 通过Alpha值设置窗口透明
* hWnd: 要设置透明的窗口句柄
* bAlpha: Alpha透明值
*/
void SetWindowTransparentAlphaStyle( HWND hWnd, BYTE bAlpha = 255 )
{
ATLASSERT( ::IsWindow( hWnd ) );
//设置窗口的扩展属性
LONG lStyle = ::GetWindowLong( hWnd, GWL_EXSTYLE );
::SetWindowLong( hWnd, GWL_EXSTYLE, lStyle|WS_EX_LAYERED );
#if _WIN32_WINNT >= 0x0500
lpfnSetLayeredWindowAttributes( hWnd, RGB(0,0,0), bAlpha, LWA_ALPHA );
#endif
}
};
#endif