2.CSplashWnd功能
能够显示真彩启动画面,能在画面上显示初始化文字信息,支持jpg,gif,bmp图像文件。 3. CSplashWnd的设计 3.1 用户关心的接口 用户使用的公开接口: 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函数中,显示主窗口之前使用,进行上述步骤,这是最典型的用法,如下面代码所示。
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 限制
√ 不允许继承。
√ 为简化接口,只提供从文件装载图像
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 p_w_picpath 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
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 p_w_picpath 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 p_w_picpath 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 p_w_picpath
//
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 p_w_picpath 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;
}
启动窗口画面类CSplashWnd
精选 转载上一篇:linux下进程间通信
下一篇:我的友情链接
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
VC6.0下配置opengl
介绍怎样在VC6.0下配置OpenGL。
职场 休闲 vc6.0 opengl -
VC6.0下配置EON SDK
网络上关于VC6.0与EON SDK的配置介绍几乎为零,这是自己摸索总结的一点经验……
Real 入事件 可执行文件 -
VC6.0同时打开多个工程
本该记下此方法的
v9 文件类型 Common -
openssl编译(VC6.0)
官网:://.openssl.org/ 得到源码: git clone https://github./openssl/openssl 一、用vc编译器编译:
android perl 编译器 linux git -
VC6.0 list sort出错#include 编译器 泛化 ios 正常运行