您的位置:首页 > 其它

在VS2010的环境下,给mfc下的对话框添加启动画面

2015-10-09 10:35 441 查看
摸索了半天,终于完成简单的开机启动画面了,不过大部分都是套用别人的模板的,即下载Spash.cpp 以及Splash.h文件并添加至工程中,该文件在附件中有讲。

首先下载一张.BMP图形,该图形最好和你的对话框的尺寸大小一致,并将该图形导入至资源视图中,设置其ID为IDB_SPLASH

在你的对话框的***.cpp下的InitInstance()函数中添加以下语句:

CShellManager *pShellManager = new CShellManager;

CCommandLineInfo cmdInfo;

ParseCommandLine(cmdInfo);

CSplashWnd::EnableSplashScreen(cmdInfo.m_bShowSplash);

当然要在该文件的前面添加 #include"Splash.h"

同样在***Dlg.cpp文件的前面添加#include"Splash.h"

在该文件下添加OnCreate虚函数

int ***Dlg::OnCreate(LPCREATESTRUCT lpCreateStruct)

{

CSplashWnd::ShowSplashScreen(this);//显示启动画面

if (CDialog::OnCreate(lpCreateStruct) == -1)

return -1;

return 0;

}

BOOL***Dlg::Create(LPCTSTR lpszTemplateName, CWnd* pParentWnd)

{

// TODO: 在此添加专用代码和/或调用基类

return CDialog::Create(IDD, pParentWnd);

}

运行一下,就显示成功了,显示的时间可以在Spash.cpp的OnCreate函数中修改

文件竟然上传不了 那我直接复制在这上面吧!

Spash.cpp

#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;

CenterWindow();

SetTimer(1, 3000, NULL);//显示时间设置为3s

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();

}

splash.h文件为:

#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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: