VC启动窗口画面制作方法研究
源代碼運(yùn)行效果圖如下:
1. 概述
前幾天在設(shè)計(jì)軟件時(shí),選擇VC作為開(kāi)發(fā)工具,想做個(gè)啟動(dòng)畫面,由于以前沒(méi)有制作過(guò),所以到網(wǎng)上搜了一通。網(wǎng)上有幾篇相關(guān)文章,有兩篇我覺(jué)得很有價(jià)值:一篇是關(guān)于 為方便顯示圖像制作的CPicture類的文章,原文是由Paul DiLascia寫的解答,很有影響力;還有一篇是關(guān)于制作真彩啟動(dòng)畫面的文章,不過(guò)其限制對(duì)位圖操作,而不支持jpg, gif,而且使用繁瑣,基本上是對(duì)Splash Screen組件導(dǎo)入后的代碼進(jìn)行簡(jiǎn)單修改。琢磨了好大一會(huì)兒才學(xué)會(huì)使用。
有感于現(xiàn)有材料使用起來(lái)不方便,隨進(jìn)行了整合和再封裝處理,設(shè)計(jì)了CSplashWnd類,使用起來(lái)非常簡(jiǎn)便。下面就把我設(shè)計(jì)的類介紹給大家。有什么不當(dāng)或錯(cuò)誤之處,敬請(qǐng)指正。我的Email: zhengxiliu@sohu.com
2.CSplashWnd功能
能夠顯示真彩啟動(dòng)畫面,能在畫面上顯示初始化文字信息,支持jpg,gif,bmp圖像文件。
3. CSplashWnd的設(shè)計(jì)
3.1 用戶關(guān)心的接口
用戶使用的公開(kāi)接口:
public: CSplashWnd(LPCTSTR lpszFileName);// 指定作為啟動(dòng)畫面的圖像文件,并裝載 BOOL ShowSplash();//顯示畫面 void CloseSplash();//關(guān)閉畫面 void ShowText(LPCTSTR pCh);在顯示的圖像上中間位置處顯示初始化信息文字3.2 其他接口
系統(tǒng)使用的公開(kāi)接口:(用戶不關(guān)心)
私有接口:(用戶不關(guān)心)
BOOL Create(CWnd* pParentWnd = NULL); int OnCreate(LPCREATESTRUCT lpCreateStruct); void OnPaint();3.3 數(shù)據(jù)設(shè)計(jì)(用戶不關(guān)心)
BOOL fileIsValid//指示 CPicture pic;//用于對(duì)圖像文件進(jìn)行操作的類 int width,height;3.4 限制
√ 不允許繼承。
√ 為簡(jiǎn)化接口,只提供從文件裝載圖像
3.5 需要的頭文件
StdAfx.h, VC++6.0自動(dòng)生成的對(duì)MFC的支持,不同的工程選項(xiàng)會(huì)產(chǎn)生不同的StdAfx.h。
afxwin.h 支持CRect類
atlbase.h 提供對(duì)IPicture (COM類)的支持。
afxpriv2.h提供對(duì)CArchiveStream類的支持。
4.類的健壯性和可調(diào)試性設(shè)計(jì)
圖像文件是否有效?
需要檢查文件是否有效,當(dāng)裝載圖像文件失敗時(shí),fileIsValid為false,否則為true。這樣在調(diào)用ShowSplash時(shí)將什么都不做,返回false。這時(shí),用戶應(yīng)檢查圖像文件是否存在,文件名稱拼寫是否正確。
5. 用法
√ 將CSplashWnd類加入項(xiàng)目中
√ 在使用CSplashWnd類的文件中#include “Splash.h”
√ 在合適的位置定義一個(gè)CSplashWnd對(duì)象
√ 在想顯示啟動(dòng)畫面的地方調(diào)用ShowSplash顯示于屏幕上
√ 如果想在啟動(dòng)畫面上顯示一些初始化或其他提示信息,調(diào)用ShowText。
√ 在你想關(guān)閉啟動(dòng)畫面的地方
在你的App類InitInstance函數(shù)中,顯示主窗口之前使用,進(jìn)行上述步驟,這是最典型的用法,如下面代碼所示。
BOOL CTsApp::InitInstance() {AfxEnableControlContainer();#ifdef _AFXDLLEnable3dControls(); // Call this when using MFC in a shared DLL #elseEnable3dControlsStatic(); // Call this when linking to MFC statically #endifSetRegistryKey(_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 windowRUNTIME_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 neededpCsw->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
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 fileHINSTANCE hInst = AfxGetResourceHandle();HRSRC hRsrc = ::FindResource(hInst,MAKEINTRESOURCE(nIDRes),"IMAGE"); // typeif (!hRsrc)return FALSE;// load resource into memoryDWORD len = SizeofResource(hInst, hRsrc);BYTE* lpRsrc = (BYTE*)LoadResource(hInst, hRsrc);if (!lpRsrc)return FALSE;// create memory file and load itCMemFile 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 unitsm_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 unitsGetHIMETRICSize(hmWidth, hmHeight);m_spIPicture->Render(*pDC, rc.left, rc.top, rc.Width(), rc.Height(),0, hmHeight, hmWidth, -hmHeight, prcMFBounds);return TRUE; }參考連接:
總結(jié)
以上是生活随笔為你收集整理的VC启动窗口画面制作方法研究的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 【6 插值方法】实例实战篇
- 下一篇: 人工智能的研究领域