MFC dialog box fade-in and fade-out

Source: Internet
Author: User
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
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.