动画式对话框类






4.80/5 (15投票s)
2002年2月12日
3分钟阅读

140674

4280
CAniDialog:一个派生自CDialog并使用DrawWireRects的动画式对话框类。
引言
我一直在使用Almond和Maunder的DrawWireRects
函数,因为我总是希望我的程序看起来很酷。该函数非常棒,但是,要使用它,我需要在每个我想让它生效的对话框类中重载OnCreate
和OnDestroy
函数,直接重用DrawWireRects
函数有一些不便。
- 如果在一个项目中使用很多(比如说5个)不同类型的对话框,每个对话框都需要是一个新的类,因为它们的功能不同,那么我需要在每个类中重载
OnCreate
和OnDestroy
函数,并添加一些其他成员来补充动画效果。这将会有相当多的重复代码。 - 除了动画化对话框之外,我还可能希望不时地进行一些调整,例如,我希望一个名为
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.h和AniDialog.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!