天天看点

使用SetLayeredWindowAttributes API 实现透明窗口

//bAlpha form 0 to 255

//When bAlpha is 0, the window is completely transparent.

//When bAlpha is 255, the window is opaque.

BOOL SetWindowAlpha(HWND hwnd, BYTE bAlpha)

{

BOOL bRet = 0;

HMODULE hModule = GetModuleHandle(_T("User32.dll"));

if(hModule)

{

#ifndef WS_EX_LAYERED

#define WS_EX_LAYERED 0x00080000UL

#endif //WS_EX_LAYERED

#ifndef LWA_ALPHA

#define LWA_ALPHA 2

#endif

typedef BOOL (WINAPI *pSETLAYEREDWINDOWATTRIBUTES)(

HWND, COLORREF, BYTE, DWORD);

pSETLAYEREDWINDOWATTRIBUTES pSetLayeredWindowAttributes =

(pSETLAYEREDWINDOWATTRIBUTES)::GetProcAddress(hModule,

"SetLayeredWindowAttributes");

if(pSetLayeredWindowAttributes)

{

// Set WS_EX_LAYERED on this window

SetWindowLong(hwnd, GWL_EXSTYLE,

GetWindowLong(hwnd, GWL_EXSTYLE) | WS_EX_LAYERED);

// Make this window bAlpha

pSetLayeredWindowAttributes(hwnd, 0, bAlpha, LWA_ALPHA);

return bRet;

}

}

return bRet;

}

继续阅读