Research on VC Start Window Picture Production Method
The source code running renderings are as follows:
1 Overview
When designing software a few days ago, I chose VC as a development tool, I want to start the screen, because I haven't made it before, so I have a pass online. There are several related articles on the Internet, there are two articles I feel very valuable: an article about the CPICTure class created by the image, which is a solution written by Paul Dilascia, and there is an article about Making a true color startup screen, but it limits the operation of the bitmap without supporting JPG, GIF, and is cumbersome, basically a simple modification of the code imported from the Splash Screen component. I have learned to use it for a while.
It is difficult to use the existing materials that are inconvenient, and the integration and re-encapsulation are made, and the CSPLASHWND class is designed, which is very easy to use. Let's introduce the class of my design to everyone. If you have any improper or wrong, please refer to it. My email: zhengxiuliu@sohu.com
2. CSPLASHWND function
It is possible to display the true color boot screen to display the initialization text information on the screen, support JPG, GIF, BMP image files.
3. Design of CSPlashWnd
3.1 Users careful interface
The public interface used by the user:
PUBLIC:
CSPLASHWND (LPCTSTR LPSZFILENAME); / / Specify an image file as a startup screen, and loading
Bool showsplash (); // Display screen
Void closeSplash (); // Close screen
Void ShowText (LPCTSTR PCH); display initialization information text at the intermediate position on the displayed image
3.2 Open Interfaces used by other interface systems: (users don't care)
~ Csplashwnd ()
Void Postncdestroy ();
Private interface: (users don't care)
Bool create (CWND * pParentWnd = NULL);
Int Oncreate (lpcreatestruct lpcreatestruct);
Void onpaint ();
3.3 Data design (users do not care)
Bool fileiSvalid // instruction
CPICTURE PIC; // Used to operate the image file
Int width, Height;
3.4 Restriction √ Do not allow inheritance. √ To simplify the interface, only provide image from the file
3.5 needed header files
STDAFX.H, VC 6.0 automatically generates support for MFC, different engineering options produce different stdafx.h.
Afxwin.h supports CRECT class
ATLBASE.H provides support for iPicture (COM classes).
AfxPriv2.h provides support for the CarchiveStream class.
4. Category robustness and adjustability design
Is the image file valid?
You need to check if the file is valid. When the loading image file fails, FileSvalid is false, otherwise true. This will not do anything when calling Showsplash, returning false. At this time, the user should check if the image file exists, and the file name spell is correct.
5. Usage
√ Add the CSPLASHWND class to the project
√ 在 in the file using the CSPLASHWND class #include "splash.h"
√ Define a csplashwnd object in the appropriate location
√ When you want to display the startup screen, call showSplash on the screen.
√ 如果 If you want to display some initialization or other prompt information on the startup screen, call ShowText.
√ 在 Play on the startup screen you want to close
In your App Class InitInstance function, use the main window before using the above steps, which is the most typical usage, as shown in the following code. Bool ctsapp :: initInstance ()
{
AFXENABLECONTROLCONTAINER ();
#ifdef _AFXDLL
Enable3dControls (); // Call thisime by the sale mfc in a shared dll
#ELSE
Enable3dControlsStatic (); // call thisime1 linking to mfc staticly
#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));
AddDDOCTemplate (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;
m_pmainwnd-> UpdateWindow ();
Return True;
}
6. Class code 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 overce
///
// Picture Object-Encapsulates iPicture
// Written by Paul Dilascia.
// used to display pictures
//
// Declare CPICTURE CLASS
//
Class CPICTURE {
PUBLIC:
Bool Render (CDC * PDC, CREC, 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
Assert (succeeded (m_hr));
Const_cast
Assert (succeeded (m_hr));
}
Void free () {
IF (m_spipicture) {
m_spipicture.release ();
}
}
protected:
CCOMQIPTR
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
#include
#include
#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
// conncture
// 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) ;;
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 //patrient
//
//
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 is written by Paul Dilascia.
// if not, i do' '''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 :: moderad | cfile :: sharednywrite))
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 what 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.himetrikeTodP (& SZ); // Convert to Pixels
} else {
PDC-> Himetrictodp (& SZ);
}
Return SZ;
}
//
// Render to Device Context. Cover to himetric for iPicture.
//
BOOL CPICTURE :: Render (CDC * PDC, CREC, LPCRECT Prcmfbounds) Const
{
ASSERT (PDC);
IF (rc.isectnull ()) {
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;
}
Reference connection: