在Windows應用程序中,對話框是應用最廣泛也是比較難控制其風格(外表)的一類窗口。相信用過Windows 的朋友在享受其強大功能的同時,一定也為它所提供的具有立體感的界面而感嘆吧。通常情況下,對話框的彈出和消隱都是瞬時的,下面將介紹如何實現對話框的動畫彈出和消隱,增強程序的美觀性。
請按以下步驟實現:
第一步:生成我們的工程(基于對話框)FlashDlg,所有的選項都取默認值,在對話框上隨意添加幾個控件。
第二步:在對話框的類頭文件中定義如下變量,如下:
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函數,添加如下代碼:
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); }
好了,對話框的動畫出現和消隱實現了,運行程序我們會發現對話框平滑的劃出,關閉程序我們會發現對話框平滑的消失。
|