在Windows應(yīng)用程序中,對(duì)話框是應(yīng)用最廣泛也是比較難控制其風(fēng)格(外表)的一類窗口。相信用過Windows 的朋友在享受其強(qiáng)大功能的同時(shí),一定也為它所提供的具有立體感的界面而感嘆吧。通常情況下,對(duì)話框的彈出和消隱都是瞬時(shí)的,下面將介紹如何實(shí)現(xiàn)對(duì)話框的動(dòng)畫彈出和消隱,增強(qiáng)程序的美觀性。
請(qǐng)按以下步驟實(shí)現(xiàn):
第一步:生成我們的工程(基于對(duì)話框)FlashDlg,所有的選項(xiàng)都取默認(rèn)值,在對(duì)話框上隨意添加幾個(gè)控件。
第二步:在對(duì)話框的類頭文件中定義如下變量,如下:
CPoint point; int nWidth,nHeight; int dx,dy; int dx1,dy1;
第三步:在OnInitDialog()中添加如下代碼:
BOOL CFlashDlgDlg::OnInitDialog() { CDialog::OnInitDialog(); CRect dlgRect; GetWindowRect(dlgRect); CRect desktopRect; GetDesktopWindow()->GetWindowRect(desktopRect); 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; SetTimer(1,10 , NULL); return TRUE; }
第四步:添加OnTimer函數(shù),添加如下代碼:
void CFlashDlgDlg::OnTimer(UINT nIDEvent) { // TODO: Add your message handler code here and/or call default 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); }
好了,對(duì)話框的動(dòng)畫出現(xiàn)和消隱實(shí)現(xiàn)了,運(yùn)行程序我們會(huì)發(fā)現(xiàn)對(duì)話框平滑的劃出,關(guān)閉程序我們會(huì)發(fā)現(xiàn)對(duì)話框平滑的消失。
|
溫馨提示:喜歡本站的話,請(qǐng)收藏一下本站!