Author information: Luo Shupeng Http://www.cnblogs.com/luoshupeng
As the author took some detours in the course of practice, I recorded these cases, hoping to provide some experience for the latter.
In the VC6.0 era, you can add SplashScreen to your project through components by using the menu Project->add to Project->componentsand Controls in the IDE, from the visual c+ + Components Select Splash Screen This component to insert the project.
But into the VC. NET era, this method is not, need the program author to join the SplashScreen class, their own processing some system messages. The following author has recorded the practice process, and pointed out the need to pay attention to the place.
First, create a new SplashScreen class, and declare members and methods
The new base class is the Csplashwnd class for CWnd (of course, the class name is free to write) and declares the following members:
CBitmap M_bitmap; Load SplashScreen pictures with
Static Csplashwnd*c_psplashwnd; A handle to the Csplashwnd class that can be used to determine whether Csplashwnd has been created or destroyed
Static Boolc_bshowsplashwnd; Identifies whether the display is displayed for static members that need to be initialized outside the class
Class members are generally declared as protection types (protected). Next, you declare several static methods to handle these members:
Static Boolc_bshowsplashwnd; Identifies whether the display is displayed for static members that need to be initialized outside the class
static void ShowSplashScreen (cwnd* pparentwnd = NULL); Used to display the SplashScreen window
Static Boolpretranslateappmessage (msg* PMSG); To handle some messages note that this is not the PreTranslateMessage method inherited from the CWnd class
These methods must be declared as public types, because these methods are called externally. Next, we declare two custom methods:
BOOL Create (Cwnd*pparentwnd = NULL); Create window
void Hidesplashscreen (void); Hide Window
I declare these methods as protection types (protected). Next, we will declare some functions for handling system messages:
virtual void PostNcDestroy ();
afx_msg int OnCreate (lpcreatestruct lpcreatestruct);
afx_msg void OnTimer (Uint_ptr nidevent);
afx_msg void OnPaint ();
At this point, the class has been designed. The complete header file is as follows:
#pragma once
#include "AFXWIN.h"
Csplashwnd
Class Csplashwnd:public CWnd
{
Declare_dynamic (Csplashwnd)
Protected
Csplashwnd ();
Public
Virtual~csplashwnd ();
Protected
CBitmap M_bitmap; Load SplashScreen pictures with
staticcsplashwnd* C_psplashwnd; A handle to the Csplashwnd class that can be used to determine whether Csplashwnd has been created or destroyed
Staticbool C_bshowsplashwnd; Identifies whether the display is displayed for static members that need to be initialized outside the class
Public
static void Enablesplashscreen (BOOL benable = TRUE); Used to set C_bshowsplashwnd
static void ShowSplashScreen (cwnd* pparentwnd = NULL); Used to display the SplashScreen window
Staticbool pretranslateappmessage (msg* pMsg); To handle some messages note that this is not the PreTranslateMessage method inherited from the CWnd class
Protected
Virtualvoid PostNcDestroy ();
afx_msg intoncreate (lpcreatestruct lpcreatestruct);
afx_msg Voidontimer (uint_ptr nidevent);
Afx_msg Voidonpaint ();
BOOL Create (cwnd* pparentwnd = NULL); Create window
Voidhidesplashscreen (void); Hide Window
Declare_message_map ()
};
The realization of the Csplashwnd class method
The point to note here is that you must set the C_psplashwnd member to a null type in the destructor, or the program will receive an exception when it receives the message. There's nothing else, just look at the code.
Splash.cpp:implementation file
//
#include "stdafx.h"
#include "Splash.h"
Csplashwnd*csplashwnd::c_psplashwnd;
Boolcsplashwnd::c_bshowsplashwnd;
Csplashwnd
Implement_dynamic (Csplashwnd,cwnd)
Csplashwnd::csplashwnd ()
{
}
Csplashwnd::~csplashwnd ()
{
ASSERT (C_psplashwnd = = this);
C_psplashwnd = NULL;
}
Begin_message_map (Csplashwnd,cwnd)
On_wm_create ()
On_wm_timer ()
On_wm_paint ()
End_message_map ()
Csplashwnd message handlers
Voidcsplashwnd::enablesplashscreen (BOOL benable/* =true*/)
{
C_bshowsplashwnd = benable;
}
Voidcsplashwnd::showsplashscreen (cwnd* pparentwnd/* =null*/)
{
If you do not show splashscreen or Splashwnd objects have been created then return
if (!c_bshowsplashwnd | | c_psplashwnd!=null)
{
Return
}
C_psplashwnd = Newcsplashwnd;
if (!c_psplashwnd->create (pParentWnd))
{
Deletec_psplashwnd;
}
Else
{
C_psplashwnd->updatewindow ();
}
}
Boolcsplashwnd::P retranslateappmessage (msg* pMsg)
{
if (C_psplashwnd = = NULL)
Returnfalse;
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;
}
Returnfalse;
}
void Csplashwnd::P Ostncdestroy ()
{
Delete this;
}
Intcsplashwnd::oncreate (lpcreatestruct lpcreatestruct)
{
if (cwnd::oncreate (lpcreatestruct) = =-1)
return-1;
Todo:add your specialized creation code here
CenterWindow ();
SetTimer (1,3000,null);
Return0;
}
Voidcsplashwnd::ontimer (Uint_ptr nidevent)
{
Todo:addyour Message Handler code here and/or call default
if (nidevent = = 1)
{
Hidesplashscreen ();
}
/*cwnd::ontimer (nidevent); * *
}
Voidcsplashwnd::onpaint ()
{
CPAINTDC DC (this);//device context for painting
Todo:addyour Message Handler code here
Do Notcall cwnd::onpaint () for painting messages
CDC dcimg;
if (!dcimg.createcompatibledc (&DC))
{
Return
}
BITMAP BM;
M_bitmap. Getbitmap (&BM);
Paint Theimage
cbitmap* poldbit =dcimg.selectobject (&M_BITMAP);
dc. BitBlt (0,0,bm.bmwidth,bm.bmheight,&dcimg,0,0,srccopy);
Dcimg.selectobject (Poldbit);
}
Boolcsplashwnd::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);
}
void Csplashwnd::hidesplashscreen (void)
{
DestroyWindow ();
AfxGetMainWnd ()->updatewindow ();
}
Third, use the Csplashwnd class in SDI or MDI
(1) Call Csplashwnd::enablesplashscreen () in CWinApp::InitInstance () to set the C_bshowsplashwnd;
Call Csplashwnd::P retranslateappmessage () in PreTranslateMessage () to pass keyboard and mouse messages to the Csplashwnd object
(2) (2) Call Csplashwnd::showsplashscreen () in OnCreate () of the CMainFrame object to create a static SplashScreen window object C_psplashwnd, and sets its parent window to CMainFrame.
The code is as follows:
Boolcdrawapp::initinstance ()
{
Used to handle whether the SplashScreen is displayed
{
CCommandLineInfo Cmdinfo;
ProcessShellCommand (Cmdinfo);
Csplashwnd::enablesplashscreen (Cmdinfo.m_bshowsplash);
}
...........
}
Boolcdrawapp::P retranslatemessage (msg* pMsg)
{
if (Csplashwnd::P retranslateappmessage (PMSG))
Returntrue;
Returncwinappex::P retranslatemessage (PMSG);
}
Intcmainframe::oncreate (lpcreatestruct lpcreatestruct)
{
if (cmdiframewndex::oncreate (lpcreatestruct) = =-1)
return-1;
...........
Csplashwnd::showsplashscreen (this);
Return0;
}
There are no errors during this process, but you will be prompted at compile time: Idb_splash is not defined. This requires the inclusion of a single-bit graph resource in the resource and the "resource.h" header file in the CSlashWnd.cpp file.
Iv. using the Csplashwnd Class in a dialog box
Used in dialog boxes and used in SDI or MDI basic similarity, first in CWinApp's inheriting class to handle the InitInstance () and PreTranslateMessage (msg* PMSG) Two message functions, and then in the dialog box class Wm_ The Create response function displays SplashScreen.
Boolcdlgapp::initinstance ()
{
Used to handle whether the SplashScreen is displayed
{
CCommandLineInfo Cmdinfo;
ProcessShellCommand (Cmdinfo);
Csplashwnd::enablesplashscreen (Cmdinfo.m_bshowsplash);
}
..........
}
Boolcdlgapp::P retranslatemessage (msg* pMsg)
{
if (Csplashwnd::P retranslateappmessage (PMSG))
{
Returntrue;
}
Returncwinapp::P retranslatemessage (PMSG);
}
Intcdlgdlg::oncreate (lpcreatestruct lpcreatestruct)
{
if (cdialogex::oncreate (lpcreatestruct) = =-1)
return-1;
Todo:add your specialized creation code here
Csplashwnd::showsplashscreen (this);
Return0;
}
Add SplashScreen in "Go" vs2010