Sunday, April 16, 2006

Closing the dialog smoothly(fade out)

http://www.codeguru.com/cpp/w-d/dislog/animation/article.php/c5063/

I "found" how to close a dialog box smoothly that is fade away.....

code snippets:

#include //for User32.dll
HMODULE hUserDll;
#define WS_EX_LAYERED 0x00080000
#define LWA_ALPHA 0x00000002

hUserDll = ::LoadLibrary(_T("USER32.dll"));
//Change style to layered window style...
::SetWindowLong(m_hWnd, GWL_EXSTYLE, ::GetWindowLong(m_hWnd, GWL_EXSTYLE) | WS_EX_LAYERED );
SetTransparent(m_hWnd, 0, 255 * 100/100 , LWA_ALPHA );

//The above will set the window to a layered style and the initial transparency value is set

void CMyDialog::SetTransparent(HWND hWnd, COLORREF crKey,
BYTE bAlpha, DWORD dwFlags)
{
BOOL bRet = TRUE;
typedef BOOL (WINAPI* lpfnSetTransparent)(HWND hWnd,
COLORREF crKey,
BYTE bAlpha,
DWORD dwFlags);

// Check that "USER32.dll" library has been
// loaded successfully...
if (hUserDll )
{
lpfnSetTransparent pFnSetTransparent = NULL;
pFnSetTransparent =
(lpfnSetTransparent)GetProcAddress(hUserDll,
"SetLayeredWindowAttributes");

if (pFnSetTransparent )
bRet = pFnSetTransparent(hWnd, crKey, bAlpha, dwFlags);
else
bRet = FALSE;
} // if( m_hUserDll )

return bRet;
} // End of SetTransparent function

//The above will set the transparency of the window(here dialog)
void CMyDialog::CloseSmoothly()
{
// Increase transparency one percent each time...
for(int nPercent=100; nPercent >= 0 ;nPercent--)
SetTransparent( m_hWnd, 0, 255 * nPercent/100, LWA_ALPHA);
if( hUserDll )
FreeLibrary(hUserDll);
}

0 Comments:

Post a Comment

<< Home