Graphic stunt in Visual C ++

Source: Internet
Author: User
Article Title:Graphic stunt in Visual C ++
Original Author: Zhao Minzhi
Original location: Unknown
Released by: loose_went
Release type: Reprinted
Released on: 2004-01-08
Browse today: 1
Overview: 2324
With the Computer Information Representation and multi-media implementation, various graphic display techniques are often used in many learning software, game software, and multimedia courseware production software, display modes such as push-pull, staggered, raindrops, hundreds of pages, and random stacking of building blocks. This makes the screen more lively, more attractive to users, and lays the foundation for better play to the functions of the software. This article discusses the principles and methods of various display techniques for graphics in Visual C ++ 6.0.

Basic Principles

In Visual C ++ 6.0, the bitmap display method and process are as follows:

1. display the bitmap in the program resource (all the data in the bitmap exists in the executable file)

(1) Load bitmap from resources

● Define the cbitmap m_bitmap Member of the bitmap object;

● Call the cbitmap member function loadbitmap (), for example, m_bitmap.loadbitmap (idb_bitmap1 );

● The input loadbitmap parameter is the identifier assigned when a bitmap is generated in the graphic editor or imported from a bitmap file.

(2) generate the memory device context object associated with the bitmap

CDC memdc;

Memdc. createcompatibledc (null );

Memdc. SelectObject (& m_bitmap );

(3) display bitmap

Cclientdc clientdc (this );

Bitmap bm;

M_bitmap.getobject (sizeof (BM), & BM );

Clientdc. bitblt

(X, y, // logical horizontal and vertical coordinates of the target device

BM. bmwidth, BM. bmheight, // display the pixel width and height of the bitmap.

& Memdc,

// The device context object of the bitmap data to be displayed

0, 0, // horizontal and vertical coordinates in the source data

Srccopy); // bit Operation Method

This method shows that bitmap is fast, but not flexible, and can increase the size of executable files.

2. display the bitmap of an independent file (all the data in the bitmap is independent of the executable file)

Hbitmap * hbitmap; // defines the bitmap object handle.

Bitmap bm;

CDC memdc;

Cclientdc clientdc (this );

Memdc. createcompatibledc (& clientdc );

Hbitmap = (hbitmap *): LoadImage

(AfxGetInstanceHandle (),

// Obtain the application handle

Includemo1.bmp ",

// Bitmap file name

Image_bitmap,

// Windows bitmap

0, 0,

Lr_loadfromfile );

// Retrieve bitmap data from the file

Memdc. SelectObject (hbitmap );

: GetObject (hbitmap, sizeof (BM), & BM );

Clientdc. bitblt (......)

// The format and method are used together.

The bitmap display speed is a little slower than the previous one, but it is more flexible. Bitmap files can be changed at will without re-compiling the source program, which also reduces the size of executable files.

Implementation Method

The following describes the implementation principles and methods of various graphic display techniques. All of the following program algorithms can be implemented in the visual class (cview, or other classes as needed), and the following operations are necessary:

Add the following class member variables:

Bitmap m_bm;

// Save the width and height of the bitmap.

Hbitmap * m_hbitmap;

// Save the bitmap data handle

CDC m_memdc; // memory device context object

Add the following code to the class constructor:

M_memdc.createcompatibledc (null); // generates a memory device context object.

M_hbitmap = (hbitmap *): LoadImage (

// Load bitmap data from the file

AfxGetInstanceHandle (),

Includemo1.bmp ",

Image_bitmap,

0, 0,

Lr_loadfromfile );

M_memdc.selectobject (m_hbitmap); // select the bitmap into the memory device context object.

: GetObject (m_hbitmap, sizeof (m_bm), & m_bm );

1. Horizontal staggered effect

Principle: splits bitmap data in a memory device context object (such as memdc) into odd and even scan lines. The odd scan lines move from top to bottom, and the even scan lines move from bottom to top, and both are performed at the same time. The effect on the screen is gradually approaching each other from the light barrier image on both sides of the screen until the entire bitmap is completely clear. The implementation principle of vertical staggered effects is similar.

Program Algorithm:

Int I, J;

For (I = 0; I <= m_bm.bmheight; I + = 2)

