Realization of special effect of flash screen graphics

Source: Internet
Author: User
Tags rand sleep

In the actual development of the program, splash screen is often used by us. If the program initialization takes some time, it is more necessary to design a splash screen, not only to make your program beautiful, but also to write your version information on it. The benefits are plenty. There is a splash screen splash component in the vc++6.0 component library that can be added to the program. But when beginners open the source code to learn, the inside function is too many, for a while also do not understand. And the splash screen displayed is very stiff. In order for beginners to understand, I have a very simple class, there are only two functions, the structure is very clear. I can see it, and I have a detailed note. The function is not inferior to the vc++6.0 itself.

Splash screen is actually a window, its base class is CWnd, and the general window is not any different, but this window at the beginning of the show just. Use the Class Wizard to generate a new class whose base class is CWnd. Add the following variable to the new class.

CDC MemDC;      //创建内存DC
BITMAP bm;      //创建位图结构变量
CBitmap m_bitmap;   //创建位图对象
CBitmap *old_bitmap; //创建位图对象指针

Import a bitmap in the resource, color can be more than 256 colors, but in VC can not edit more than 256 of the bitmap. Initialized in the constructor.

m_bitmap.LoadBitmap(IDB_BITMAP1);//拷贝资源位图
m_bitmap.GetBitmap(&bm);//得到位图结构中的大小信息

and #include "resource.h" in the header file of the class, otherwise the Idb_bitmap1 undefined error occurs at compile time. Then add a function Creatljxwnd () to the class to generate the window. Add the following code inside:

CreateEx(0,
  AfxRegisterWndClass(0,AfxGetApp()->LoadStandardCursor(IDC_ARROW)),//注册类
            "animatesplash",//窗口标题
            WS_POPUP,//窗口为弹出式
      0,0,bm.bmWidth,bm.bmHeight, //建立大小与位图大小相同的窗口
            NULL,
            NULL,
            NULL );

Clatter we can display the bitmap in the window. Stunts show a lot, like fade in, translucent, and so on. If you want to get more special effects algorithm, you can refer to the Transformation Programming community website (www.5xsoft.com) has a "Visual C + + graphics" and "proficient in VC + + image programming," The algorithm inside them is written in detail. We use random block method here. The principle is: the memory device situation object (such as MEMDC) in the bitmap data divided into 10 equal portions of 100 sets of data, then randomly remove one of these 100 sets of data to display to the corresponding position of the bitmap to be displayed in the target device (such as CLIENTDC) until all 100 sets of data are displayed. We add WM_PAINT message, VC + + automatically generate response to this message of the corresponding function, stunt display code also executed in this function. To add code to a function:

Memdc.createcompatibledc (NULL);//Set up a DC-compatible memory DC to place the bitmap
Old_bitmap=memdc.selectobject (&m_bitmap); /select the created bitmap into memory DC
//random block image display special algorithm
int i,j,stepx,stepy,dispnum,x,y;
   int ljx[20][20]; Array records the data groups that have been displayed
for (i=0; i<20; i++)
for (j=0; j<20; j + +)
Ljx[i][j]=0;
STEPX=BM.BMWIDTH/20;
Stepy=bm.bmheight/20;
Srand ((unsigned) time (NULL));  
Dispnum=0//Record the number of data groups that have been displayed
while (1) {
X=rand ()%
Y=rand ()% 20; The
if (Ljx[x][y])//If 1 is already displayed, out of the loop.
Continue;
Ljx[x][y]=1;//display, set to 1
DC. StretchBlt (X*STEPX,//target device logical horizontal axis
Y*stepy,//target device logical ordinate
stepx,//display bitmap pixel width
stepy,//display bit The pixel height of the graph
&MEMDC,//Bitmap Memory Device Object
X*stepx,//bitmap start horizontal
Y*stepy,//bitmap start ordinate
stepx  ,//Bitmap pixel width
stepy,//bitmap pixel height
srccopy;
dispnum++;  
if (dispnum >=400)
break;
Sleep (10);
}
Memdc.selectobject (old_bitmap);

By this, our class is done. Now is the time to show it before the dialog box application is displayed. Next we will instantiate this class and then show it again. We found the application's initialization function InitInstance (), adding the following code inside:

BOOL canimatesplashapp::initinstance ()
{
AfxEnableControlContainer ();
#ifdef _afxdll
Enable3dcontrols (); Call the When using MFC in a shared DLL
#else
Enable3dcontrolsstatic (); Linking to MFC statically
#endif
Added code
Cljxwnd *ljxljx=new Cljxwnd; Create a new Window object
Ljxljx->creatljxwnd (); Create a window
Ljxljx->centerwindow (); In the center of the screen
Ljxljx->showwindow (Sw_show); Display window
Ljxljx->updatewindow (); Update window, activate onpait function
Sleep (2000); Wait function specifies seconds
if (ljxljx!=null) ljxljx->sendmessage (wm_close); Close window
End of code
Canimatesplashdlg Dlg;
m_pMainWnd = &dlg;
int nresponse = dlg. DoModal ();
if (Nresponse = = Idok)
{
Todo:place code here to handle when the dialog is
Dismissed with OK
}
else if (nresponse = = IDCANCEL)
{
Todo:place code here to handle when the dialog is
Dismissed with Cancel
}
Since the dialog has been closed, return FALSE so we exit the
Application, rather than start the application ' s message pump.
return FALSE;
}

Here is completed, compile the implementation to see the effect of it. You can also make improvements to the code. Is that it's more powerful.

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.