您的位置:首页 > 其它

VC/MFC对话框的动画弹出和动画消隐

2008-01-28 23:28 302 查看
VC/MFC对话框的动画弹出和动画消隐
最近看到一篇文章《谈对话框的动画弹出和动画消隐》,文章提到了利用定时器实现对话框动画的效果,比葫芦化瓢,却发现不但没有动画,而且连窗口也没有了,经过简单的修改,终于实现了文中的效果,发现是作者少说了几个地方,在此将完整的实现方法贴出来,也算是在这个小程序上花费时间的小成果吧。相信我,照着这个步骤,肯定可以做出来的:->。
首先,在vc6中创建一个基于对话框的程序,并简单的创建一个自己的对话框;
以下部分摘自原文,注意重要部分加了注解,关键在这些地方:
在对话框的类头文件中定义如下变量,如下:
int nWidth,nHeight;
int dx,dy;
int dx1,dy1;
在OnInitDialog()中添加如下代码:
CDialog::OnInitDialog();
CRect dlgRect;
GetWindowRect(dlgRect);
CRect desktopRect;
GetDesktopWindow()->GetWindowRect(desktopRect);
int t1=desktopRect.Width();
int t2=dlgRect.Width();
int t3=desktopRect.Height();
int t4=dlgRect.Height();
MoveWindow((desktopRect.Width()-dlgRect.Width())/2,
(desktopRect.Height()-dlgRect.Height())/2,0,0);
nWidth=dlgRect.Width();
nHeight=dlgRect.Height();
dx=2;
dy=4;
dx1=2;
dy1=2;
//在这里设置定时器,注意和原文的区别,各参数的含义如下
//this->m_hWnd是当前窗口的句柄,1是定时器标识符,此处不重要,只要有个值就可以
//10是定时器间隔,相当于10毫秒,可以自己设置以改变动画的快慢
//0表示空,这样系统会发送一个WM_TIMER消息到应用程序队列
::SetTimer(this->m_hWnd,1,10,0);
return TRUE;
添加OnTimer函数,添加如下代码(这些都不用改,照原文就可以了):
CRect dlgRect;
GetWindowRect(dlgRect);
CRect desktopRect;
GetDesktopWindow()->GetWindowRect(desktopRect);
if(nIDEvent==1)
{MoveWindow((-dx+desktopRect.Width()-dlgRect.Width())/2,
(-dy+desktopRect.Height()-dlgRect.Height())/2,
+dx+dlgRect.Width(),
+dy+dlgRect.Height());
if(dlgRect.Width()>=nWidth)
dx=0; // do not over grow
if(dlgRect.Height()>=nHeight)
dy=0; // do not over grow
if((dlgRect.Width()>=nWidth)&&(dlgRect.Height()>=nHeight))
KillTimer(1); //Stop the timer
}
if((dlgRect.Width() >=nWidth) && (dlgRect.Height() >=nHeight))
KillTimer(1); //Stop the timer
if(nIDEvent == 2)
{MoveWindow((+dx+desktopRect.Width() - dlgRect.Width()) / 2,
(+dy+desktopRect.Height() - dlgRect.Height()) / 2,
-dx1+dlgRect.Width(),
-dy1+dlgRect.Height());
if(dlgRect.Width() <= 0)
dx1=0;// do not over grow
if(dlgRect.Height()<=0)
dy1=0;// do not over grow
if((dlgRect.Width() <= 0 ) && (dlgRect.Height()<=0))
{KillTimer(2); //Stop the timer
CDialog::OnOK();
}
}
CDialog::OnTimer(nIDEvent);
下面就是要补充的啦,一定要在自定义对话框的消息映射中加入ON_WM_TIMER(),否则系统不会处理定时器OnTimer函数,那么对话框只能按照初始情况显示,也就是长为0,宽为0。试试看,是不是搞定了。
Ps:注意OnInitDialog()中的以下语句:
MoveWindow((desktopRect.Width()-dlgRect.Width())/2,
(desktopRect.Height()-dlgRect.Height())/2,0,0);
这段代码设置了自定义对话框的初始位置和大小,后面两个值都是0,表示对话框最初是不显示出来的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: