Chinaunix首页 | 论坛 | 博客
  • 博客访问: 414159
  • 博文数量: 99
  • 博客积分: 4230
  • 博客等级: 上校
  • 技术积分: 1026
  • 用 户 组: 普通用户
  • 注册时间: 2005-06-21 14:52
文章分类

全部博文(99)

文章存档

2011年(1)

2010年(1)

2008年(13)

2007年(28)

2006年(45)

2005年(11)

我的朋友

分类: C/C++

2006-08-07 09:31:46

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);
 

 
阅读(8611) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~