Click here to Skip to main content
11,480,626 members (63,236 online)
Click here to Skip to main content

Go to sleep! Fade to B/W

, 3 Feb 2014 CPOL 159.5K 4.1K 130
Rate this:
Please Sign up or sign in to vote.
Fading a window from color to black and white like Windows/XP

Introduction

Did you ever notice that when you shutdown Windows/XP there is a fading effect from color to B/W on the desktop? Ok that's the objective of this article, a simple way to capture a given window and fade it in an inactive status.

The main idea to perform this operation is quite simple: the code basically creates a fading-window over the target one. This fading-window has all the needing to make this simple animation and the complete implementation is not long and it's based on 3 parts:

  • a snap-shot routine to copy the given window in an in-memory bitmap,
  • a function to decolorize the given bitmap,
  • the main loop to step the decolorizing.

Notice: this code will work only on 16, 24 or 32 bit display color mode.

Using the code

There are basically 2 modalities to perform the fading process: automatic and controlled.

Automatic - this is the simplest way, and it can be used to put the given window in a "relaxed" mode - when the user will click on the given window, this will return to it's original status - here the simple steps to start with this operation:

  1. add the FadeWnd.h and FadeWnd.cpp, to your project,
  2. include the FadeWnd.h in the implementation code (.cpp) where you will call the fading effect,
  3. simply call the CFadeWnd::AutoFade(CWnd* pWnd) on the window you want to fade.

Controlled - don't worry, this is quite easy too, the difference is that we want to control the moment the fading effect must finish. For this, I have assumed that the fading operation must simply be done before a modal window call - here the steps:

  1. as before, add the FadeWnd.h and FadeWnd.cpp, to your project,
  2. include the FadeWnd.h in the implementation code (.cpp) where you will call the fading effect,
  3. define a CFadeWnd object before a dialog or a message-box modal call, for sample: wndFade,
  4. create the fade window, passing as parent the window to fade as shown: wndFade.Create(this).

Difficult?

Let's code!

Remember, first at all, add the FadeWnd.h and FadeWnd.cpp, to your project. To do this, copy these 2 files in the folder of your project, and from the [Project] menu item of the Visual Studio, choose [Add to Project] -> [Files] voice. Select the 2.

Next step, include the FadeWnd.h in the implementation code (.cpp) where you will call the fading effect. If you want to use the CFadeWnd class a bit every where, you can include it in the stdafx.h.

#include "FadeWnd.h"

To use it in a simple way, you can perform the call:

void CMakeInactiveDlg::OnButton1() 
{
    CFadeWnd::AutoFade(this);
}

Fade all the desktop:

void CMakeInactiveDlg::OnButton2() 
{
    CFadeWnd::AutoFade(GetDesktopWindow());
}

Fade the given window for the necessary modal sub-window call:

void CMakeInactiveDlg::OnButton3() 
{
    CFadeWnd wndFade(this);
 
    // modal window call as a DIALOG or a MESSAGEBOX
    AfxMessageBox(
        _T("Disabled until this message will be closed"),
        MB_OK | MB_ICONINFORMATION);
}

Fade using a different gradient color:

void CMakeInactiveDlg::OnButton4() 
{
    CFadeWnd::AutoFade(
        this,                // window to fade
        RGB(255,128, 64),    // light color ref
        RGB( 64, 16,  0));   // dark color ref
}
 
// - or -
 
void CMakeInactiveDlg::OnButton5() 
{
    CFadeWnd wndFade(
        this,                // window to fade
        RGB(255,128, 64),    // light color ref
        RGB( 64, 16,  0));   // dark color ref
 
    // modal window call as a DIALOG or a MESSAGEBOX
    AfxMessageBox(
        _T("Disabled until this message will be closed"),
        MB_OK | MB_ICONINFORMATION);
}

Notices

To permit other programmers to change the behavior of this control, I have added 3 virtual members that can be easily overridden:

  • void CreateGradient(...) - this allow to try other fading algorithms
  • bool OnLButtonCheck() - this is needed to make security checks directly inside the CFadeWnd during left button mouse clicks
  • bool OnRButtonCheck() - same as above, but for right button mouse clicks

Play with the CreateGradient call

I have made virtual the CreateGradien member of the CFadeWnd. This will allow some programmers to play with this call deriving a new class from the CFadeWnd. Let's see a sample of this process.

Create your derived class, for sample:

class CMyGradientFadeWnd : public CFadeWnd
{
public:
    virtual void CreateGradient(
        COLORREF /* clrLight = RGB(255,255,255) */,  // parameters are not 
                                                     // used in this sample
        COLORREF /* clrDark  = RGB(  0,  0,  0) */)
    {
        for(int i=0; i<256; i++)
        {
            m_clrGradient[i] = RGB(i, 255-i, i);
        }
    }
};

