窗体透明效果的实现 渐进式等SetLayeredWindowAttributes的用法
2006-10-25 14:45
459 查看
BOOL SetLayeredWindowAttributes(
HWND hwnd, // handle to the layered window
COLORREF crKey, // specifies the color key
BYTE bAlpha, // value for the blend function
DWORD dwFlags // action
);
Windows NT/2000/XP: Included in Windows 2000 and later.
Windows 95/98/Me: Unsupported.
Header: Declared in Winuser.h; include Windows.h.
Library: Use User32.lib.
一些常量:
WS_EX_LAYERED = 0x80000;
LWA_ALPHA = 0x2;
LWA_COLORKEY=0x1
其中dwFlags有LWA_ALPHA和LWA_COLORKEY。LWA_ALPHA被设置的话,通过bAlpha决定透明度,LWA_COLORKEY被设置的话,则指定被透明掉的颜色为crKey,其他颜色则正常显示。
注:要使使窗体拥有透明效果,首先要有WS_EX_LAYERED扩展属性(旧sdk也没有的)。
例子代码:
在OnInitDialog()加入:
//加入WS_EX_LAYERED扩展属性
SetWindowLong(this->GetSafeHwnd(),GWL_EXSTYLE,
GetWindowLong(this->GetSafeHwnd(),GWL_EXSTYLE)^0x80000);
HINSTANCE hInst = LoadLibrary("User32.DLL");
if(hInst)
{
typedef BOOL (WINAPI *MYFUNC)(HWND,COLORREF,BYTE,DWORD);
MYFUNC fun = NULL;
//取得SetLayeredWindowAttributes函数指针
fun=(MYFUNC)GetProcAddress(hInst, "SetLayeredWindowAttributes");
if(fun)fun(this->GetSafeHwnd(),0,128,2);
FreeLibrary(hInst);
}
第二种例子代码
在.h中加入
#define LWA_COLORKEY 0x00000001
#define WS_EX_LAYERED 0x00080000
typedef BOOL (WINAPI *lpfnSetLayeredWindowAttributes)(HWND hWnd, COLORREF crKey, BYTE bAlpha, DWORD dwFlags);
lpfnSetLayeredWindowAttributes SetLayeredWindowAttributes;
在.cpp中
COLORREF maskColor=RGB(0X80,0XC0,0XFF);
HMODULE hUser32 = GetModuleHandle("user32.dll");
SetLayeredWindowAttributes = (lpfnSetLayeredWindowAttributes)GetProcAddress(hUser32,"SetLayeredWindowAttributes");
SetWindowLong(GetSafeHwnd(), GWL_EXSTYLE, GetWindowLong(GetSafeHwnd(), GWL_EXSTYLE)|WS_EX_LAYERED);
SetLayeredWindowAttributes(GetSafeHwnd(), maskColor, 255, LWA_COLORKEY);
FreeLibrary(hUser32);
HWND hwnd, // handle to the layered window
COLORREF crKey, // specifies the color key
BYTE bAlpha, // value for the blend function
DWORD dwFlags // action
);
Windows NT/2000/XP: Included in Windows 2000 and later.
Windows 95/98/Me: Unsupported.
Header: Declared in Winuser.h; include Windows.h.
Library: Use User32.lib.
一些常量:
WS_EX_LAYERED = 0x80000;
LWA_ALPHA = 0x2;
LWA_COLORKEY=0x1
其中dwFlags有LWA_ALPHA和LWA_COLORKEY。LWA_ALPHA被设置的话,通过bAlpha决定透明度,LWA_COLORKEY被设置的话,则指定被透明掉的颜色为crKey,其他颜色则正常显示。
注:要使使窗体拥有透明效果,首先要有WS_EX_LAYERED扩展属性(旧sdk也没有的)。
例子代码:
在OnInitDialog()加入:
//加入WS_EX_LAYERED扩展属性
SetWindowLong(this->GetSafeHwnd(),GWL_EXSTYLE,
GetWindowLong(this->GetSafeHwnd(),GWL_EXSTYLE)^0x80000);
HINSTANCE hInst = LoadLibrary("User32.DLL");
if(hInst)
{
typedef BOOL (WINAPI *MYFUNC)(HWND,COLORREF,BYTE,DWORD);
MYFUNC fun = NULL;
//取得SetLayeredWindowAttributes函数指针
fun=(MYFUNC)GetProcAddress(hInst, "SetLayeredWindowAttributes");
if(fun)fun(this->GetSafeHwnd(),0,128,2);
FreeLibrary(hInst);
}
第二种例子代码
在.h中加入
#define LWA_COLORKEY 0x00000001
#define WS_EX_LAYERED 0x00080000
typedef BOOL (WINAPI *lpfnSetLayeredWindowAttributes)(HWND hWnd, COLORREF crKey, BYTE bAlpha, DWORD dwFlags);
lpfnSetLayeredWindowAttributes SetLayeredWindowAttributes;
在.cpp中
COLORREF maskColor=RGB(0X80,0XC0,0XFF);
HMODULE hUser32 = GetModuleHandle("user32.dll");
SetLayeredWindowAttributes = (lpfnSetLayeredWindowAttributes)GetProcAddress(hUser32,"SetLayeredWindowAttributes");
SetWindowLong(GetSafeHwnd(), GWL_EXSTYLE, GetWindowLong(GetSafeHwnd(), GWL_EXSTYLE)|WS_EX_LAYERED);
SetLayeredWindowAttributes(GetSafeHwnd(), maskColor, 255, LWA_COLORKEY);
FreeLibrary(hUser32);
相关文章推荐
- VC中实现透明窗体--SetLayeredWindowAttributes
- VC++ 实现整个对话框透明的效果SetLayeredWindowAttributes
- VC6.0使用SetLayeredWindowAttributes实现透明效果
- 渐进式透明的设置(SetLayeredWindowAttributes的用法)
- (转)xp下实现窗体透明特效/异形窗口SetLayeredWindowAttributes函数
- WinAPI透明窗体SetLayeredWindowAttributes
- 用SetLayeredWindowAttributes实现窗口透明
- WinAPI透明窗体SetLayeredWindowAttributes( 转)
- 用SetLayeredWindowAttributes实现窗口透明
- 使用SetLayeredWindowAttributes API 实现透明窗口
- 透明窗口(窗口上面文字图片等内容不透明)的实现(使用SetLayeredWindowAttributes API函数)
- 工作问题积累(八)SetLayeredWindowAttributes(设置透明窗体)
- WinAPI: SetLayeredWindowAttributes - 设置窗口的透明
- Win7下的异形窗体——SetLayeredWindowAttributes()的应用注意
- 使用SetLayeredWindowAttributes函数设置窗口透明色与透明度
- SetLayeredWindowAttributes 函数的使用方法,对话框透明的方法
- VC++调用UpdateLayeredWindow实现半透明窗体【转】
- mfc的窗口透明度调整(SetLayeredWindowAttributes函数)
- DUI-分层窗口两种模式(SetLayeredWindowAttributes和UpdateLayeredWindow两种方法各有利弊)
- SetLayeredWindowAttributes未定义的解决方案