Closing A Window Smoothly | CodeGuru

Closing A Window Smoothly

Environment: VC6, win2000, winXP Enhance the user interface for your application by closing its windows in a smoothly way–make your window disappear slowly (fade out) while its closing. Technique The technique is very simple. While closing the window, just loop and increase transparency value to the window each loop until the window disappear completely. The […]

Written By
CodeGuru Staff
CodeGuru Staff
May 8, 2002
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

Environment: VC6, win2000, winXP

Enhance the user interface for your application by closing its windows in a smoothly way–make your window disappear slowly (fade out) while its closing.

Technique

The technique is very simple. While closing the window, just loop and increase transparency value to the window each loop until the window disappear completely. The API function that responsible to make the window transparent is:

SetLayeredWindowAttributes(HWND, COLORREF, BYTE,DWORD)

The third parameter is responsible for determine the degree of transparency, by increase this value each time the window will disappear slowly. The SetLayeredWindowAttributes(…) function is found in USER32.DLL, so you need to load this dll before you can use it. Here is the code:

First Step: you must change the window style to a layered style By calling SetWindowLong(…) API function. Use this step in the OnInitDialog() function in a dialog based application or in the OnCreate() function in a
Document/View application:

SetWindowLong(m_hWnd,
                   GWL_EXTYLE,
                   ::GetWindowLong(m_hWnd, GWL_EXSTYLE) | WS_EX_LAYERED);

Then Call:

SetTransparent( m_hWnd, 0, 255 , LWA_ALPHA );

with a bAlpha value equal 255. The implementation of the function look like this:
 

// This function sets the transparency layered window
// by calling SetLayeredWindowAttributes API function.
BOOL 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 ( m_hUserDll )
  {
    lpfnSetTransparent pFnSetTransparent = NULL;
    pFnSetTransparent =
      (lpfnSetTransparent)GetProcAddress(m_hUserDll,
                    "SetLayeredWindowAttributes");
    if (pFnSetTransparent )
       bRet = pFnSetTransparent(hWnd, crKey, bAlpha, dwFlags);
    else
       bRet = FALSE;
  } // if( m_hUserDll )
   return bRet;
} // End of SetTransparent function
Second Step: call the CloseSmoothly() function in the OnClose() function. The CloseSmoothly() function will look like this:
 void CloseSmoothly()
{
  // Increase transparency one percent each time…
  for(int nPercent=100; nPercent >= 0 ;nPercent–)
     SetTransparent( m_hWnd, 0, 255 * nPercent/100, LWA_ALPHA);
}

Downloads
Download demo project4 Kb
Download source13 Kb
CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.