Now you have only to use your CMyGradiendFadeWnd in substitution of the CFadeWnd in your application.

Intercepting the left and right mouse button clicks inside the CFadeWnd class using OnLButtonCheck and OnRButtonCheck calls

Of course, this could be done deriving the whole class with CLASSWIZARD, and for more skilled developers, I'll suggest to do it that way - but to allow fast and easy way to inject our own made dialogs directly in the CFadeWnd, I have made the 2 overridden calls.

As for the CreateGradiend saw before, create your derived class, for sample:

class CMyPasswordFadeWnd : public CFadeWnd
{
public:
    virtual bool OnLButtonCheck()
    {
        // add the password check here
        CPasswordDlg dlg; // make your own dialog
        return (dlg.DoModal() == IDOK);
    }
    virtual bool OnRButtonCheck() { return false; } // no response
};

Credits - Special thanks to

Credits are reported inside the source code. The major help come from these articles/posts:

History

  • 10 November 2003 - bug fixes and some new things from the message posts:
    • virtual CreateGradient(...) call to perform a different graying effect
    • virtual OnLButtonChek() and OnRButtonChek() to perform a mouse click security check inside the CFadeWnd class
    • timing calculation to make the fading effect fit in less than 2 seconds
    • region heredity for shaped windows (example skinned windows)
    • disabled the parent window during the fade process, and eliminated the icon on the task-bar 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

John A. Johnson
Instructor / Trainer
Italy Italy
Has gained his experience working with several private companies, both production and services kind of farms, especially in managerial, as a trainer and marketing/sales as considerable experience in project management.

Has completed his technical/scientific expertise with several companies and universities working on software engineering, industrial automation and production at various levels, producing CAD for textiles (in the press and yarn, both for design applications and treatment of color and digital printing), incorporation of the farm, factory automation (textiles, plastics, luxury mechanics and dyes) to complete the design of ERP systems (accounting, analytical, warehouse, quality systems, quality control, planning, ...).

