CAlphaBlend::CAlphaBlend(const HWND hWnd, const int nOpactiy, const BOOL bTransparent)
: m_hWnd(hWnd), m_nOpacity(nOpactiy), m_bTransparent(bTransparent)
{
// make sure they have set it up properly
ASSERT(m_nOpacity >= 0 && m_nOpacity <= 255);
ASSERT(m_hWnd && ::IsWindow(m_hWnd));
if (m_bTransparent)
{
SetTransparent();
SetOpacity(nOpactiy);
}
}
void CAlphaBlend::SetOpacity(const int nOpacity)
{
// make sure they have set it up properly
ASSERT(m_nOpacity >= 0 && m_nOpacity <= 255);
ASSERT(m_hWnd && ::IsWindow(m_hWnd));
if (nOpacity >= 0 && nOpacity <= 255)
{
m_nOpacity = nOpacity;
if (m_bTransparent)
SetLayeredWindowAttributes(m_hWnd, 0, m_nOpacity, LWA_ALPHA);
}
}
void CAlphaBlend::SetTransparent(BOOL bTransparent)
{
// make sure they have set it up properly
ASSERT(m_nOpacity >= 0 && m_nOpacity <= 255);
ASSERT(m_hWnd && ::IsWindow(m_hWnd));
if (bTransparent)
{
// make it transparent
long l = GetWindowLong(m_hWnd, GWL_EXSTYLE) | WS_EX_LAYERED;
SetWindowLong(m_hWnd, GWL_EXSTYLE, l);
m_bTransparent = TRUE;
}
else
{
// make it no transparent
long l = GetWindowLong(m_hWnd, GWL_EXSTYLE) ^ WS_EX_LAYERED;
SetWindowLong(m_hWnd, GWL_EXSTYLE, l);
m_bTransparent = FALSE;
}
}
void CAlphaBlend::FadeIn()
{
// make sure they have set it up properly
ASSERT(m_nOpacity >= 0 && m_nOpacity <= 255);
ASSERT(m_hWnd && ::IsWindow(m_hWnd));
for (int i = 0; i <= 100; i++)
{
SetOpacity(i*255/100);
UpdateWindow(m_hWnd);
}
SetTransparent(FALSE);
UpdateWindow(m_hWnd);
}