VS2008中添加Splash screen控件
2015-11-30 14:01
387 查看
在VC6.0中,可以通过添加Splash组件,然后在工程调用组件中提供的函数,就可以实现。但是在VS2008中却没有这样的组件(我自己找了好久,没找到,网上查阅也没有相关资料)。在查阅了相关资料后总结如下
:
1、需要自己添加一个专门的类C SplashWnd。
代码如下:
//////////////////////////////////////////////////////////////////////////////SplashWnd.h
#ifndef _SPLASH_HEADER_
#define_SPLASH_HEADER_
#pragmaonce
//#include "afxwin.h"
//CSplashWnd
classCSplashWnd :public
CWnd
{
//DECLARE_DYNAMIC(CSplashWnd)
public:
CSplashWnd();
virtual~CSplashWnd();
virtualvoid
PostNcDestroy();
public:
CBitmap m_bitmap;
protected:
staticBOOL c_bShowSplashWnd;
staticCSplashWnd*c_pSplashWnd;
public:
staticvoid
EnableSplashScreen(BOOL bEnable=
TRUE);
staticvoid
ShowSplashScreen(CWnd*pParentWnd=
NULL);
staticBOOL PreTranslateAppMessage(MSG*pMsg);
protected:
BOOL Create(CWnd*pParentWnd=
NULL);
voidHideSplashScreen();
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()
voidCSplashWnd::EnableSplashScreen(BOOL bEnable)
{
c_bShowSplashWnd
= bEnable;
}
voidCSplashWnd::ShowSplashScreen(CWnd*pParentWnd)
{
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)
returnFALSE;
//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();
returnTRUE;
//
message handled here
}
returnFALSE;
//
message not handled
}
BOOL CSplashWnd::Create(CWnd*pParentWnd)
{
if(!m_bitmap.LoadBitmap(IDB_SPLASH))
returnFALSE;
BITMAP bm;
m_bitmap.GetBitmap(&bm);
returnCreateEx(0,
AfxRegisterWndClass(0, AfxGetApp()->LoadStandardCursor(IDC_ARROW)),
NULL, WS_POPUP
| WS_VISIBLE,0,0,
bm.bmWidth, bm.bmHeight, pParentWnd->GetSafeHwnd(), NULL);
}
voidCSplashWnd::HideSplashScreen()
{
//Destroy the window, and update the mainframe.
DestroyWindow();
AfxGetMainWnd()->UpdateWindow();
}
voidCSplashWnd::PostNcDestroy()
{
//Free the C++ class.
deletethis;
}
intCSplashWnd::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);
return0;
}
voidCSplashWnd::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);
}
voidCSplashWnd::OnTimer(UINT nIDEvent)
{
//Destroy the splash screen window.
HideSplashScreen();
}
////////////////////////////////////////////////////////////////////////////////////
2、 在App主程序实现代码中把上面的头文件包含进去,并在InitInstance函数中添加下面一段代码
CCommandLineInfo
cmdInfo;
ParseCommandLine(cmdInfo);
CSplashWnd::EnableSplashScreen(cmdInfo.m_bShowSplash);
【注意】 CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo);这两句代码已经在 InitInstance函数中存在,只需将最后一句代码加上即可。
3、 最后,在主框架窗口实现代码中包含Splash头文件,在OnCreate函数return前调用CSplashWnd类的ShowSplashScreen函数即可。
//显示Splash窗口
CSplashWnd::ShowSplashScreen(this)
4、按照上面的顺序不能完成此功能,原因如下:
08中MFC中的 InitInstance函数, 默认的代码大致是:
// 创建主 MDI 框架窗口
CMainFrame* pMainFrame = new CMainFrame;
if (!pMainFrame || !pMainFrame->LoadFrame(IDR_MAINFRAME))
{
delete pMainFrame;
return FALSE;
}
m_pMainWnd = pMainFrame;
// 分析标准外壳命令、DDE、打开文件操作的命令行
CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo);
即默认是先建立框架,再解析命令行。也就是限执行MainFrame里面的 OnCreate函数里面中的3,再执行2。在里面跟踪一下,就会发现,这样就无法满足执行条件而无法完成功能。
解决方法:把CCommandLineInfo cmdInfo;ParseCommandLine(cmdInfo);
CSplashWnd::EnableSplashScreen(cmdInfo.m_bShowSplash);
三行代码放到Instance函数的最前面即可!
5、这里面的图片是需要先将文件中的图片导入资源中, 【提醒】只能导入BMP图像,如果不是,可以先用Photoshop处理下。 【特别提醒】导入的BMP的大小要有限制, Photoshop里面也有调节图像大小功能,
一般像素宽度和高度大于700多(经验值,具体可以自己实验下),调用系统函数加载图像时,就会失败。
m_bitmap.LoadBitmap(IDB_SPLASH)
以上为个人使用经验之谈,如有不足,请留言指正!
转载地址:http://my.oschina.net/tjguo/blog/12742
:
1、需要自己添加一个专门的类C SplashWnd。
代码如下:
//////////////////////////////////////////////////////////////////////////////SplashWnd.h
#ifndef _SPLASH_HEADER_
#define_SPLASH_HEADER_
#pragmaonce
//#include "afxwin.h"
//CSplashWnd
classCSplashWnd :public
CWnd
{
//DECLARE_DYNAMIC(CSplashWnd)
public:
CSplashWnd();
virtual~CSplashWnd();
virtualvoid
PostNcDestroy();
public:
CBitmap m_bitmap;
protected:
staticBOOL c_bShowSplashWnd;
staticCSplashWnd*c_pSplashWnd;
public:
staticvoid
EnableSplashScreen(BOOL bEnable=
TRUE);
staticvoid
ShowSplashScreen(CWnd*pParentWnd=
NULL);
staticBOOL PreTranslateAppMessage(MSG*pMsg);
protected:
BOOL Create(CWnd*pParentWnd=
NULL);
voidHideSplashScreen();
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()
voidCSplashWnd::EnableSplashScreen(BOOL bEnable)
{
c_bShowSplashWnd
= bEnable;
}
voidCSplashWnd::ShowSplashScreen(CWnd*pParentWnd)
{
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)
returnFALSE;
//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();
returnTRUE;
//
message handled here
}
returnFALSE;
//
message not handled
}
BOOL CSplashWnd::Create(CWnd*pParentWnd)
{
if(!m_bitmap.LoadBitmap(IDB_SPLASH))
returnFALSE;
BITMAP bm;
m_bitmap.GetBitmap(&bm);
returnCreateEx(0,
AfxRegisterWndClass(0, AfxGetApp()->LoadStandardCursor(IDC_ARROW)),
NULL, WS_POPUP
| WS_VISIBLE,0,0,
bm.bmWidth, bm.bmHeight, pParentWnd->GetSafeHwnd(), NULL);
}
voidCSplashWnd::HideSplashScreen()
{
//Destroy the window, and update the mainframe.
DestroyWindow();
AfxGetMainWnd()->UpdateWindow();
}
voidCSplashWnd::PostNcDestroy()
{
//Free the C++ class.
deletethis;
}
intCSplashWnd::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);
return0;
}
voidCSplashWnd::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);
}
voidCSplashWnd::OnTimer(UINT nIDEvent)
{
//Destroy the splash screen window.
HideSplashScreen();
}
////////////////////////////////////////////////////////////////////////////////////
2、 在App主程序实现代码中把上面的头文件包含进去,并在InitInstance函数中添加下面一段代码
CCommandLineInfo
cmdInfo;
ParseCommandLine(cmdInfo);
CSplashWnd::EnableSplashScreen(cmdInfo.m_bShowSplash);
【注意】 CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo);这两句代码已经在 InitInstance函数中存在,只需将最后一句代码加上即可。
3、 最后,在主框架窗口实现代码中包含Splash头文件,在OnCreate函数return前调用CSplashWnd类的ShowSplashScreen函数即可。
//显示Splash窗口
CSplashWnd::ShowSplashScreen(this)
4、按照上面的顺序不能完成此功能,原因如下:
08中MFC中的 InitInstance函数, 默认的代码大致是:
// 创建主 MDI 框架窗口
CMainFrame* pMainFrame = new CMainFrame;
if (!pMainFrame || !pMainFrame->LoadFrame(IDR_MAINFRAME))
{
delete pMainFrame;
return FALSE;
}
m_pMainWnd = pMainFrame;
// 分析标准外壳命令、DDE、打开文件操作的命令行
CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo);
即默认是先建立框架,再解析命令行。也就是限执行MainFrame里面的 OnCreate函数里面中的3,再执行2。在里面跟踪一下,就会发现,这样就无法满足执行条件而无法完成功能。
解决方法:把CCommandLineInfo cmdInfo;ParseCommandLine(cmdInfo);
CSplashWnd::EnableSplashScreen(cmdInfo.m_bShowSplash);
三行代码放到Instance函数的最前面即可!
5、这里面的图片是需要先将文件中的图片导入资源中, 【提醒】只能导入BMP图像,如果不是,可以先用Photoshop处理下。 【特别提醒】导入的BMP的大小要有限制, Photoshop里面也有调节图像大小功能,
一般像素宽度和高度大于700多(经验值,具体可以自己实验下),调用系统函数加载图像时,就会失败。
m_bitmap.LoadBitmap(IDB_SPLASH)
以上为个人使用经验之谈,如有不足,请留言指正!
转载地址:http://my.oschina.net/tjguo/blog/12742
相关文章推荐
- Ubuntu系统重装后必做的事
- error c2678解决方法
- 最最最重要的内容!!!!!!
- 开始Admob广告盈利模式详细教程
- 代码文档生成工具-Doxygen生成CHM和RTF图文教程
- Centos下安装nginx rpm包
- 【183】VMware + CentOS 安装教程等
- Bootstrap 警告框(Alert)插件
- genymotion 极速模拟器
- genymotion 极速模拟器
- 感受SoftReference
- Linux下MySQL5.6的修改字符集编码为UTF8(解决中文乱码问题)
- Django程序无法获取数据库更新过的记录
- java用xstream实现xml与实体的互相转化
- 1051. Pop Sequence
- genymotion 极速模拟器
- 【codevs1014/1068】背包型动态规划
- ruby 字符串学习2
- 微信支付认证和ssl-https
- VMware11.1.1及Ubuntu14.04下载安装