Comments and Discussions

 
QuestionCreate Window Error 0x00000579 Pin
Erhy4-Jan-15 12:15
memberErhy4-Jan-15 12:15 
AnswerRe: Create Window Error 0x00000579 Pin
John A. Johnson2-Feb-15 9:20
memberJohn A. Johnson2-Feb-15 9:20 
Bugsome problems on multi display environment [modified] Pin
babosamdol16-Sep-14 16:45
memberbabosamdol16-Sep-14 16:45 
QuestionUpdated for VB.Net or C#? Pin
Tim8w19-Jun-08 13:47
memberTim8w19-Jun-08 13:47 
AnswerRe: Updated for VB.Net or C#? Pin
John A. Johnson23-Jun-08 22:22
memberJohn A. Johnson23-Jun-08 22:22 
AnswerRe: Updated for VB.Net or C#? Pin
shenjian7-Feb-10 5:44
membershenjian7-Feb-10 5:44 
GeneralRe: Updated for VB.Net or C#? Pin
yjvujj30-Sep-11 9:05
memberyjvujj30-Sep-11 9:05 
QuestionGreat code but how can I do this? Pin
Anonymous5-Oct-04 12:50
sussAnonymous5-Oct-04 12:50 
QuestionIs there a Way to set XP generally to B/W ? Pin
e-t-c27-Sep-04 0:01
membere-t-c27-Sep-04 0:01 
GeneralLeaving a window Pin
Anonymous20-Jun-04 5:29
sussAnonymous20-Jun-04 5:29 
GeneralC# Pin
Harsmaken18-May-04 9:20
memberHarsmaken18-May-04 9:20 
GeneralPreventing a WM_CLOSE message for CFadeWnd (e.g. Alt+F4) + fix Pin
Valentin Mocanu18-Mar-04 3:27
memberValentin Mocanu18-Mar-04 3:27 
GeneralCool Pin
Hockey14-Feb-04 14:33
memberHockey14-Feb-04 14:33 
QuestionFading non-rectangular dialogs? Pin
t00lb0x_t00lb0x25-Jan-04 5:01
membert00lb0x_t00lb0x25-Jan-04 5:01 
AnswerRe: Fading non-rectangular dialogs? Pin
A sleepy one30-Jan-04 0:48
memberA sleepy one30-Jan-04 0:48 
GeneralSmall bug and fix Pin
Andrew122-Dec-03 3:03
memberAndrew122-Dec-03 3:03 
QuestionFading like in WinXP? Pin
Mirza Miletic18-Dec-03 12:04
memberMirza Miletic18-Dec-03 12:04 
AnswerRe: Fading like in WinXP? Pin
A sleepy one30-Jan-04 0:43
memberA sleepy one30-Jan-04 0:43 
GeneralKeep updating Pin
Major Major20-Nov-03 2:48
memberMajor Major20-Nov-03 2:48 
GeneralRe: Keep updating Pin
John A. Johnson20-Nov-03 23:49
memberJohn A. Johnson20-Nov-03 23:49 
GeneralRe: Keep updating Pin
Major Major24-Nov-03 3:36
memberMajor Major24-Nov-03 3:36 
GeneralThats Great Pin
Balkrishna Talele19-Nov-03 21:07
memberBalkrishna Talele19-Nov-03 21:07 
GeneralWow! Pin
machocr13-Nov-03 7:29
membermachocr13-Nov-03 7:29 
GeneralRe: Wow! Pin
John A. Johnson17-Nov-03 0:14
memberJohn A. Johnson17-Nov-03 0:14 
GeneralGreat code! one minor thing.. Pin
Boonchai M.12-Nov-03 21:20
memberBoonchai M.12-Nov-03 21:20 
GeneralRe: Great code! one minor thing.. Pin
John A. Johnson12-Nov-03 21:30
memberJohn A. Johnson12-Nov-03 21:30 
QuestionHow about doing the fade in a thread? Pin
Jeremy Davis12-Nov-03 1:32
memberJeremy Davis12-Nov-03 1:32 
AnswerRe: How about doing the fade in a thread? Pin
John A. Johnson12-Nov-03 2:07
memberJohn A. Johnson12-Nov-03 2:07 
GeneralRe: How about doing the fade in a thread? Pin
skratt19-Nov-03 6:40
memberskratt19-Nov-03 6:40 
GeneralRe: How about doing the fade in a thread? Pin
John A. Johnson19-Nov-03 10:03
memberJohn A. Johnson19-Nov-03 10:03 
GeneralWile E Coyote Pin
Paul Evans11-Nov-03 23:35
memberPaul Evans11-Nov-03 23:35 
GeneralExcellent Pin
manos_crete11-Nov-03 21:34
membermanos_crete11-Nov-03 21:34 
GeneralYet another. Pin
lusores11-Nov-03 7:17
susslusores11-Nov-03 7:17 
GeneralGood Job! Pin
MrLeeGriffiths11-Nov-03 0:51
memberMrLeeGriffiths11-Nov-03 0:51 
GeneralFade it back Pin
Verifier11-Nov-03 0:37
memberVerifier11-Nov-03 0:37 
GeneralRe: Fade it back Pin
John A. Johnson11-Nov-03 3:53
memberJohn A. Johnson11-Nov-03 3:53 
GeneralPretty cool... Pin
benjaminl10-Nov-03 8:44
memberbenjaminl10-Nov-03 8:44 
GeneralRe: Pretty cool... Pin
John A. Johnson10-Nov-03 23:31
memberJohn A. Johnson10-Nov-03 23:31 
GeneralRe: Pretty cool... Pin
John A. Johnson11-Nov-03 3:24
memberJohn A. Johnson11-Nov-03 3:24 
GeneralRe: Pretty cool... Pin
benjaminl11-Nov-03 7:06
memberbenjaminl11-Nov-03 7:06 
GeneralRe: Pretty cool... Pin
John A. Johnson11-Nov-03 10:45
memberJohn A. Johnson11-Nov-03 10:45 
GeneralSweet Pin
Rome Singh4-Nov-03 3:09
memberRome Singh4-Nov-03 3:09 
GeneralRe: Sweet Pin
John A. Johnson4-Nov-03 9:33
memberJohn A. Johnson4-Nov-03 9:33 
GeneralYes, really sweet, but Pin
PixiGreg10-Nov-03 9:48
memberPixiGreg10-Nov-03 9:48 
GeneralGreat, reminds me of Visage Pin
Patje3-Nov-03 21:47
memberPatje3-Nov-03 21:47 
GeneralFade to any gradient Pin
Mikko Mononen3-Nov-03 13:06
memberMikko Mononen3-Nov-03 13:06 
GeneralRe: Fade to any gradient Pin
John A. Johnson3-Nov-03 22:52
memberJohn A. Johnson3-Nov-03 22:52 
GeneralPlay with the demo Pin
JOHN113-Nov-03 11:29
memberJOHN113-Nov-03 11:29 
GeneralRe: Play with the demo Pin
John A. Johnson3-Nov-03 11:39
memberJohn A. Johnson3-Nov-03 11:39 
GeneralI have an idea !! Pin
WREY3-Nov-03 9:26
memberWREY3-Nov-03 9:26 

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.150520.1 | Last Updated 3 Feb 2014
Article Copyright 2003 by John A. Johnson
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid