现在的位置: 首页 > 综合 > 正文

让窗口透明起来

2013年09月22日 ⁄ 综合 ⁄ 共 1703字 ⁄ 字号 评论关闭
SetLayeredWindowAttributes是windows的新api,win2000以上才支持,它能使使窗体拥有透明效果.
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

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
);
一些常量:  
  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也没有的)。  

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

#define WS_EX_LAYERED           0x00080000
#define LWA_COLORKEY            0x00000001
#define LWA_ALPHA               0x00000002

typedef BOOL (*_SetLayeredWindowAttributes)(HWND,COLORREF,BYTE,DWORD);
_SetLayeredWindowAttributes SetLayeredWindowAttributes;

void SetLayered(HWND hWnd,int Pos)
{
SetLayeredWindowAttributes = (_SetLayeredWindowAttributes)GetProcAddress(GetModuleHandle(///"user32.dll///"),///"SetLayeredWindowAttributes///");
if ( SetLayeredWindowAttributes == NULL )
  return ;
else{
   __try{
    SetWindowLong(hWnd,GWL_EXSTYLE,GetWindowLong(hWnd,GWL_EXSTYLE)|WS_EX_LAYERED);
    SetLayeredWindowAttributes(hWnd,RGB(0,0,0),Pos,LWA_ALPHA);
   }
   __except(EXCEPTION_EXECUTE_HANDLER){
    AfxMessageBox(///"SYSTEM FALSE///");
   }
}
}

 

抱歉!评论已关闭.