VC启动窗口画面制作方法研究

源代码运行效果图如下:

1. 概述

前几天在设计软件时,选择VC作为开发工具,想做个启动画面,由于以前没有制作过,所以到网上搜了一通。网上有几篇相关文章,有两篇我觉得很有价值:一篇是关于 为方便显示图像制作的CPicture类的文章,原文是由Paul DiLascia写的解答,很有影响力;还有一篇是关于制作真彩启动画面的文章,不过其限制对位图操作,而不支持jpg, gif,而且使用繁琐,基本上是对Splash Screen组件导入后的代码进行简单修改。琢磨了好大一会儿才学会使用。

有感于现有材料使用起来不方便,随进行了整合和再封装处理,设计了CSplashWnd类,使用起来非常简便。下面就把我设计的类介绍给大家。有什么不当或错误之处,敬请指正。我的Email: zhengxiliu@sohu.com

2.CSplashWnd功能

能够显示真彩启动画面,能在画面上显示初始化文字信息,支持jpg,gif,bmp图像文件。

3. CSplashWnd的设计

3.1 用户关心的接口

用户使用的公开接口:

public:
CSplashWnd(LPCTSTR lpszFileName);// 指定作为启动画面的图像文件,并装载
BOOL ShowSplash();//显示画面
void CloseSplash();//关闭画面
void ShowText(LPCTSTR pCh);在显示的图像上中间位置处显示初始化信息文字

3.2 其他接口
系统使用的公开接口:(用户不关心)

~CSplashWnd()
void PostNcDestroy();

私有接口:(用户不关心)

BOOL Create(CWnd* pParentWnd = NULL);
int OnCreate(LPCREATESTRUCT lpCreateStruct);
void OnPaint();

3.3 数据设计(用户不关心)

BOOL fileIsValid//指示
CPicture pic;//用于对图像文件进行操作的类
int width,height;

3.4 限制

√ 不允许继承。
√ 为简化接口,只提供从文件装载图像

3.5 需要的头文件

StdAfx.h, VC++6.0自动生成的对MFC的支持,不同的工程选项会产生不同的StdAfx.h。

afxwin.h 支持CRect类

atlbase.h 提供对IPicture (COM类)的支持。

afxpriv2.h提供对CArchiveStream类的支持。

4.类的健壮性和可调试性设计

图像文件是否有效?

需要检查文件是否有效,当装载图像文件失败时,fileIsValid为false,否则为true。这样在调用ShowSplash时将什么都不做,返回false。这时,用户应检查图像文件是否存在,文件名称拼写是否正确。

5. 用法

√ 将CSplashWnd类加入项目中

√ 在使用CSplashWnd类的文件中#include “Splash.h”

√ 在合适的位置定义一个CSplashWnd对象

√ 在想显示启动画面的地方调用ShowSplash显示于屏幕上

√ 如果想在启动画面上显示一些初始化或其他提示信息,调用ShowText。

√ 在你想关闭启动画面的地方

在你的App类InitInstance函数中,显示主窗口之前使用,进行上述步骤,这是最典型的用法,如下面代码所示。

BOOL CTsApp::InitInstance()
{
AfxEnableControlContainer();
#ifdef _AFXDLL
Enable3dControls();                  // Call this when using MFC in a shared DLL
#else
Enable3dControlsStatic();   // Call this when linking to MFC statically
#endif
SetRegistryKey(_T("Local AppWizard-Generated Applications"));
LoadStdProfileSettings();  // Load standard INI file options (including MRU)
CSingleDocTemplate* pDocTemplate;
pDocTemplate = new CSingleDocTemplate(
IDR_MAINFRAME,
RUNTIME_CLASS(CTsDoc),
RUNTIME_CLASS(CMainFrame),       // main SDI frame window
RUNTIME_CLASS(CTsView));
AddDocTemplate(pDocTemplate);
CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo);
if (!ProcessShellCommand(cmdInfo))
return FALSE;
/
CSplashWnd* pCsw = new CSplashWnd("fx.jpg");//located in the local directory,or else full-path file name is needed
pCsw->ShowSplash();
Sleep(750);//delay some time to observe the image displayed.
pCsw->CloseSplash();
delete pCsw;
pCsw = NULL;
/
// The one and only window has been initialized, so show and update it.
m_pMainWnd->ShowWindow(SW_SHOW);
m_pMainWnd->UpdateWindow();
return TRUE;
}

6. 类代码

6.1 Splash.h

