The splashscreen principle is added to the VC ++ project to prepare the display interface of the software.

Source: Internet
Author: User
Splashscreen-we can see software like word in the short start time. It is usually used in Program The program, user name, and copyright information are displayed at startup. I don't know what the exact name is (is it a pop-up screen ?), This is what we call it. You may also want to add this feature to your project. This article will gradually analyze its implementation principles by creating an actual project.

Note: Unless otherwise specified, in this article, the base class names such as cwinapp, cmainframe, and cdialog will be used to describe the corresponding derived class names in the actual project.
Visual c ++ is a powerful C ++ development tool embedded with support for splashscreen. However, in an mfc exe project, only this support is provided for SDI or MDI projects with the main framework class, and projects based on the dialog box class are excluded. Let's start now. The first step is to add the splashscreen to the SDI project.
First, use Appwizard to generate an SDI project, except that the docking toolbar must be selected. (I think this is a bug in MFC. Of course, this is irrelevant to the splashscreen discussed in this article ), other document-view support, Status Bar, and so on can be avoided, this can minimize uselessCode.
From the menu project> Add to project> components and controls in IDE, we can select the splash screen component from Visual C ++ components to insert the project.

After clicking "insert", a dialog box as shown in is displayed, which sets the class name for inserting the splashscreen in the project, the ID and file name of the displayed bitmap, and uses the default value.

The above steps generate the splash. cpp and splash. H files under the project directory. This is the implementation file and header file of the csplashwnd class. At the same time, some codes in the cwinapp and cmainframe classes in the project will be modified to process messages in the csplashwnd window.
Next let's take a look at the csplashwnd class declaration and the main code (which has been deleted ): // Class Declaration

Class Csplashwnd: Public Cwnd
{
Csplashwnd ();
~ Csplashwnd ();
Virtual   Void Postncdestroy ();

Static   Void Enablesplashscreen (bool benable = True );
Static   Void Showsplashscreen (cwnd * Pparentwnd = Null );
Static Bool pretranslateappmessage (msg * PMSG );
Bool create (cwnd * Pparentwnd = Null );
Void Hidesplashscreen ();

Afx_msg Int Oncreate (maid );
Afx_msg Void Onpaint ();
Afx_msg Void Ontimer (uint nidevent );

Cbitmap m_bitmap; // Bitmap objects used for display in the splashscreen window
Static Bool c_bshowsplashwnd; // Whether to display the number of splashscreen flags
Static Csplashwnd * C_psplashwnd;
} ;
// Whether to use splashscreen void csplashwnd: enablesplashscreen (bool benable)
{
C_bshowsplashwnd=Benable;
}  

// Create a csplashwnd object and call create () to create a window void csplashwnd: showsplashscreen (cwnd * pparentwnd)
{
// If you do not want to display whether the splashscreen or splashwnd object has been created, return
If ( ! C_bshowsplashwnd | C_psplashwnd ! = Null)
Return ;

C_psplashwnd =   New Csplashwnd;

If ( ! C_psplashwnd -> Create (pparentwnd ))
Delete c_psplashwnd;
Else  
C_psplashwnd -> Updatewindow ();
}  

// Load the splashscreen to display the bitmap. Use createex () to activate oncreate () to create the window and set bool csplashwnd: Create (cwnd * pparentwnd)
{
If ( ! M_bitmap.loadbitmap (idb_splash ))
Return False;

Bitmap bm;
M_bitmap.getbitmap ( & BM );

Return Createex ( 0 , Afxregisterwndclass ( 0 , Afxgetapp () -> Loadstandardcursor (idc_arrow), null, ws_popup | Ws_visible, 0 , 0 , BM. bmwidth, BM. bmheight, pparentwnd -> Getsafehwnd (), null );
}  

// Destroy window, refresh the framework void csplashwnd: hidesplashscreen ()
{
Destroywindow ();
Afxgetmainwnd ()->Updatewindow ();
}  

// Create a window using the window structure and set the timer to trigger ontimer () int csplashwnd: oncreate (lpcreatestruct) after Ms)
{
If (Cwnd: oncreate (lpcreatestruct) =   - 1 )
Return   - 1 ;

Centerwindow (); // Center window display
Settimer ( 1 , 750 , Null ); // Set timer

Return   0 ;
}  

// Pass the keyboard and mouse messages to the csplashwnd object to destroy the window bool csplashwnd: pretranslateappmessage (MSG * PMSG)
{
If (C_psplashwnd = Null)
Return False;

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;
}  
Return False;
}  

Void Csplashwnd: ontimer (uint nidevent)
{
Hidesplashscreen ();
}  

Let's take a look at the changes in the cwinapp and cmainframe classes:
(1) Call csplashwnd: enablesplashscreen () in cwinapp: initinstance () to set c_bshowsplashwnd;
Call csplashwnd: pretranslateappmessage () in pretranslatemessage () to pass the keyboard and mouse messages to the csplashwnd object, and then call csplashwnd: hidesplashscreen () to destroy the splashscreen window itself.
(2) Call csplashwnd: showsplashscreen () in oncreate () of the cmainframe object to create a static splashscreen window object c_psplashwnd and set its parent window to cmainframe. In this process, csplashwnd sets a timer by creating itself, and then the timer calls hidesplashscreen () to destroy itself when triggered in the first cycle.
(3) While the window creation message of the cmainframe object is passed by the cwinapp object in initinstance ()
M_pmainwnd-> showwindow () call.
The entire process can be expressed. The basic principle is that cmainframe is used to create csplashwnd, and then the timer message of csplashwnd is triggered by its own timer to destroy the window. Therefore, csplashwnd has nothing to do with SDI or MDI.

Step 2: Let's take a look at how to add splashscreen to a dialog box-based project.
By analyzing the splashscreen principle added to the above SDI project, I think everyone will also think about how to add this feature to the dialog box-based project. In essence, the cdialog class completes the cmainframe class work in the SDI project. The implementation steps are as follows:
(1) Use classwizard to add the oncreate () function for processing the wm_create message for cmydialog. (here, cmydialog is used to distinguish it from the base class name cdialog in the function .) Int cmydialog: oncreate (maid)
{
If (cdialog: oncreate (lpcreatestruct) =-1)
Return-1;
Csplashwnd: showsplashscreen (this );

Return 0;
}
(2) Use classwizard to add the message forwarding function pretranslatemessage () for the cwinapp; bool cwinapp: pretranslatemessage (MSG * PMSG)
{
If (csplashwnd: pretranslateappmessage (PMSG ))
Return true;

Return cwinapp: pretranslatemessage (PMSG );
}

(3) Add the following call to csplashwnd: enablesplashscreen (true) in cwinapp: initinstance ();
(4) of course, you also need to add the splash generated in the previous SDI project. CPP and splash. copy the H file to the current project directory, and use project-> Add to project-> files to introduce the two files to the project. In addition, # include "Splash. H" must be included in the implementation files of cwinapp and cmainframe ".
(5) Add a bitmap with ID idb_splash in the resource manager. Because the VC ++ IDE can only display bitmaps of less than 256 colors, if you want to display a real-color bitmap, Please import a pre-made Bitmap Using the import method. Of course, VC ++ will prompt that the bitmap has been successfully imported, but it cannot be displayed in the IDE bitmap editor, and will be displayed when the program is running. If you want to display user name and other information like word, you can add your own code to modify the bitmap after loading the bitmap in csplashwnd: Create.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.