MFC中添加Splash Screen

1.  新建一个MFC项目SDI或MDI。

2.  新建或导入一个ID为IDB_SPLASH的位图。

3.  添加现有项SplashWnd.h和SplashWnd.cpp。

SplashWnd.h源代码
 
   

#ifndef _SPLASH_HEADER_
#define _SPLASH_HEADER_

#pragma once
// #include "afxwin.h"

// CSplashWnd

class CSplashWnd : public CWnd
{
// DECLARE_DYNAMIC(CSplashWnd)

public :
CSplashWnd();
virtual ~ CSplashWnd();
virtual void PostNcDestroy();

public :
CBitmap m_bitmap;

protected :
static BOOL c_bShowSplashWnd;
static CSplashWnd * c_pSplashWnd;

public :
static void EnableSplashScreen(BOOL bEnable = TRUE);
static void ShowSplashScreen(CWnd * pParentWnd = NULL);
static BOOL PreTranslateAppMessage(MSG * pMsg);

protected :
BOOL Create(CWnd
* pParentWnd = NULL);
void HideSplashScreen();

afx_msg
int OnCreate(LPCREATESTRUCT lpCreateStruct);
afx_msg
void OnPaint();
afx_msg
void OnTimer(UINT nIDEvent);

protected :
DECLARE_MESSAGE_MAP()
};

#endif
SplashWnd.cpp源代码
 
   
// SplashWnd.cpp : 实现文件
//

#include
" stdafx.h "
#include
" resource.h "
#include
" SplashWnd.h "


// CSplashWnd

// IMPLEMENT_DYNAMIC(CSplashWnd, CWnd)

BOOL CSplashWnd::c_bShowSplashWnd
= FALSE;
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)
ON_WM_CREATE()
ON_WM_PAINT()
ON_WM_TIMER()
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 , 1000 , 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();
}

4.  在App主程序实现代码中把上面的头文件包含进去,并在InitInstance函数中添加下面一段代码。 

 
  
{
  CCommandLineInfo cmdInfo;
  ParseCommandLine(cmdInfo);
  CSplashWnd::EnableSplashScreen(cmdInfo.m_bShowSplash);
}

5.  最后,在主框架窗口实现代码中包含Splash头文件,在OnCreate函数return前调用CSplashWnd类的ShowSplashScreen函数即可。 

 
  
// 显示Splash窗口
CSplashWnd::ShowSplashScreen( this );

 

转载于:https://www.cnblogs.com/flyandon/archive/2010/09/21/1832475.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值