/
//Written by Liu Zhengxi
//May 5,2003
//Compiles with Visual C++ 6.0 for Windows 98 and probably Windows 2000
// too.
/
#ifndef _SPLASH
#define _SPLASH
#include
#include
// Splash.h : header file
//
/
//   Splash Screen class
#pragma once
///
// Picture object—encapsulates IPicture
//Written by Paul DiLascia.
//used to display picture
//
// declare CPicture class
//
class CPicture {
public:
BOOL Render(CDC* pDC,CRect rc,LPCRECT prcMFBounds=NULL) const;
CPicture();
~CPicture();
// Load from various resources
BOOL Load(UINT nIDRes);
BOOL Load(LPCTSTR pszPathName);
BOOL Load(CFile& file);
BOOL Load(CArchive& ar);
BOOL Load(IStream* pstm);
// render to device context
CSize GetImageSize(CDC* pDC=NULL) const;
operator IPicture*() {
return m_spIPicture;
}
void GetHIMETRICSize(OLE_XSIZE_HIMETRIC& cx, OLE_YSIZE_HIMETRIC& cy)
const {
cx = cy = 0;
const_cast<CPicture*>(this)->m_hr = m_spIPicture->get_Width(&cx);
ASSERT(SUCCEEDED(m_hr));
const_cast<CPicture*>(this)->m_hr = m_spIPicture->get_Height(&cy);
ASSERT(SUCCEEDED(m_hr));
}
void Free() {
if (m_spIPicture) {
m_spIPicture.Release();
}
}
protected:
CComQIPtr<IPicture>m_spIPicture;     // ATL smart pointer to IPicture
HRESULT m_hr;                        // last error code
};
///
//
//declare CSplashWnd
//
class CSplashWnd : public CWnd
{
// Construction
public:
CSplashWnd(LPCTSTR lpszFileName);
// Operations
public:
BOOL ShowSplash();
BOOL PreTranslateAppMessage(MSG* pMsg);
void ShowText(LPCTSTR lpStr);
void CloseSplash();
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CSplashWnd)
//}}AFX_VIRTUAL
// Implementation
public:
~CSplashWnd();
virtual void PostNcDestroy();
private:
BOOL Create(CWnd* pParentWnd = NULL);
// Generated message map functions
private:
//{{AFX_MSG(CSplashWnd)
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
afx_msg void OnPaint();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
private:
int height;//the height of the displayed picture
int width;//the width of the displayed picture
CPicture pic;//used to operate the picture
BOOL fileIsValid;
};
#endif

6.2 Splash.cpp

