vs 启动画面
引用:http://topic.csdn.net/u/20080616/01/12222875-f86b-48e5-a9c7-772c0d860ff5.html
//CSplashWindow.h
class CSplashWindow : public CWnd
{
private:
CDC MemDC;
BITMAP bmBitmap;
CBitmap m_Bitmap;
CBitmap *Old_Bitmap;
public:
CSplashWindow();
~CSplashWindow();
void CreateSplash();
afx_msg void OnPaint();
DECLARE_MESSAGE_MAP()
};
// SplashWindow.cpp : CSplashWindow 类的实现
#include "stdafx.h"
#include "resource.h"
#include "SplashWindow.h"
BEGIN_MESSAGE_MAP(CSplashWindow, CWnd)
ON_WM_PAINT()
END_MESSAGE_MAP()
CSplashWindow::CSplashWindow()
{
m_Bitmap.LoadBitmap(MAKEINTRESOURCE(IDB_SPLASHWINDOW)); //Load Bitmap
m_Bitmap.GetBitmap(&bmBitmap); //Get Bitmap Info
}
CSplashWindow::~CSplashWindow()
{
}
void CSplashWindow::CreateSplash()
{
//Create Splash Window
CWnd::CreateEx(0,
AfxRegisterWndClass(
0,
AfxGetApp()->LoadStandardCursor(IDC_UPARROW)),
NULL,
// "SplashWindow Sample",
WS_POPUP,
0,
0,
bmBitmap.bmWidth, //Bitmap Width = Splash Window Width
bmBitmap.bmHeight, //Bitmap Height = Splash Window Height
NULL,
NULL,
NULL);
}
void CSplashWindow::OnPaint()
{
CPaintDC dc(this);
CBrush brush;
brush.CreateSolidBrush(RGB(64,64,255));
dc.SelectObject(&brush);
dc.Rectangle(0,0,bmBitmap.bmWidth,bmBitmap.bmHeight);
MemDC.CreateCompatibleDC(NULL); //Create Memory DC
Old_Bitmap = MemDC.SelectObject(&m_Bitmap); //Select DC
int num = bmBitmap.bmWidth/40;
dc.StretchBlt(0,0,bmBitmap.bmWidth,bmBitmap.bmHeight,&MemDC,0,0,bmBitmap.bmWidth,bmBitmap.bmHeight,SRCCOPY);
/* for(int k=0; k <40; k++) //百叶窗效果
{
for(int m=0; m <num+1; m++)
{
dc.BitBlt(m*40+k,0,1,bmBitmap.bmHeight,&MemDC,m*40+k,0,SRCCOPY);
}
Sleep(1);
}*/
MemDC.SelectObject(Old_Bitmap); //Select Bitmap
}
在你App类的InitInstance函数中
C/C++ codeBOOL CXxxxApp::InitInstance()
{
CWinApp::InitInstance();
CSplashWindow *pSplashWindow = new CSplashWindow;
pSplashWindow->CreateSplash();
pSplashWindow->CenterWindow();
pSplashWindow->ShowWindow(SW_SHOW);
pSplashWindow->UpdateWindow();
Sleep(3000); //Delay 3 Second
pSplashWindow->DestroyWindow();
delete pSplashWindow;
...
return true;
}
============================================================
引用:http://hi.baidu.com/wanzhende/item/deec5d8253f725eae496e09a
VC++ 6.0可以很方便的在工程中components and controls gallery添加Splash screen,来实现给程序插入启动动画,而VC++ 2008找不到插入components and controls gallery的地方,可以重写SplashScreen类来实现,代码如下:
====================================================================
//Splash.h头文件代码
// 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
====================================================================
//Splash.cpp 代码文件代码
// 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 "SplashScreen.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, 2000, 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();
}
插入以上两个文件即可~