(1)用Photoshop等制作启动画面图像,保存为bmp格式。
(2)用Appwizard建一个基于单文档的工程Splash。
(3)在资源中插入位图资源
打开VC++的资源编辑器,用鼠标右键单击Resources文件夹,选择Import命令,插入所制作的位图。如果位图超过256色,VC会弹出一个对话框,提示位图已经插入但不能在位图编辑器中显示,确定即可。将位图ID改为IDB_SPLASH。
(4)添加Splash Screen控件
①选择菜单“project”/“Add To Project”/“Conponents and Controls”打开对话框,在列表框中双击“Visual C++ Conponents”选项,选择“Splash Screen”控件,然后单击“Insert”。
②确认或修改类名和位图资源ID,单击OK确认。
③编译、连接,漂亮的启动画面就显示出来了。
(5)如果需要改变启动画面的停留时间,就修改SetTimer()函数的第二个参数,默认是750 毫秒。该函数所在位置:
int CSplashWnd::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
...
// Set a timer to destroy the splash screen.
SetTimer(1, 2000, NULL); //修改第二个参数以调整画面停留时间
return 0;
}
这样就生成了Splash1.cpp 和Splash1.h 这两个文件
2、制作基于对话框的应用程序启动画面
(1)建立基于对话框的工程Cover。
(2)文件移植
①将Splash1.cpp 和Splash1.h 两个文件从步骤一建立的工程拷贝到Cover工程中,并且分别加入到Source Files和Header Files中;
②导入位图文件到工程的资源中,改ID为IDB_SPLASH。
(3)修改代码,实现启动画面的调用
①添加CCoverApp 的InitInstance() 函数代码
#include "Splash1.h" //加在Cover.cpp文件的头文件调用部位
BOOL CCoverApp::InitInstance()
{
CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo);
CSplashWnd::EnableSplashScreen(cmdInfo.m_bShowSplash);
...
}
②使用ClassWizard 添加OnCreate() 函数到对话框类CCoverDlg中,并修改代码#include "Splash1.h" //加在CoverDlg.cpp文件的头文件调用部位
int CCoverDlg::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
...
CSplashWnd::ShowSplashScreen(this); //显示启动画面
...
}
3、使得启动画面消失后再显示主程序
①在CCoverDlg类中添加WM­_TIMER消息响应函数
void CCoverDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
this->MoveWindow(300,300,600,400);
CDialog::OnTimer(nIDEvent);
}
② 在intCCoverDlg::OnCreate(LPCREATESTRUCT lpCreateStruct)中添加如下代码:
CSplashWnd::ShowSplashScreen(this);
this->MoveWindow(0,0,0,0);
this->SetTimer(1,2000,NULL);//注意这个2000一点要等于步骤1中的2000
return 0;
③在void CCoverDlg::OnPaint()的末尾添加如下代码:
this->KillTimer(1);
附Splash1类
// CG: This file was added by the Splash Screen component.
#ifndef _SPLASH_SCRN_
#define _SPLASH_SCRN_
// Splash.h : header file
//
/
// Splash Screen class
class CSplashWnd : public CWnd
{
// Construction
protected:
CSplashWnd();
// Attributes:
public:
CBitmap m_bitmap;
// Operations
public:
static void EnableSplashScreen(BOOL bEnable = TRUE);
static void ShowSplashScreen(CWnd* pParentWnd = NULL);
static BOOL PreTranslateAppMessage(MSG* pMsg);
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CSplashWnd)
//}}AFX_VIRTUAL
// Implementation
public:
~CSplashWnd();
virtual void PostNcDestroy();
protected:
BOOL Create(CWnd* pParentWnd = NULL);
void HideSplashScreen();
static BOOL c_bShowSplashWnd;
static CSplashWnd* c_pSplashWnd;
// Generated message map functions
protected:
//{{AFX_MSG(CSplashWnd)
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
afx_msg void OnPaint();
afx_msg void OnTimer(UINT nIDEvent);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
#endif
// CG: This file was added by the Splash Screen component.
// Splash.cpp : implementation file
//
#include "stdafx.h" // e. g. stdafx.h
#include "resource.h" // e.g. resource.h
#include "Splash.h" // e.g. splash.h
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif
/
// Splash Screen class
BOOL CSplashWnd::c_bShowSplashWnd;
CSplashWnd* CSplashWnd::c_pSplashWnd;
CSplashWnd::CSplashWnd()
{
}
CSplashWnd::~CSplashWnd()
{
// Clear the static window pointer.
ASSERT(c_pSplashWnd == this);
c_pSplashWnd = NULL;
}
BEGIN_MESSAGE_MAP(CSplashWnd, CWnd)
//{{AFX_MSG_MAP(CSplashWnd)
ON_WM_CREATE()
ON_WM_PAINT()
ON_WM_TIMER()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
void CSplashWnd::EnableSplashScreen(BOOL bEnable /*= TRUE*/)
{
c_bShowSplashWnd = bEnable;
}
void CSplashWnd::ShowSplashScreen(CWnd* pParentWnd /*= NULL*/)
{
if (!c_bShowSplashWnd || c_pSplashWnd != NULL)
return;
// Allocate a new splash screen, and create the window.
c_pSplashWnd = new CSplashWnd;
if (!c_pSplashWnd->Create(pParentWnd))
delete c_pSplashWnd;
else
c_pSplashWnd->UpdateWindow();
}
BOOL CSplashWnd::PreTranslateAppMessage(MSG* pMsg)
{
if (c_pSplashWnd == NULL)
return FALSE;
// If we get a keyboard or mouse message, hide the splash screen.
if (pMsg->message == WM_KEYDOWN ||
pMsg->message == WM_SYSKEYDOWN ||
pMsg->message == WM_LBUTTONDOWN ||
pMsg->message == WM_RBUTTONDOWN ||
pMsg->message == WM_MBUTTONDOWN ||
pMsg->message == WM_NCLBUTTONDOWN ||
pMsg->message == WM_NCRBUTTONDOWN ||
pMsg->message == WM_NCMBUTTONDOWN)
{
c_pSplashWnd->HideSplashScreen();
return TRUE; // message handled here
}
return FALSE; // message not handled
}
BOOL CSplashWnd::Create(CWnd* pParentWnd /*= NULL*/)
{
if (!m_bitmap.LoadBitmap(IDB_SPLASH))
return FALSE;
BITMAP bm;
m_bitmap.GetBitmap(&bm);
return CreateEx(0,
AfxRegisterWndClass(0, AfxGetApp()->LoadStandardCursor(IDC_ARROW)),
NULL, WS_POPUP | WS_VISIBLE, 0, 0, bm.bmWidth, bm.bmHeight, pParentWnd->GetSafeHwnd(), NULL);
}
void CSplashWnd::HideSplashScreen()
{
// Destroy the window, and update the mainframe.
DestroyWindow();
AfxGetMainWnd()->UpdateWindow();
}
void CSplashWnd::PostNcDestroy()
{
// Free the C++ class.
delete this;
}
int CSplashWnd::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CWnd::OnCreate(lpCreateStruct) == -1)
return -1;
// Center the window.
CenterWindow();
// Set a timer to destroy the splash screen.
SetTimer(1, 1750, NULL);
return 0;
}
void CSplashWnd::OnPaint()
{
CPaintDC dc(this);
CDC dcImage;
if (!dcImage.CreateCompatibleDC(&dc))
return;
BITMAP bm;
m_bitmap.GetBitmap(&bm);
Paint the image.
CBitmap* pOldBitmap = dcImage.SelectObject(&m_bitmap);
dc.BitBlt(0, 0, bm.bmWidth, bm.bmHeight, &dcImage, 0, 0, SRCCOPY);
dcImage.SelectObject(pOldBitmap);
}
void CSplashWnd::OnTimer(UINT nIDEvent)
{
// Destroy the splash screen window.
HideSplashScreen();
}