{J = I;

While (j> 0)

{Clientdc. stretchblt (

// An odd number, from top to bottom

0, J-1,

// The logical horizontal and vertical coordinates of the target device

M_bm.bmwidth, 1,

// Display the pixel width and height of a bitmap

& M_memdc,

// Source bitmap device context object

0, m_bm.bmheight-(i-j-1 ),

// Start horizontal and vertical coordinates of the source bitmap

M_bm.bmwidth, 1,

// The pixel width and height of the source bitmap

Srccopy );

Clientdc. stretchblt (

// Even number, from bottom to top

0, m_Bm.bmHeight-j,

// The logical horizontal and vertical coordinates of the target device

M_bm.bmwidth, 1,

// Display the pixel width and height of a bitmap

& M_memdc,

// Source bitmap device context object

0, I-j,

// Start horizontal and vertical coordinates of the source bitmap

M_bm.bmwidth, 1,

// The pixel width and height of the source bitmap

Srccopy );

J-= 2 ;}

// While (j> 0)

Sleep (10 );

}

// For (I = 0; I <= m_bm.bmheight; I + = 2)

2. Raindrops Effect

Principle: sequentially scans the last line of bitmap data from the memory device context object (such as memdc) from the target device (such as clientdc) the position of the first scanned line of the bitmap to be displayed is moved to the last line, and the trajectory left when the scanning line moves on the screen is retained. Then, the last and second scanning lines of the bitmap data in memdc are sequentially moved from the position of the first scanning line of the bitmap to be displayed in the target device (such as clientdc) to the last and second scanning lines. Other scanning lines are pushed accordingly.

Program Algorithm:

Int I, J;

For (I = 0; I <= m_bm.bmheight; I ++)

{For (j = 0; j <= m_Bm.bmHeight-i; j ++)

Clientdc. stretchblt (

0, J,

// The logical horizontal and vertical coordinates of the target device

M_bm.bmwidth, 1,

// Display the pixel width and height of a bitmap

& M_memdc,

// Source bitmap device context object

0, m_Bm.bmHeight-i,

// Start horizontal and vertical coordinates of the source bitmap

M_bm.bmwidth, 1,

// The pixel width and height of the source bitmap

Srccopy );

Sleep (20 );

}

// For (I = 0; I <= m_bm.bmheight; I ++)

3. shutter Effect

Principle: Divide bitmap data in memory device context objects (such as memdc) into several groups, and then move the data from the first group to the last group, the first time you move the first scanned line in each group to the corresponding position of the bitmap to be displayed in the target device (such as clientdc), the second scanned line in each group, and the third and fourth scanned lines.

Program Algorithm:

Int I, stepi, J;

Stepi = m_bm.bmheight/10;

For (I = 0; I <= stepi; I ++)

{For (j = 0; j <10; j ++)

Clientdc. stretchblt (

0, J * stepi + I,

// The logical horizontal and vertical coordinates of the target device

M_bm.bmwidth, 1,

// Display the pixel width and height of a bitmap

& M_memdc,

// Source bitmap device context object

0, J * stepi + I,

// Start horizontal and vertical coordinates of the source bitmap

M_bm.bmwidth, 1,

// The pixel width and height of the source bitmap

Srccopy );

Sleep (20 );

} // For (I = 0; I <= stepi; I ++)

4. Random Block Effects

Principle: divides bitmap data from memory device context objects (such as memdc) into one hundred groups of data in the aspect and aspect ratio, then, a group of the one hundred sets of data is randomly retrieved and displayed to the corresponding location of the bitmap to be displayed in the target device (such as clientdc) until all the one hundred sets of data are displayed.

Program Algorithm:

Int I, j, stepx, stepy, dispnum, X, Y;

Int pxy [10] [10];

// Use this array to record the displayed data groups

For (I = 0; I <10; I ++)

For (j = 0; j <10; j ++)

Pxy [I] [J] = 0;

Stepx = m_bm.bmwidth/10;

Stepy = m_bm.bmheight/10;

Srand (unsigned) Time (null ));

Dispnum = 0;

// Record the number of displayed data groups

While (1)

{X = rand () % 10;

Y = rand () % 10;

If (pxy [x] [Y])

// Has the data group x and y displayed?

Continue;

Pxy [x] [Y] = 1;

// Indicates that the data group x and y in this group has been shown

Clientdc. stretchblt (

X * stepx, y * stepy,

// The logical horizontal and vertical coordinates of the target device

Stepx, stepy,

// Display the pixel width and height of a bitmap

& M_memdc,

// Source bitmap device context object

X * stepx, y * stepy,

// Start horizontal and vertical coordinates of the source bitmap

Stepx, stepy,

// The pixel width and height of the source bitmap

Srccopy );

Dispnum ++;

If (dispnum >=100)

Break;

Sleep (30 );

} // While (1)

Knot

The above program code has been debugged in Visual C ++ 6.0. All parts can be written into independent functions for flexible use. If you change the display effects of the above several types, we can also achieve a variety of other effects.

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.