一:
typedef BOOL (WINAPI *lpfnSetLayeredWindowAttributes)(HWND hWnd, COLORREF crKey, BYTE bAlpha, DWORD dwFlags);
lpfnSetLayeredWindowAttributes SetLayeredWindowAttributes; //设置成边缘透明
COLORREF maskColor = RGB(240, 240, 240);
HMODULE hUser32 = GetModuleHandle(_T("user32.dll")); //加载动态链接库
SetLayeredWindowAttributes = (lpfnSetLayeredWindowAttributes)GetProcAddress(hUser32, "SetLayeredWindowAttributes"); //取得SetLayeredWindowAttributes函数指针 //为窗口加入WS_EX_LAYERED扩展属性
SetWindowLong(this->GetSafeHwnd(), GWL_EXSTYLE, GetWindowLong(GetSafeHwnd(), GWL_EXSTYLE)^WS_EX_LAYERED); //调用SetLayeredWinowAttributes函数
/*SetLayeredWindowAttributes(this->GetSafeHwnd(), maskColor, 192, LWA_COLORKEY); */
SetLayeredWindowAttributes(this->GetSafeHwnd(), maskColor, 255, LWA_ALPHA | LWA_COLORKEY);
FreeLibrary(hUser32); //释放动态链
//LWA_ALPHA时:crKey参数无效,bAlpha参数有效;
//LWA_COLORKEY:窗体中的所有颜色为crKey的地方将变为透明,bAlpha参数无效。
//LWA_ALPHA | LWA_COLORKEY:crKey的地方将变为全透明,而其它地方根据bAlpha参数确定透明度。
二:
COLORREF m_bgcolor = RGB(0, 0, 0);
BOOL m_bTransparent = ToggleTransparent(m_bgcolor, 100, LWA_ALPHA);
BOOL ToggleTransparent(COLORREF crKey, BYTE bAlpha, DWORD dwFlags)
{
BOOL bResult;
LONG dwExStyle;
dwExStyle = GetWindowLong(this->GetSafeHwnd(), GWL_EXSTYLE);
if (dwExStyle & WS_EX_LAYERED)
{
dwExStyle &= ~(WS_EX_LAYERED);
bResult = FALSE;
}
else
{
dwExStyle |= (WS_EX_LAYERED);
bResult = TRUE;
}
SetWindowLong(this->GetSafeHwnd(), GWL_EXSTYLE, dwExStyle);
SetLayeredWindowAttributes( crKey, bAlpha, dwFlags);
return bResult;
}