我给窗口设定WS_EX_LAYERED,然后用SetLayeredWindowAttributes来设定它的AlphaBlend属性,发现:对于顶层窗口(Overlapped),这个方法就可行;对于自窗口(Child),这个方法就不行。我的问题是:是不是Win2000以后增加的分层窗口特性只能应用于顶层窗口?我若是想将某一个控件窗口设定为半透明就不行?我查了MSDN,还有好多书,没有看到关于这个问题的解释。谢谢。另外,有没有办法为自窗口设定半透明呢?
调试欢乐多
#define MWT_MIN_FACTOR (0)
#define MWT_MAX_FACTOR (0xFF)bool MakeWindowTransparent(HWND hWnd, BYTE factor);
bool MakeWindowTransparent(CWnd *w, BYTE factor);CPP文件:typedef DWORD (WINAPI *PSLWA)(HWND, DWORD, BYTE, DWORD);static PSLWA pSetLayeredWindowAttributes = NULL;
static BOOL initialized = FALSE;bool MakeWindowTransparent(HWND hWnd, unsigned char factor)
{
/* First, see if we can get the API call we need. If we've tried
* once, we don't need to try again. */
if (!initialized)
{
HMODULE hDLL = LoadLibrary ("user32"); pSetLayeredWindowAttributes =
(PSLWA) GetProcAddress(hDLL, "SetLayeredWindowAttributes"); initialized = TRUE;
} if (pSetLayeredWindowAttributes == NULL)
return FALSE; /* Windows need to be layered to be made transparent. This is done
* by modifying the extended style bits to contain WS_EX_LAYARED. */
SetLastError(0); SetWindowLong(hWnd,
GWL_EXSTYLE ,
GetWindowLong(hWnd, GWL_EXSTYLE) | WS_EX_LAYERED); if (GetLastError())
return FALSE; /* Now, we need to set the 'layered window attributes'. This
* is where the alpha values get set. */
return pSetLayeredWindowAttributes (hWnd,
RGB(0,0,255),
factor,
LWA_COLORKEY|LWA_ALPHA);
}bool MakeWindowTransparent(CWnd *w, unsigned char factor)
{
HWND wnd = w->GetSafeHwnd(); ASSERT(wnd); return MakeWindowTransparent(wnd, factor);
}
//BYTE factor就是透明度~~~ ////////////////////////以下实现窗体的透明///////////////////////////////////
if (!MakeWindowTransparent(this, MWT_MAX_FACTOR/2))
{
MessageBox("Transparency is not supported on this platform");
}
//////////////////////////////////////////////////////////////////////////////////