用VC制作带有滚动字幕的软件封面

发表于:2007-07-14来源:作者:点击数: 标签:
软件启动时,常常需要自下而上循环滚动显示一些诸如软件名称、功能、版本、用户名、研制单位等信息的文字,在用户按任意键或点击鼠标键后,立即结束演示进入程序主界面。本文在vc中通过向工程中添加splash screen组件,然后扩展csplashwnd类方便地实现了这一
软件启动时,常常需要自下而上循环滚动显示一些诸如软件名称、功能、版本、用户名、研制单位等信息的文字,在用户按任意键或点击鼠标键后,立即结束演示进入程序主界面。本文在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, srclearcase/" target="_blank" >ccopy);
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;
}
文中程序在windows9、visual c++ 6.0下运行通过。

原文转自:http://www.ltesting.net