Click here to Skip to main content
6,634,665 members and growing! (14,335 online)
Email Password   helpLost your password?
Desktop Development » Dialogs and Windows » Dialogs     Intermediate

An Animation-Style Dialog Class

By =[ Abin ]=

CAniDialog : An Animation-Style Dialog Class Derived from CDialog and Using DrawWireRects
VC6Win2K, WinXP, MFC, Dev
Posted:11 Feb 2002
Updated:12 Feb 2002
Views:98,662
Bookmarked:52 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
18 votes for this article.
Popularity: 5.22 Rating: 4.16 out of 5
1 vote, 7.1%
1

2
2 votes, 14.3%
3
1 vote, 7.1%
4
10 votes, 71.4%
5

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

About the Author

=[ Abin ]=


Member

Location: China China

Other popular Dialogs and Windows articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 11 of 11 (Total in Forum: 11) (Refresh)FirstPrevNext
Questionquestion about CAniDialog class PinmemberMember 397888023:30 11 Dec '07  
GeneralDoes not work under windows vista PinmemberMark Essien23:04 14 Apr '07  
GeneralShow dialogs' during animation... PinmemberVladimir Georgiev13:24 2 Jul '04  
GeneralExcellent Demo Pinsussajh20:13 14 Oct '03  
GeneralHandle to a dialog PinmemberDidaa0:00 3 Jan '03  
GeneralCool cept for the big big :) PinmemberAnonymous4:35 1 Mar '02  
GeneralRe: Cool cept for the big bug (maybe not) PinmemberAnonymous6:35 1 Mar '02  
GeneralRE: No bug cool PinmemberAnonymous8:43 1 Mar '02  
GeneralRe: Cool cept for the big bug (maybe not) Pinmemberzola10:26 23 Jan '04  
GeneralModeless Dialog PinmemberNikoTanghe1:14 16 Feb '02  
GeneralGood work and a good walk through PinmemberAnonymous4:50 14 Feb '02  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 12 Feb 2002
Editor: Nishant Sivakumar
Copyright 2002 by =[ Abin ]=
Everything else Copyright © CodeProject, 1999-2009
Web22 | Advertise on the Code Project