example 1 :
set up with function AnimateWindow() .
::this method can't set up one CDialog which is transparent
1. In the header file stdafx. H, add:
# UNDEF winver // undefines the original version and redefines the version
# Define winver 0x5000 // to make the animatewindow function available
2. Implementation functions:
Animatewindow (getsafehwnd (), 1000, aw_blend); // fade into the form for 1 second
Animatewindow (getsafehwnd (), 1000, aw_blend | aw_hide); // fades out the form for 1 second
/*
example 2 :
set up with function ::SetLayeredWindowAttributes() */
//SmoothDlgDlg.h
HMODULEm_hUserDll;
//SmoothDlgDlg.CPP
// This function sets the transparency of the form by calling the setlayeredwindowattributes API Function
BOOL CSmoothDlgDlg::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 whether the USER32 dynamic Connection Library has been loaded
if(m_hUserDll)
{
lpfnSetTransparentpFnSetTransparent = NULL;
pFnSetTransparent=(lpfnSetTransparent)GetProcAddress(m_hUserDll,"SetLayeredWindowAttributes");
if(pFnSetTransparent)
bRet = pFnSetTransparent(hWnd, crKey, bAlpha, dwFlags);
else
bRet = FALSE;
}
returnbRet;
}
voidCSmoothDlgDlg::OnClose()
{
// Cyclically increase the transparency of the window
for(int nPercent = 100; nPercent >= 0 ; nPercent --)
SetTransparent(m_hWnd, 0, 255 * nPercent / 100, LWA_ALPHA);
// Release USER32 dynamic Connection Library
if(m_hUserDll)
::FreeLibrary(m_hUserDll);
CDialog::OnClose();}
Add the following code to the window initialization function:
BOOLCSmoothDlgDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Load the USER32 dynamic Connection Library
m_hUserDll=::LoadLibrary(_T("USER32.dll"));
// Set the window style to ws_ex_layered (stacked window style)
::SetWindowLong(m_hWnd, GWL_EXSTYLE, ::GetWindowLong(m_hWnd,GWL_EXSTYLE) | WS_EX_LAYERED);
SetTransparent(m_hWnd, 0, 255 * 100 / 100, LWA_ALPHA);
// Some automatically generated codes are omitted here
return TRUE;//return TRUE unless you set the focus to acontrol
}