![]()
Code
bool SetTransparent(int alpha)
{
typedef DWORD (WINAPI *PSETLAYEREDWINDOWATTR)(HWND, DWORD, BYTE, DWORD);
PSETLAYEREDWINDOWATTR pSetLayeredWindowAttributes = NULL;
if ( pSetLayeredWindowAttributes == NULL )
{
HINSTANCE hInst = LoadLibrary("User32.DLL");
if(hInst)
{
//取得SetLayeredWindowAttributes函数指针
pSetLayeredWindowAttributes =(PSETLAYEREDWINDOWATTR )GetProcAddress(hInst, "SetLayeredWindowAttributes");
}
if ( pSetLayeredWindowAttributes == NULL )
return false;
LONG exstyle = GetWindowLong(this->GetSafeHwnd(), GWL_EXSTYLE);
// if setting alpha to fully opaque then turn off the layered style
if (alpha == 255)
{
SetWindowLong(GetHwnd(), GWL_EXSTYLE, exstyle & ~WS_EX_LAYERED);
Refresh();
return true;
}
// Otherwise, set the layered style if needed and set the alpha value
if ((exstyle & WS_EX_LAYERED) == 0 )
SetWindowLong(this->GetSafeHwnd(), GWL_EXSTYLE, exstyle | WS_EX_LAYERED);
return pSetLayeredWindowAttributes(this->GetSafeHwnd(), 0, alpha, LWA_ALPHA) != 0;
}