///
//Written by Liu Zhengxi
//May 5,2003
//Compiles with Visual C++ 6.0 for Windows 98 and probably Windows 2000
// too.
/
//
// Splash.cpp : implementation file
//
#include <atlbase.h>
#include <afxwin.h>
#include <afxpriv2.h>
#include "stdafx.h"  // e. g. stdafx.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
/
//   CSplashWnd class
//constructor
//Load image from the given file
//
CSplashWnd::CSplashWnd(LPCTSTR lpszFileName)
{
fileIsValid = pic.Load(lpszFileName);
if(fileIsValid)
{
CSize cz = pic.GetImageSize(NULL);
width = cz.cx;
height = cz.cy;
}
}
//nothing to do
//deconstructor
//
CSplashWnd::~CSplashWnd()
{
}
//message map
//
BEGIN_MESSAGE_MAP(CSplashWnd, CWnd)
//{{AFX_MSG_MAP(CSplashWnd)
ON_WM_CREATE()
ON_WM_PAINT()
ON_WM_TIMER()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
//ShowSplash
//to display the given image on screen
//
BOOL CSplashWnd::ShowSplash()
{
if(fileIsValid)
{
if (!Create(AfxGetMainWnd()))
return false;
else
{
UpdateWindow();
return true;
}
}
else
{
return false;
}
}
//PreTranslateAppMessage
//
BOOL CSplashWnd::PreTranslateAppMessage(MSG* pMsg)
{
// 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)
{
CloseSplash();
return TRUE; // message handled here
}
return FALSE;      // message not handled
}
//Create
//make a popup splash window
//
BOOL CSplashWnd::Create(CWnd* pParentWnd /*= NULL*/)
{
return CreateEx(0,
AfxRegisterWndClass(0, AfxGetApp()->LoadStandardCursor(IDC_ARROW)),
NULL, WS_POPUP | WS_VISIBLE, 0, 0, width, height, pParentWnd->GetSafeHwnd(), NULL);
}
//CloseSplash
//Quit the splash window
//
void CSplashWnd::CloseSplash()
{
// Destroy the window, and update the mainframe.
DestroyWindow();
}
//do nothing
//
void CSplashWnd::PostNcDestroy()
{
}
//OnCreate
//put the splash window on center
//
int CSplashWnd::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CWnd::OnCreate(lpCreateStruct) == -1)
return -1;
// Center the window.
CenterWindow();
return 0;
}
//OnPaint
//Display the given image
//
void CSplashWnd::OnPaint()
{
CPaintDC dc(this);
CRect rc(0,0,0,0);;
pic.Render(&dc, rc);
}
//ShowText
//sometimes if we show what we are doing (I display the information on the center of //the picture ), the customer will be more //patient
//
//
void CSplashWnd::ShowText(LPCTSTR lpStr)
{
Invalidate();
CPaintDC dc(this);
dc.SetBkMode(TRANSPARENT);
SIZE sz;
sz = (SIZE)dc.GetTextExtent(lpStr,strlen(lpStr));
dc.TextOut((width-sz.cx)/2,height/2,lpStr);
}
// MSDN Magazine — October 2001
// If this code works, it was written by Paul DiLascia.
// If not, I don''t know who wrote it.
// Compiles with Visual C++ 6.0 for Windows 98 and probably Windows 2000
// too.
// Set tabsize = 3 in your editor.
//
// CPicture implementation
//
CPicture::CPicture()
{
}
CPicture::~CPicture()
{
}
//
// Load from resource. Looks for "IMAGE" type.
//
BOOL CPicture::Load(UINT nIDRes)
{
// find resource in resource file
HINSTANCE hInst = AfxGetResourceHandle();
HRSRC hRsrc = ::FindResource(hInst,
MAKEINTRESOURCE(nIDRes),
"IMAGE"); // type
if (!hRsrc)
return FALSE;
// load resource into memory
DWORD len = SizeofResource(hInst, hRsrc);
BYTE* lpRsrc = (BYTE*)LoadResource(hInst, hRsrc);
if (!lpRsrc)
return FALSE;
// create memory file and load it
CMemFile file(lpRsrc, len);
BOOL bRet = Load(file);
FreeResource(hRsrc);
return bRet;
}
//
// Load from path name.
//
BOOL CPicture::Load(LPCTSTR pszPathName)
{
CFile file;
if (!file.Open(pszPathName, CFile::modeRead|CFile::shareDenyWrite))
return FALSE;
BOOL bRet = Load(file);
file.Close();
return bRet;
}
//
// Load from CFile
//
BOOL CPicture::Load(CFile& file)
{
CArchive ar(&file, CArchive::load | CArchive::bNoFlushOnDelete);
return Load(ar);
}
//
// Load from archive—create stream and load from stream.
//
BOOL CPicture::Load(CArchive& ar)
{
CArchiveStream arcstream(&ar);
return Load((IStream*)&arcstream);
}
//
// Load from stream (IStream). This is the one that really does it: call
// OleLoadPicture to do the work.
//
BOOL CPicture::Load(IStream* pstm)
{
Free();
HRESULT hr = OleLoadPicture(pstm, 0, FALSE,
IID_IPicture, (void**)&m_spIPicture);
ASSERT(SUCCEEDED(hr) && m_spIPicture);
return TRUE;
}
//
// Get image size in pixels. Converts from HIMETRIC to device coords.
//
CSize CPicture::GetImageSize(CDC* pDC) const
{
if (!m_spIPicture)
return CSize(0,0);
LONG hmWidth, hmHeight; // HIMETRIC units
m_spIPicture->get_Width(&hmWidth);
m_spIPicture->get_Height(&hmHeight);
CSize sz(hmWidth,hmHeight);
if (pDC==NULL) {
CWindowDC dc(NULL);
dc.HIMETRICtoDP(&sz); // convert to pixels
} else {
pDC->HIMETRICtoDP(&sz);
}
return sz;
}
//
// Render to device context. Covert to HIMETRIC for IPicture.
//
BOOL CPicture::Render(CDC* pDC, CRect rc, LPCRECT prcMFBounds) const
{
ASSERT(pDC);
if (rc.IsRectNull()) {
CSize sz = GetImageSize(pDC);
rc.right = sz.cx;
rc.bottom = sz.cy;
}
long hmWidth,hmHeight; // HIMETRIC units
GetHIMETRICSize(hmWidth, hmHeight);
m_spIPicture->Render(*pDC, rc.left, rc.top, rc.Width(), rc.Height(),
0, hmHeight, hmWidth, -hmHeight, prcMFBounds);
return TRUE;
}

参考连接:

