65.9K
CodeProject 正在变化。 阅读更多。
Home

动画式对话框类

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.80/5 (15投票s)

2002年2月12日

3分钟阅读

viewsIcon

140674

downloadIcon

4280

CAniDialog:一个派生自CDialog并使用DrawWireRects的动画式对话框类。

Sample Image - CAniDialog.gif

引言

我一直在使用Almond和Maunder的DrawWireRects函数,因为我总是希望我的程序看起来很酷。该函数非常棒,但是,要使用它,我需要在每个我想让它生效的对话框类中重载OnCreateOnDestroy函数,直接重用DrawWireRects函数有一些不便。

  1. 如果在一个项目中使用很多(比如说5个)不同类型的对话框,每个对话框都需要是一个新的类,因为它们的功能不同,那么我需要在每个类中重载OnCreateOnDestroy函数,并添加一些其他成员来补充动画效果。这将会有相当多的重复代码。
  2. 除了动画化对话框之外,我还可能希望不时地进行一些调整,例如,我希望一个名为CMyDialog的对话框从中心打开并朝右下角关闭,然后在几分钟后我改变主意,希望它朝系统托盘关闭……无论如何,我应该能够通过简单地使用DrawWireRects来做到这些,但很快我会发现MyDialog.cpp 的大小迅速增长,该项目中的其他对话框类也是如此。

如果有一个现有的类封装了所有必要的代码就好了,我可以只从它派生我的类,然后它会为我完成所有繁琐的工作。这就是CAniDialog类的由来。

CAniDialog类

首先,这个类的核心和灵魂是DrawWireRects函数,它是由Almond和Maunder编写的,所以功劳归他们。我试图尽可能保持该函数原样,但是,为了使其与其他类成员良好配合,必须进行一些小的更改。

CAniDialog类是从CDialog派生的,这个类的目的是使使用动画对话框就像使用MessageBox函数一样简单,用户真正需要提供的是一个CPoint对象,该对象指定对话框应该从何处弹出以及如果使用AS_REFER动画样式标志,应该朝哪个方向关闭。所有其他元素都有默认值,在大多数情况下,已经过优化,无需更改。

CAniDialog类的公共方法

// 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

// 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 

如何使用

要使用CAniDialog类,您需要

  • AniDialog.hAniDialog.cpp 添加到您的项目,并在需要的地方包含AniDialog.h
  • 使用资源编辑器创建一个新的对话框,在对话框上绘制您想要的任何控件,然后使用类向导为其创建一个新类,例如,将新类命名为CMyDlg,并让CDialog成为它的基类。
  • 转到MyDlg.h并使用代码编辑器的“替换”功能,将整个文件中的所有CDialog替换为CAniDialog
  • 转到MyDlg.cpp并使用代码编辑器的“替换”功能,将整个文件中的所有CDialog替换为CAniDialog。此时,您已将CMyDlg的基类从CDialog更改为CAniDialog
  • 修改CMyDlg类的构造函数,使其看起来像这样
  • // CMyDlg(CWnd* pParent = NULL);   // original
    CMyDlg(CPoint pt, CWnd* pParent = NULL);   // now
    
  • 构造函数的主体变为这样
  • 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
    }
    

就是这样!现在CMyDlg类已准备就绪。当您创建CMyDlg对象时,您需要向它传递一个CPoint对象,该对象告诉对话框从何处弹出以及朝哪个方向关闭。

// 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!
© . All rights reserved.