軟件啟動時,常常需要自下而上循環(huán)滾動顯示一些諸如軟件名稱、功能、版本、用戶名、研制單位等信息的文字,在用戶按任意鍵或點擊鼠標(biāo)鍵后,立即結(jié)束演示進(jìn)入程序主界面。本文在VC中通過向工程中添加Splash screen組件,然后擴(kuò)展CSplashWnd類方便地實現(xiàn)了這一功能。設(shè)計步驟如下:
1.創(chuàng)建一個名為test的SDI工程;
2.向工程中添加Splash screen組件,即添加CSplashWnd類;
3.導(dǎo)入軟件封面的位圖資源,標(biāo)識號為IDB_SPLASH;
4.向CSplashWnd類中添加函數(shù)DrawText,在軟件封面中顯示文字。代碼如下:
void CSplashWnd::DrawText(CDC *pDC, int x, int y, int nAlign,
CString sFontName, int nFONTSIZE0,COLORREF crTextColor, CString sText)
{ //pDC:內(nèi)存dc,x,y:文字位置,nAlign:對齊方式
//sFont:字體,nFONTSIZE0:字號,crTextColor:文字顏色,sText:正文
LOGFONT lf;//字體
lf.lfStrikeOut=0;//刪除線
lf.lfCharSet = DEFAULT_CHARSET ;//字符集
lf.lfEscapement =0;//角度
lf.lfItalic = 0 ;//傾斜
lf.lfUnderline = 0 ;//下劃線
lf.lfHeight = nFONTSIZE0 ;//字號
strcpy(lf.lfFaceName,sFaceName.GetBuffer(sFontName.GetLength()));
CFont font ;
font.CreateFontIndirect(lf);
CFont *pOldFont = (CFont *)pDC->SelectObject(&font);
UINT oldAlign,uAlign ;
switch(nAlign)
{
case 0: uAlign = TA_LEFT TA_TOP ; break;
case 1: uAlign = TA_CENTER TA_TOP; break;
case 2: uAlign = TA_RIGHT TA_TOP ; break;
default: uAlign = TA_LEFT TA_TOP ; break;
}
oldAlign = pDC->SetTextAlign(uAlign);
int oldMode = pDC->SetBkMode(TRANSPARENT);
int oldColor = pDC->SetTextColor(crTextColor) ;
pDC->TextOut(x,y,sText);pDC->SetTextColor(oldColor) ;
pDC->SetTextAlign(oldAlign);
pDC->SetBkMode(oldMode);
pDC->SelectObject(pOldFont);
font.DeleteObject();
}
5. 給CSplashWnd類中添加以下變量,并在構(gòu)造函數(shù)中加入初始化代碼;
class CSplashWnd : public CWnd
{public: CDC m_dcText,m_dcImage;//顯示dc的兼容內(nèi)存dc
CBitmap* m_pBitmap;//位圖對象指針
CBitmap* m_pOldBitmapText,*m_pOldBitmap;//跟蹤內(nèi)存dc中的原位圖
CRgn m_rgn;//用于生成矩形區(qū)域
CRect m_TextRect ;//顯示文字的矩形區(qū)域
int m_nCurPos ;//文字當(dāng)前位置
}
CSplashWnd::CSplashWnd()
{ m_nCurPos = 50 ;
}
6.設(shè)置顯示文字的矩形區(qū)域。
BOOL CSplashWnd::Create(CWnd* pParentWnd /*= NULL*/)
{ m_TextRect.CopyRect(&CRect(50,100,500,300));
}
7.添加 WM_PAINT消息處理函數(shù),調(diào)用CSplashWnd::DrawText顯示文字。
void CSplashWnd::OnPaint()
{ CPaintDC dc(this);
BITMAP bm;
m_bitmap.GetBitmap(bm);
static BOOL bFirst = TRUE;//指示是否第一次繪制窗口
if(bFirst)
{ if (!m_dcImage.CreateCompatibleDC(&dc)) return;
m_pOldBitmap = m_dcImage.SelectObject(&m_bitmap);
if (!m_dcText.CreateCompatibleDC(&dc)) return;
m_pBitmap = new CBitmap ;
int nBitCount = m_dcText.GetDeviceCaps(BITSPIXEL);
m_pBitmap->CreateBitmap(bm.bmWidth,bm.bmHeight,1,nBitCount,NULL);
m_pOldBitmapText = m_dcText.SelectObject(m_pBitmap);
m_rgn.CreateRectRgn(m_TextRect.left,m_TextRect.top,
m_TextRect.right,m_TextRect.bottom);
bFirst = FALSE;
}
m_dcText.SelectClipRgn(NULL);
m_dcText.BitBlt(0, 0, bm.bmWidth, bm.bmHeight,&m_dcImage, 0, 0, SRCCOPY);
m_dcText.SelectClipRgn(&m_rgn);
int nBasex = m_TextRect.left ;
int nBasey = m_TextRect.bottom-m_nCurPos;
int nMidx = m_TextRect.left + m_TextRect.Width()/2 ;
DrawText(&m_dcText,nMidx,nBasey, 1,
"楷體_GB2312",20, RGB(0,0,255) ,"研制單位");
DrawText(&m_dcText,nMidx,nBasey+30, 1,
"楷體_GB2312",15, RGB(0,255,255) ,"武警指揮學(xué)院模擬中心");
dc.BitBlt(0, 0, bm.bmWidth, bm.bmHeight,&m_dcText, 0,0, SRCCOPY);
}
8.改變文字垂向位置,出現(xiàn)循環(huán)滾動效果:
void CSplashWnd::OnTimer(UINT nIDEvent)
{ m_nCurPos++;
if( m_nCurPos>300 ) //循環(huán)
{ m_nCurPos = 0 ;
}
Invalidate(TRUE);
}
9. 演示軟件封面時,隱藏主框架窗口:
BOOL CTestApp::InitInstance()
{ m_pMainWnd->ShowWindow(SW_HIDE);
m_pMainWnd->UpdateWindow();
}
10.按任意鍵或點擊鼠標(biāo)鍵結(jié)束封面演示:
LRESULT CSplashWnd::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{ if (c_pSplashWnd)
{
if (message == WM_KEYDOWN message == WM_SYSKEYDOWN
message == WM_LBUTTONDOWN message == WM_RBUTTONDOWN
message == WM_MBUTTONDOWN message == WM_NCLBUTTONDOWN
message == WM_NCRBUTTONDOWN message == WM_NCMBUTTONDOWN)
{ c_pSplashWnd->HideSplashScreen();
AfxGetMainWnd()->ShowWindow(SW_MAXIMIZE);
return TRUE;
}
}
return CWnd::WindowProc(message, wParam, lParam);
}
11.封面窗口銷毀后,釋放有關(guān)對象:
void CSplashWnd::PostNcDestroy()
{ m_dcImage.SelectObject(m_pOldBitmap);
m_dcText.SelectClipRgn(NULL);
m_dcText.SelectObject(m_pOldBitmapText);
m_bitmap.DeleteObject();
m_rgn.DeleteObject();
delete this;
}
文中程序在Windows9、Visual C++ 6.0下運行通過。