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 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 project - 4 Kb
Download source - 13 Kb


Comments

  • Setting to transparent on exit only

    Posted by Gavin Thornton on 03/17/2005 06:49am

    Setting your window to transparent slows down your windows drawing speed. You can just set it to transparent when you exit like below:
    
    void MyDlg::OnClose() 
    {
    	m_hUserDll = ::LoadLibrary(_T("USER32.dll"));
    	::SetWindowLong(m_hWnd, GWL_EXSTYLE, ::GetWindowLong(m_hWnd, GWL_EXSTYLE) |  WS_EX_LAYERED );
    	SetTransparent(m_hWnd, 0, 255 * 100/100 , LWA_ALPHA );
    	UpdateWindow();  
    	CloseSmoothly();
    
    	CDialog::OnClose();
    }

    • chat dialog box

      Posted by rama711 on 05/21/2005 07:47am

      How to extract a stored dialog box from EXE file. I have this EXE file it is for a game. When you run the game and when you hit [ENTER] a dialog box pop ups (Chat box) to enter some chat. I would like to know where I can find this dialog box so I can edit its shape or make it wider?I appreciate your respond.Note I used PE explorer but did not extract dialog boxes only icon stuff and some scripts

      Reply
    Reply
  • GWL_EXTYLE undeclared identifier

    Posted by Legacy on 10/14/2003 12:00am

    Originally posted by: Courtz

    Where does this get declared

    Reply
  • GWL_EXTYLE undeclared identifier

    Posted by Legacy on 10/14/2003 12:00am

    Originally posted by: Courtz

    Where does this get declared

    • Its a typo

      Posted by Pabs on 03/21/2006 05:36am

      Its a typo should be GWL_EXSTYLE :)

      Reply
    Reply
  • abo salah???:); nice idea

    Posted by Legacy on 08/15/2003 12:00am

    Originally posted by: saeed AL-hamed

    Abo-Salah????? lol... i think u know ahmad abo salah .. nice idea anyway...

    Reply
  • how can i make transparent bitmap

    Posted by Legacy on 04/23/2003 12:00am

    Originally posted by: Ravi Sharma

    I want to create a bitmap of + sighn and want to bitblt on the screen at mouse cursor position.till now everything is ok now i want to make this "+" sighn transparent.. transperency level will be from 0 to 100

    Reply
  • found it only can work to a dialog window.

    Posted by Legacy on 09/26/2002 12:00am

    Originally posted by: Gempin

    i found it only can work to a dialog window, but a control window, it do nothing, and you?

    Reply
  • Really nice indeed..

    Posted by Legacy on 07/25/2002 12:00am

    Originally posted by: alex

    Really nice.. I still have a little flickering flash when I pop up any window using this effect, but I guess this is usual as NVidia Desktop manager also have it when enabling transparent windows.

    Keep up the good work :) and thank you.

    Alexis.

    Reply
  • A way to make OpenGL window also transparent

    Posted by Legacy on 06/10/2002 12:00am

    Originally posted by: yfei

    Just set window attribute don't work for hardware accelerated window like OpenGL and DirectX

    Upto now, I got some good progress on OpenGL. It's working, but still need some improvement.

    What I did:
    1) Don't use set...attribute(), and don't response to WM_PAINT. Instead, use UpdateLayeredWindow() to paint the window.
    2) Don't (Don't need to) call SwapBuffer()
    3) Let OpenGL only paint to the GL_BACK background buffer. after GL painted the scene, call glReadPixels(...) to grab OpenGL's output to array 'data'. use 'data' to generate a 'bitmap'.
    4) Call UpdateLayeredWindow() to draw the window, use 'bitmap's DC as input.

    Then, done! everything smooth and perfect, except:

    problems:
    1) Due to bits format difference between OpenGL and Windows Bitmap, the bitmap is 'upside-down'.
    2) And color also not right if use 'GL_RGBA' when calling glReadPixels. if use 'GL_BGRA_EXT' then color will be right.
    but 'upside-down' problem I don't know how to solve.


    Reply
  • good

    Posted by Legacy on 05/13/2002 12:00am

    Originally posted by: hu

    very good!
    

    Reply
  • good

    Posted by Legacy on 05/13/2002 12:00am

    Originally posted by: hu

    very good!
    

    Reply
  • Loading, Please Wait ...

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • On-demand Event Event Date: October 23, 2014 Despite the current "virtualize everything" mentality, there are advantages to utilizing physical hardware for certain tasks. This is especially true for backups. In many cases, it is clearly in an organization's best interest to make use of physical, purpose-built backup appliances rather than relying on virtual backup software (VBA - Virtual Backup Appliances). Join us for this webcast to learn why physical appliances are preferable to virtual backup appliances, …

  • IBM Worklight is a mobile application development platform that lets you extend your business to mobile devices. It is designed to provide an open, comprehensive platform to build, run and manage HTML5, hybrid and native mobile apps.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds