An implementation method of initialization interface in VC application

zhaozj2021-02-11  243

An implementation method of initialization interface in VC application

Key Words: Spash Screen Non-Mode dialog

Level: Primer

Photoshop When you start, the welcome screen will dynamically display some of the information initialization when you initialize, and then enter the work interface. If our own developed program can implement this function, you will have a lot of color.

Vc provides us with a small stadete: Visual C Components-> Splash Screen, you can implement the function of the program startup interface, give a frame, some code still write it, feel some bondage, I don't like it. Just now develop a database project in hand, I use my own way to implement Splash Screen, and some network detection, database coupling, and some other initialization operations are made when the program starts. As shown below:

Figure 1: Joint Remote Database

Figure 2: Some license information has been completed, this information is in the database]

Figure 3: Joint failure

My method is briefly introduced as follows:

Ctrl 1 Add a form as our Splash Window:

IDD_SPLASH DIALOG DISCARDABLE 0, 0, 188, 162

STYLE WS_POPUP | WS_BORDER

Font 9, "Song"

Begin

End

Add a bitmap resource IDB_SPLASH, I deliberately leave a white in this picture to display Chinese characters. The window size does not have to be adjusted, the program is automatically adjusted when the program starts, the code is as follows:

Bool csplashdlg :: oninitdialog ()

{

CDIALOG :: OnInitdialog ();

// Todo: Add Extra Initialization Here

Settimer (1,2000, null); // This timer will be said

m_bitmap.loadbitmap (idb_splash);

Bitmap BMP;

m_bitmap.getObject (sizeof (bitmap), & bmp);

CRECT RTOLD;

GetWindowRect (RTOLD);

Cpoint Point = rtold.topleft ();

CRECT RTNEW = CRECT (Point, CSIZE (bmp.bmwidth, bmp.bmheight);

MoveWindow (RTNEW);

:: setwindowpos (GetSafehWnd (), HWND_TOPMOST, RTNEW.LEFT, RTNEW.TOP, RTNEW.WIDTH (), RTNew.Height (), SWP_NOSize;

// onpaint ();

Return True; // Return True UnsS you set the focus to a control

// Exception: OCX Property Pages SHOULD RETURN FALSE

}

The display of the window is in the form of a non-mode dialog box, to implement the destruction of the Non-Mode dialog, add the processing of WM_DESTROY within the CSPlashDLG class:

Void csplashdlg :: ONDESTROY ()

{

CDIALOG :: ONDESTROY ();

// Todo: Add your message Handler Code Here

Delete this; // SO important

}

as well as:

Void csplashdlg :: onok ()

{

// Todo: add extra validation heredientog :: onok ();

DESTROYWINDOW ();

}

Void csplashdlg :: oncancel ()

{

// Todo: Add extra cleanup here

CDIALOG :: oncancel ();

DESTROYWINDOW ();

}

In this way, we can perform the window display in the INITINSTANCE of the app class:

/ / SPLASH Screen

CSPLASHDLG * SPLASHDLG = New CSPLASHDLG;

Splashdlg-> Create (IDD_SPLASH, NULL);

Splashdlg-> showWindow; SW_SHOW

Splashdlg-> UpdateWindow ();

But how do you implement the information in a dynamic display window?

By calling a member function of the CSPLASHDLG class [of course, it is Public] implementation:

Void CSPlashDlg :: setInfotext (lpctstr sztext)

{

// invalidate ();

CDC * PDC = getWindowDC ();

CRECT RT;

GetWindowRect (RT);

ScreenToClient (RT);

Cfont * pfont = getFont ();

CFont * PoldFont = PDC-> SelectObject (PFont);

PDC-> Textout (RT.LEFT 10, RT.BOTTOM - 20, SZTEXT);

IF (PoldFont)

PDC-> SELECTOBJECT (POLDFONT);

}

This way we can:

CSPLASHDLG * SPLASHDLG = New CSPLASHDLG;

Splashdlg-> Create (IDD_SPLASH, NULL);

Splashdlg-> showWindow; SW_SHOW

Splashdlg-> UpdateWindow ();

Splashdlg-> setInfotext (_t ("initialization system ..."));

/ * Initialize the COM object, create a smart pointer instance, connect the database * /

IF (! Afxoleinit ())

{

AFXMessageBox ("OLE initialization error");

Return False;

}

Splashdlg-> setInfotext (_T ("Join Remote Database Server ..."));

CString stropen = "provider = sqloledb; network library = dbmssocn; data source = 127.0.0.1; initial catalog = xxxx; user ID = sa; password = x";

Try

{

M_PCONNECTION.CREATEINSTANCE (__ uuidof (connection));

M_PConnection-> Open (const char *) stropen, "", ", - 1);

M_PCONNECTION-> CURSORLOCATION = aduseclient;

}

Catch (...)

{

Splashdlg-> setInfotext (_T ("Dip Remote Database Server Failure"));

AfxMessageBox ("The join server fails, the program will turn off !!!");

Return False;

}

SPLASHDLG-> setInfotext (_t ("Check this machine usage permission ...")); (omit some test code)

Splashdlg-> setInfotext ("Welcome to") PCINFO);

Splashdlg-> m_binitfinished = true;

You may ask splashdlg-> m_binitfinished = true; what is doing, now Splash has completed it, but how can it destroy it?

Use DESTROYWINDOW directly, there is a shortcoming, that is, when your database is connected, the initialization action is fast, such as 0.5 seconds, then your spash will flash, this is certainly not what you want. I use a data member variable in a cspashdlg class to inform the Splash window a series of work. If the Splash window feels that initialization is too fast, it will be more than a while, then destroy yourself, otherwise it will be destroyed, the article is the first One of the code in the code: SetTimer (1, 2000, null); it is the key to the "technology".

Void csplashdlg :: ONTIMER (uint nidevent)

{

// Todo: add your message handler code here and / or call default

IF (nidevent == 1 && m_binitfinished)

{

Killtimer (1);

ONOK ();

}

// cdialog :: ONTIMER (Nidevent);

}

Such a Splash box is at least 2 seconds.

[Finish]

I hope this article can help you.

9CBS roast chicken wings

2002-11-29

转载请注明原文地址:https://www.9cbs.com/read-5756.html

New Post(0)