Click here to Skip to main content
Click here to Skip to main content

FaderWnd - a Window Fader for Windows 2000

, 28 Jun 2000
Rate this:
Please Sign up or sign in to vote.
An MFC class to fade any window with only one line of code.
<!-- Link to source file download -->
  • Download source files - 4 Kb
  • <!-- Link to demo file download -->
  • Download demo project - 28 Kb
  • <!-- Article image -->

    Sample Image - FaderWnd.jpg

    <!-- Add the rest of your HTML here -->


    One of the attractive new features of Windows 2000 is the menu and other selections that fade away instead of just disappearing. To see this in operation you need to select "Use transition effects for menus and tooltips" on the "Effects" tab of the Display Properties dialog and then select "Fade effect" in the combo box.

    In this article I present a simple MFC class that allows you to apply a similar fading effect to any window you choose - with only one line of code! Don't just close a window - have it fade away! The effect is most appropriate to small windows; I use it most often for splash windows, about boxes and other small dialogs.

    To use the code you just add the FaderWnd source and header files to your MFC project, include the header in the relevant source file and construct a CFaderWnd when you want your window to fade. The CFaderWnd object itself takes care of fading your window and then destroying and deleting itself.

    The only code you need to write is new CFaderWnd(this). Once the CFaderWnd is constructed you can destroy or hide your window, or call EndDialog if the window is a modal dialog.

    For example, a modeless dialog (or it could be any child window):
    void CFadeTestDlg::OnClose() 
    	new CFaderWnd(this);
    or a modal dialog:
    void CFadeTestDlg::OnOK() 
    	if (UpdateData)
    		new CFaderWnd(this);
    void CFadeTestDlg::OnCancel() 
    	new CFaderWnd(this);

    How it works

    The CFaderWnd uses the new layered windows feature and the UpdateLayeredWindow API function. The constructor creates a new topmost window with the WS_EX_LAYERED style (among others) and with the same size and position as the source window (the window to be faded). It then uses UpdateLayeredWindow to fill the new window with a bitmap that is an exact copy of the source window. It also sets up a timer. When the constructor returns the source window can be destroyed (or hidden) and the new window remains visible.

    On each timer message CFaderWnd calls UpdateLayeredWindow again with a smaller value of alpha. Reducing the alpha value makes the window increasingly translucent. When alpha is at or very near zero CFaderWnd destroys the window and PostNcDestroy deletes the CFaderWnd object.

    The CFaderWnd constructor takes three parameters; it looks like this:

    CFaderWnd(CWnd *pWndToFade, UINT nFadeTime = 2000, BYTE byAlpha = 255);

    The second and third parameters are optional; the examples above all use the default values for these parameters. For obvious reasons (and if they're not obvious you shouldn't be reading this) the first parameter, which specifies the window to fade, must be supplied.

    CWnd *pWndToFade This is a pointer to the source window (the window to be faded).
    UINT nFadeTime This is a number of milliseconds for the whole duration of the fade. The default value gives a 2 second fade.
    BYTE byAlpha This is the alpha value at which to start the fade. The default value of 255 represents starting with a fully opaque window. You might choose to start at 127, for example, so that the window immediately becomes semi-transparent and then fades from there.

    UpdateLayeredWindow is only available on Windows 2000, so if the code were to call that function directly any program using the CFaderWnd class would fail to run on any other platform. I usually want my programs to run on any Win32 platform but to use the new features when they're available. Therefore the code does not call UpdateLayeredWindow directly. Instead it uses GetProcAddress to find out whether the function is available and calls it through the returned pointer when it is. If the function is not available CFaderWnd quietly deletes itself so it behaves as a no-op on older platforms. Thus you can use CFaderWnd in all your programs without compromising their ability to run on all Win32 platforms.


    This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

    A list of licenses authors might use can be found here


    About the Author

    P J Pearson

    United Kingdom United Kingdom
    No Biography provided

    Comments and Discussions

    GeneralFader on Vista... PinmemberPaul Runner22-Dec-08 14:02 
    GeneralRe: Fader on Vista... PinmemberAndrew Kulikov9-Feb-09 11:26 
    GeneralFading out a Splash Screen. PinmemberNick Thomas21-Jul-06 2:45 
    GeneralRe: Fading out a Splash Screen. PinmemberPhil J Pearson21-Jul-06 10:01 
    QuestionUpdateLayeredWindow on extended desktops? Pinmemberlamagaaya8-Mar-06 7:35 
    AnswerRe: UpdateLayeredWindow on extended desktops? Pinmemberb@andreasen8.org25-Sep-06 9:32 
    GeneralRe: UpdateLayeredWindow on extended desktops? Pinmemberlamagaaya26-Sep-06 17:11 
    GeneralInvalid access to memory location. Pinmemberiampams1-Nov-05 20:12 
    AnswerRe: Invalid access to memory location. PinmemberPhil J Pearson2-Nov-05 9:59 
    GeneralLayered Windows API PinmemberZero DeHero14-May-03 1:08 
    QuestionDivide by zero? Pinmembermsvcpp17-Mar-03 2:21 
    GeneralSmall memory leak PinmemberCorey A16-Jan-03 5:36 
    Generallayerd window on 9x Pinmemberkuzi6-Mar-02 6:46 
    GeneralRe: layerd window on 9x PinmemberPhil J Pearson6-Mar-02 11:04 
    QuestionHelp with Alpha Blending? Pinmemberswinefeaster30-Jan-02 11:41 
    Hi there,

    I was hoping perhaps you could shed some light on this little problem of mine... Sorry to bother, but you seem to know a bit about alpha blending.

    I'm trying to use the ::AlphaBlt function to perform a simple fade effect on the screen. Basically I need to create a transition between the current bitmap on a dc, and a new bitmap which is to "fade in".

    I have hacked up the following code, and it works somewhat. The problem is that it is not smooth. Also, it takes way toooo much cpu to do, and this causes inconsistencies in timing. Usually in runs in about a few seconds at 1024x768, but on some machines at 1280x1024, it takes 10 seconds or worse!

    Here's the code. Hopefully it won't be too hard to read through the irrelevant stuff...

    cPkpPhotoImage::BlitStretchedFromProprietaryBlitEffectFade(CDC& DestDc,
    const cRect& PassedSourceRect, const cRect& DestRect, uint32 rop,
    const sEffectInfo& EffectInfo)const
    LBitmap* Bitmap = GetBlitBitmap();

    // Should not get here.
    cSize ThisSize = GetCurrentImageSize();
    cRect SourceRect(ThisSize);

    BlendInfo.BlendOp = AC_SRC_OVER;
    BlendInfo.BlendFlags = 0;
    BlendInfo.AlphaFormat = 0;
    BlendInfo.SourceConstantAlpha = (BYTE)grain;

    CDC ThisDc;
    if(LockDcInternal(ThisDc, Bitmap))
    cTimer Timer;
    cTimeTag WhenCanProgress;

    cBitmap NoFlickerBitmap(DestDc, DestRect.GetSize());
    cDC NoFlickerBitmapDc(&NoFlickerBitmap, &DestDc);

    // First copy what's in the dest dc onto our temporary bitmap so we
    // can do the fade.
    NoFlickerBitmapDc.BitBlt(0, 0, DestRect.GetWidth(),
    DestRect.GetHeight(), &DestDc, DestRect.left,, rop);

    int numIterations = ((256 / grain) * 2) + 1;
    for(int i = 0; i < numIterations; i++)
    // Do the alpha blend onto the temporary bitmap.
    ::AlphaBlend(NoFlickerBitmapDc.m_hDC, 0, 0,
    DestRect.GetWidth(), DestRect.GetHeight(), ThisDc.m_hDC,
    SourceRect.left,, SourceRect.GetWidth(),
    SourceRect.GetHeight(), BlendInfo);

    // Now we wait a bit if we're too quick. Note that we do as much
    // processing before waiting, to ensure delay accuracy.
    if(Timer > WhenCanProgress)
    // Can progress.
    int sleepTime = -(Timer - WhenCanProgress);

    if(sleepTime > 0)
    ::Sleep(sleepTime - 2);

    // Then blit that bitmap onto the dest dc.
    DestDc.BitBlt(DestRect.left,, DestRect.GetWidth(),
    DestRect.GetHeight(), &NoFlickerBitmapDc, 0, 0, rop);

    WhenCanProgress = cTimeTag(Timer, delay);

    // Now do the final, non-transparent blit.
    DestDc.StretchBlt(0, 0, DestRect.GetWidth(), DestRect.GetHeight(),
    &ThisDc, SourceRect.left,, SourceRect.GetWidth(),
    SourceRect.GetHeight(), rop);

    UnlockDcInternal(ThisDc, Bitmap);

    Note that I use NoFlickerBitmap (cBitmap is just an extended version of CBitmap), into which I create the composite semi faded image. This image I then opaquely blit onto the destination dc. I have to do this because ::AlphaBlend() doesn't do a nice clean blit; rather it does some kind of iterative scan, which can be seen and looks quite ugly.

    One way I could do this is create a window over the region i was to fade, and just use the dialog fading code article. Unfortunately, this seems kind of like a hack, and I'm reluctant to do it. There must be a better way!



    Check out Aephid Photokeeper, the powerful digital
    photo album solution at

    GeneralA question of "FaderWnd"Class from China. PinmemberSailing11-Oct-01 4:43 
    GeneralDoes'nt work with maximized windows PinmemberAnonymous6-Sep-01 3:33 
    GeneralGreat + Small Suggestion PinmemberDanG21-Aug-01 0:13 
    Questionhow can i use on winnt?? are you know it? Pinsusswhat?26-Jul-00 22:07 
    AnswerRe: how can i use on winnt?? are you know it? PinsussChangYoung Jung16-Sep-00 19:28 
    GeneralExplain of code in lame PinsussAdun13-Jul-00 11:35 
    GeneralRe: Explain of code in lame PinmemberRomout8-Sep-01 3:10 
    GeneralRe: Explain of code in lame PinmemberRomout8-Sep-01 3:10 
    GeneralRe: Explain of code in lame PinmemberPhil J Pearson10-Sep-01 11:52 
    GeneralComments PinsussRui Lopes24-Jun-00 1:51 
    GeneralRe: Comments PinsussPhil24-Jun-00 10:09 
    GeneralRe: Comments PinsussMike Dunn29-Jun-00 1:22 
    GeneralGood! PinsussJoe23-Jun-00 11:00 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

    | Advertise | Privacy | Terms of Use | Mobile
    Web02 | 2.8.150331.1 | Last Updated 29 Jun 2000
    Article Copyright 2000 by P J Pearson
    Everything else Copyright © CodeProject, 1999-2015
    Layout: fixed | fluid