软件启动时,常常需要自下而上循环滚动显示一些诸如软件名称、功能、版本、用户名、研制单位等信息的文字,在用户按任意键或点击鼠标键后,立即结束演示进入程序主界面。本文在VC中通过向工程中添加Splash screen组件,然后扩展CSplashWnd类方便地实现了这一功能。设计步骤如下:
1.创建一个名为test的SDI工程;
2.向工程中添加Splash screen组件,即添加CSplashWnd类;
3.导入软件封面的位图资源,标识号为IDB_SPLASH;
4.向CSplashWnd类中添加函数DrawText,在软件封面中显示文字。代码如下:
void CSplashWnd::DrawText(CDC *pDC, int x, int y, int nAlign,
CString sFontName, int nFONTSIZE0,COLORREF crTextColor, CString sText)
{ //pDC:内存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类中添加以下变量,并在构造函数中加入初始化代码;
class CSplashWnd : public CWnd
{public: CDC m_dcText,m_dcImage;//显示dc的兼容内存dc
CBitmap* m_pBitmap;//位图对象指针
CBitmap* m_pOldBitmapText,*m_pOldBitmap;//跟踪内存dc中的原位图
CRgn m_rgn;//用于生成矩形区域
CRect m_TextRect ;//显示文字的矩形区域
int m_nCurPos ;//文字当前位置
}
CSplashWnd::CSplashWnd()
{ m_nCurPos = 50 ;
}
6.设置显示文字的矩形区域。
BOOL CSplashWnd::Create(CWnd* pParentWnd /*= NULL*/)
{ m_TextRect.CopyRect(&CRect(50,100,500,300));
}
7.添加 WM_PAINT消息处理函数,调用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) ,"武警指挥学院模拟中心");
dc.BitBlt(0, 0, bm.bmWidth, bm.bmHeight,&m_dcText, 0,0, SRCCOPY);
}
8.改变文字垂向位置,出现循环滚动效果:
void CSplashWnd::OnTimer(UINT nIDEvent)
{ m_nCurPos++;
if( m_nCurPos>300 ) //循环
{ m_nCurPos = 0 ;
}
Invalidate(TRUE);
}
9. 演示软件封面时,隐藏主框架窗口:
BOOL CTestApp::InitInstance()
{ m_pMainWnd->ShowWindow(SW_HIDE);
m_pMainWnd->UpdateWindow();
}
10.按任意键或点击鼠标键结束封面演示:
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.封面窗口销毁后,释放有关对象:
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;
}
文中程序在Windows9x、Visual C++ 6.0下运行通过。