本文介紹了如何在VC中繪制軟件初始畫(huà)面的方法,具體分10個(gè)步驟來(lái)講解。
1.創(chuàng)建一個(gè)SDI或MDI工程。
2.新建或?qū)胍粋€(gè)ID號(hào)為IDB—SPLASH的位圖。
3.從CWnd類(lèi)派生一個(gè)名為CSplashWnd的子類(lèi),并添加兩個(gè)保護(hù)成員變量:
CBitmap m—bitmap;//初始畫(huà)面位圖
static CSplashWnd c—pSplashWnd;
//指向初始畫(huà)面窗口的指針
c—pSplashWnd為靜態(tài)成員變量,應(yīng)在類(lèi)的實(shí)現(xiàn)文件(.cpp)開(kāi)頭說(shuō)明:
CSplashWnd CSplashWnd::c—pSplashWnd;
4.向CSplashWnd類(lèi)中加入一個(gè)靜態(tài)公有成員函數(shù)ShowSplashScreen,此函數(shù)將被主框架窗口調(diào)用:
void CSplashWnd::ShowSplashScreen(CWnd pParentWnd)
{ //此函數(shù)傳遞的參數(shù)是主框架窗口
if(c—pSplashWnd!=NULL) return;
c—pSplashWnd=new CSplashWnd;
if(!c—pSplashWnd->Create(pParentWnd))
//創(chuàng)建初始畫(huà)面窗口
delete c—pSplashWnd;
else
c—pSplashWnd->UpdateWindow();
//顯示初始畫(huà)面窗口
}
5.編輯ShowSplashScreen函數(shù)中調(diào)用的Create函數(shù)(保護(hù)成員函數(shù)):
BOOL CSplashWnd::Create(CWnd pParentWnd){
if(!m—bitmap.LoadBitmap(IDB—SPLASH))
//載入位圖
return FALSE;
BITMAP bm;
m—bitmap.GetBitmap(&bm);
return CreateEx(0, AfxRegisterWndClass(0, AfxGetApp()->LoadStandardCursor(IDC—WAIT)),NULL, WS—POPUP | WS—VISIBLE, 0, 0,bm.bmWidth,m.bmHeight,pParentWnd->GetSafeHwnd(), NULL);
//創(chuàng)建主框架窗口的子窗口
}
6. CreateEx將調(diào)用OnCreate函數(shù)進(jìn)行窗口的初始化,重載此函數(shù):
int CSplashWnd::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CWnd::OnCreate(lpCreateStruct)==-1)
return -1;
CenterWindow();
SetTimer(1,1000,NULL); //時(shí)間控制
return 0;
}
7.顯示窗口時(shí)發(fā)送WM—PAINT消息,所以我們映射此消息:
void CSplashWnd::OnPaint()
{
CPaintDC dc(this);
CDC dcImage;
if(!dcImage.CreateCompatibleDC(&dc)) return;
BITMAP bm;
m—bitmap.GetBitmap(&bm);
CBitmap pOldBitmap=dcImage.SelectObject(&m—bitmap);
dc.BitBlt(0,0,bm.bmWidth,bm.bmHeight,&dcImage,0,0,SRCCOPY);
dcImage.SelectObject(pOldBitmap);
}
8.映射WM—TIMER消息,從而在一定時(shí)間后銷(xiāo)毀窗口:
void CSplashWnd::OnTimer(UINT nIDEvent)
{
DestroyWindow(); //銷(xiāo)毀初始畫(huà)面窗口
AfxGetMainWnd()->UpdateWindow();
//刷新主框架窗口
}
9.為防止內(nèi)存溢出,窗口銷(xiāo)毀后要釋放CSplashWnd對(duì)象,為此,我們重載虛擬函數(shù)PostNcDestroy,此函數(shù)在窗口銷(xiāo)毀后調(diào)用:
void CSplashWnd::PostNcDestroy()
{
delete this;
}
10.最后,為了顯示初始畫(huà)面,我們?cè)谥骺蚣艽翱诘腛nCreate函數(shù)最后調(diào)用ShowSplashScreen函數(shù),當(dāng)然別忘了將CSplashWnd的頭文件包含進(jìn)去:
CSplashWnd::ShowSplashScreen(this);
以上程序在VC++6.0中調(diào)試通過(guò)。
新聞熱點(diǎn)
疑難解答