VC启动窗口画面制作方法研究相关推荐

  1. java清除运行窗口内容,Java实现软件运行时启动信息窗口的方法

    本文实例形式详述了Java实现一个程序运行时的启动窗口效果,如常用的Microsoft Word. Borland JBuilder 等,这样的窗口称为信息窗口.使用信息窗口的好处是可以使用户在等待软 ...

  2. s域到c语言离散化方法,离散化方法研究.docx

    离散化方法研究.docx 东南大学自动化学院实 验 报 告课程名称 计算机控制技术 第 二 次实验实验名称 离散化方法的研究 院 (系) 自动化 专 业 自动化 姓 名 学 号 实 验 室 实验组别 ...

  3. Oracle Spatial中上载GIS空间数据方法研究

    Oracle Spatial中上载GIS空间数据方法研究 作者:佚名    文章来源:博客中国    点击数:6873    更新时间:2006-8-24 摘要:采用Oracle Spatial 存储 ...

  4. 基于MATLAB的多项式数据拟合方法研究-毕业论文

    摘要:本论文先介绍了多项式数据拟合的相关背景,以及对整个课题做了一个完整的认识.接下来对拟合模型,多项式数学原理进行了详细的讲解,通过对文献的阅读以及自己的知识积累对原理有了一个系统的认识.介绍多项式 ...

  5. 机器学习与数据挖掘 课程作业 基于数据驱动的空调结霜程度检测方法研究

    机器学习与数据挖掘 课程作业 基于数据驱动的空调结霜程度检测方法研究 摘要: 在我国的夏热冬冷地区,由于没有集中供暖,在冬季使用空调制热进行供暖是一种很常见的方式,但该地区冬季空气相对湿度较高并且环境 ...

  6. 关于vc++ runtime error解决方法汇总

    转自http://blog.163.com/hx_2000_long/blog/static/53697531200911695752887/ 关于vc++ runtime error解决方法汇总 1 ...

  7. windows7 x64系统安装VC++6.0的方法探讨

    破解方法: 运行setup.exe安装程序,会弹出如下的的程序兼容性助手提示:此程序存在已知的兼容性问题,这个是Win7在警告用户VC++6.0存在兼容性问题:此程序存在已知的兼容性问题.如下图,选中 ...

  8. Android App定位和规避内存泄露方法研究

    from:http://site.douban.com/android/widget/notes/350758/note/167481484/ 工作中刚好用到,网上搜到的,觉得不错,与大家分享 And ...

  9. WPF中实现先登录后启动主程序的方法

    我觉得先登录后启动应用主程序是一个很经典的问题,基本上如果要写一个应用程序都会用到这个的小环节.我在这个问题上挣扎了大半天才找到解决方案,我的实现方法我觉得有点不正宗,如果有哪位高手知道更好的方法欢迎 ...

最新文章

  1. LeetCode - Valid Number
  2. lru页面置换算法_C|内存管理|从LRU王国到NRU王国
  3. Opencv基础画图函数——line、circle、rectangle、Rect、ellipse、polylines、putText函数的用法
  4. 8s存储最佳方案_MaxiDi, 意大利:自动化物流解决方案
  5. PowerDesigner逆向工程导入MYSQL数据库总结
  6. 漫画面向对象编程,用漫画给你讲技术!
  7. Ubuntu 12.04下NFS安装配置
  8. C语言:计算1*2*3*....*100,即求100!。
  9. SSAS中出现“对象引用未被设置到对象实例”的解决记录
  10. Asf PHP扩展框架之预警模块介绍
  11. 让网站支持RSS订阅
  12. 哪些技术会决定前端开发者的未来发展?
  13. python视频人脸检测_OpenCV + python 实现人脸检测(基于照片和视频进行检测)
  14. js获取微信号_前端js可以直接获取到微信用户基本信息吗
  15. CentOS7 通过Systemctl实现脚本的开机自启(亲测有效)
  16. TinyOS总体介绍
  17. imu matlab,IMU姿态解算matlab
  18. HTML5新特性小结
  19. char matlab中,matlab中char什么意思
  20. 本题要求递归实现一个计算X的N次方 (n≥1)的函数。

热门文章

  1. springmvc通过@Value注解读取Properties配置文件的值,junit测试可以取到值,但是在业务中无法读取
  2. 手把手教你编写接口需求文档
  3. shiro subject.getprincipal()为null_(变强、变秃)Java从零开始之Shiro安全框架
  4. cclink 和发那科机器人_FANUC机器人焊枪工具坐标系设定
  5. 秦叶阳(1986-),女,就职于北京大学,安徽荣创智能科技有限公司联合创始人...
  6. 【2016年第4期】大数据应用系统的消息驱动架构
  7. 2015中国大数据技术大会在北京隆重开幕
  8. 编译原理——实验壹——TINY语言的词法分析
  9. 【操作系统】库函数与系统调用的区别
  10. 来看各种数值的反转吧(洛谷P1553题题解,Java语言描述)