Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

An Animation-Style Dialog Class

0.00/5 (No votes)
12 Feb 2002 1  
CAniDialog : An Animation-Style Dialog Class Derived from CDialog and Using DrawWireRects

Sample Image - CAniDialog.gif

Introduction

I've been using Almond and Maunder's DrawWireRects function because I always like my programs to look cool. That function is great and all, however, to use it I need to overload OnCreate and OnDestroy functions in EVERY dialog class which I want it to be effected, there are a few inconvenience on directly reusing the DrawWireRects function:

  1. If there are many (well, let's say 5) different kind of Dialog boxes I will be using in a project, each of which has to be a new class due to their different functionalities, then I need to overload OnCreate and OnDestroy functions in every class and add some other members which are needed to complement the animation effect. Those will be quite a lot of duplicated code.
  2. Other than animating the dialogs, I may also want to make some adjustment from time to time, for example, I want a dialog with class name of CMyDialog open from center and close towards its bottom-right corner, then a few minutes later I change my mind and want it close towards the system tray... anyway, I should be able to do those kind of stuff by simply using the DrawWireRects, but quickly I'll find out size of MyDialog.cpp grows up rapidly, and so do other dialog classes in that project.

It would be nice if there was an existing class that encapsulates all those necessary stuff, I could just derive my classes from it and it does all the dirty work for me. So that's where the CAniDialog class came from.

The CAniDialog class

First of all, the heart and soul of this class is the DrawWireRects function, which was written by Almond and Maunder, so credits go there. I tried to leave that function as intact as possible, some small changes were necessary to be done, however, in order to make it cooperate with other class members well.

The CAniDialog class is derived from CDialog, the aim of this class is to make using animated dialog boxes as simple as using the MessageBox function, the only thing that really needed to be provided by the user is a CPoint object, which specifies where the dialog box should pop up from and close towards if you are going to use the AS_REFER animation style flag. All other elements have default values and, in most case, are already optimized and need not be changed.

Public Methods of the CAniDialog class

// construction

CAniDialog(UINT nDlgID, CWnd* pParent);
CAniDialog(CPoint pt, UINT nDlgID, CWnd* pParent);

// operation


// access frame number

UINT GetFrameNum() const;
void SetFrameNum(UINT nFrames);

// access animation speed

UINT GetAniSpeed() const;
void SetAniSpeed(UINT nMillSec);

// access the reference point

CPoint GetRefPt() const;
void SetRefPt(CPoint pt);

// access animation styles

WORD GetOpenStyle() const;
WORD GetCloseStyle() const;
void SetAniStyles(WORD wOpenStyle, WORD wCloseStyle);
WORD GetDispStyle() const;
void SetDispStyle(WORD wDispStyle);

// temporarily enable/disable animation

void EnableAni(BOOL bEnable = TRUE);

// check object status

BOOL IsAniValid() const;

Animation Styles

Predefined animation style flags:

// animation styles


// animate from/to the refer point

#define AS_REFER        0 

// animate from/to center

#define AS_CENTER       1 

// animate from/to corners

#define AS_TOPLEFT      2 

#define AS_TOPRIGHT     3
#define AS_BOTTOMLEFT   4
#define AS_BOTTOMRIGHT  5

// animate from/to sides

#define AS_LEFT         6 
#define AS_TOP          7
#define AS_RIGHT        8
#define AS_BOTTOM       9

// animate from/to a random point inside of dialog

#define AS_RANDOM      10 

// define displaying style


// call "DrawWireRects"

#define DS_WIRE         0 
// call "::DrawAnimatedRects"

#define DS_CAPTION      1 

How To Use

To use the CAniDialog class, you need to:

  • Add AniDialog.h and AniDialog.cpp into your project and include AniDialog.h where needed.
  • Use resource editor to create a new dialog, draw whatever controls on the dialog box as you wish, then use class wizard to create a new class for it, for example, name the new class CMyDlg and let CDialog be its base class.
  • Go to MyDlg.h and use the Replace feature of your code editor to replace all CDialog with CAniDialog in whole file.
  • Go to MyDlg.cpp and use the "Replace" feature of your code editor to replace all CDialog with CAniDialog in whole file. At this point you have changed the base class of CMyDlg from CDialog into CAniDialog.
  • Modify the constructor of CMyDlg class so it looks like this:
  • // CMyDlg(CWnd* pParent = NULL);   // original
    
    CMyDlg(CPoint pt, CWnd* pParent = NULL);   // now
    
    
  • And the body of the constructor becomes this:
  • CMyDlg::CMyDlg(CPoint pt, CWnd* pParent /*=NULL*/)
    : // CDialog(CMyDlg::IDD, pParent) // original
    
    CAniDialog(pt, CMyDlg::IDD, pParent) // now
    
    {
        //{{AFX_DATA_INIT(CDemoPopupDlg)
    
        // NOTE: the ClassWizard will add 
    
        // member initialization here
    
        //}}AFX_DATA_INIT
    
    }
    

That's it! Now the CMyDlg class is ready to go. When you create a CMyDlg object, you need to pass it a CPoint object which tells the dialog where to pop up from and where to close towards.

// If you are going to use the AS_REFER flag, 

// you must provide a valid CPoint.

// If you are not going to use the AS_REFER flag, 

// then the value of this CPoint

// does not matter at all, in that case, 

// just give it one so the compiler won't

// complain.

CPoint pt;

// we are going to use the AS_REFER flag so 

// we need a valid CPoint value.

::GetCursorPos(&pt); // fetch current cursor position


CMyDlg dlg(pt);

// optional

// you can specify animation styles, displaying 

// styles, animation speed, animation

// frame numbers etc.


// the dialog will pop up from you cursor

dlg.SetAniStyles(AS_REFER, AS_CENTER); 

// position and close inwards its own center

dlg.DoModal(); // watch the animation!

License

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