![]() |
Desktop Development »
Dialogs and Windows »
General
Intermediate
License: The Code Project Open License (CPOL)
Go to sleep! Fade to B/WBy John A. JohnsonFading a window from color to black and white like Windows/XP |
VC6, VC7Win2K, WinXP, MFC, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
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:
Notice: this code will work only on 16, 24 or 32 bit display color mode.
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:
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:
CFadeWnd object before a dialog or a message-box modal call, for sample: wndFade,
wndFade.Create(this). Difficult?
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); }
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 CreateGradient callI 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.
CFadeWnd class using OnLButtonCheck and OnRButtonCheck callsOf 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 are reported inside the source code. The major help come from these articles/posts:
CreateGradient(...) call to perform a different graying effect
OnLButtonChek() and OnRButtonChek() to perform a mouse click security check inside the CFadeWnd class
Of course, all suggestions are welcome ![]()
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 9 Nov 2003 Editor: Nishant Sivakumar |
Copyright 2003 by John A. Johnson Everything else Copyright © CodeProject, 1999-2009 Web21 | Advertise on the Code Project |