Graphic stunts in Visual C ++

zhaozj2021-02-17  58

With the multimedia of computer information and the multimedia, in many learning software, game software, and multimedia courseware software, various graphic display skills, such as graphics, staggered, raindrops, 100-page window, building blocks random Stacking and other display modes. This makes the picture more lively, and it is more attractive to the user, and lays the foundation for better playing software. This article discusses some of the principles and specific methods of implementing graphics in Visual C 6.0.

Fundamental

In Visual C 6.0, the method and process of displaying bitmaps are as follows:

1. Display bitmap in the program resource (all data of the bitmap exist in the executable)

(1) Installing the position from the resource

● Define bitmap object data members cBitmap m_bitmap;

● Call the CBITMAP member function loadbitmap (), such as M_bitmap.LoadBitmap (IDB_bitmap1);

● Parameters of incoming loadbitmap are identifiers given when the bitmap is generated or introduced from the graphical editor.

(2) Generate memory device situations associated with bitmaps

CDC MEMDC;

MEMDC.CREATECOMPALEDC (NULL);

MEMDC.SELECTOBJECT (& M_Bitmap);

(3) Display bitmap

CClientDC ClientDC (this);

Bitmap BM;

M_bitmap.getObject (Sizeof (BM), & BM);

Clientdc.bitblt

(X, y, // target logic horizontal, longitudinal coordinates

BM.BMWIDTH, BM.BMHEIGHT, / / ​​Display bitmap pixel width, height

& MEMDC,

// Device context object to be displayed

Crossing, ordinate in 0, // source data

Srccopy); // bit operation mode

This method shows that the bitmap speed is fast, but it is not very flexible, and the executable will increase.

2. Display a bitmap of a separate file mode (all the data of the bitmap is independent of the executable)

Hbitmap * hbitmap; // Define bitmap object handle

Bitmap BM;

CDC MEMDC;

CClientDC ClientDC (this);

Memdc.createCompatibleDC (& ClientDC);

Hbitmap = (hbitmap *) :: loadimage

(AfxGetInstanceHandle (),

// acquire the application handle

"Demo1.bmp",

// bitmap file name

Image_bitmap,

// type is Windows bitmap

0, 0,

Lr_loadfromfile;

// Net the diagram data from the file

Memdc.selectObject (hbitmap);

:: GetObject (Hbitmap, Sizeof (BM), & BM);

Clientdc.bitblt (...)

// Use the format and method

This method shows that the bitmap speed is slower than the previous one, but its flexibility is large, can change bitmap files, without removing the source program, also reduces the size of the executable.

Implementation

The specific implementation principle and method of various graphic display techniques are introduced. All the implementations of all program algorithms can be placed in terms of view (CView, also on other classes), and it is necessary to perform as follows:

Increase the following member variables:

Bitmap m_bm;

/ / Save data from the width, height of the bitmap

Hbitmap * m_hbitmap;

/ / Save bitmap data handle

CDC M_MEMDC; // Memory device situational object

Add the following code in the class constructor:

m_memdc.createcompatibledc (null); // Generate a memory device situation

m_hbitmap = (hbitmap *) :: loadingimage

/ / Push the bitmap data from the file

AfxgetInstanceHandle (), "Demo1.bmp",

Image_bitmap,

0, 0,

Lr_loadfromfile;

m_memdc.selectObject (m_hbitmap); // Select bitmap to memory device situations

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

1. horizontal interlaced effect

Principle: Split the bitmap data in the memory device context object (such as MEMDC), two parts: two parts: the odd number of scan lines move downward, the even number of scan lines move from the bottom, and Both are carried out simultaneously. The effect on the screen is the more light grid graphics that appear on both ends, gradually approaching, until the entire bitmap is fully clear. The principle of the realization of vertical interleaved effects.

Program algorithm:

INT I, J;

For (i = 0; i <= m_bm.bmheight; i = 2)

{j = i;

While (j> 0)

{Clientdc.stretchChblt

// odd number, from top to bottom

0, J-1,

// Target device logic horizontal, longitudinal coordinate

m_bm.bmwidth, 1,

// Display the pixel wide, height of the bitmap

& m_memdc,

// Source bit map device situation object

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

// Source bit map start horizontal, ordinate

m_bm.bmwidth, 1,

// Pixel wide, height

Srccopy);

Clientdc.stretchblt (

// Even, from below

0, m_bm.bmheight-j,

// Target device logic horizontal, longitudinal coordinate

m_bm.bmwidth, 1,

// Display the pixel wide, height of the bitmap

& m_memdc,

// Source bit map device situation object

0, I-J,

// Source bit map start horizontal, ordinate

m_bm.bmwidth, 1,

// Pixel wide, height

Srccopy);

J- = 2;

// While (j> 0)

Sleep (10);

}

// for (i = 0; i <= m_bm.bmHeight; i = 2)

2. Rain drops effect

Principle: The last scan line of the memory device situation object (such as MEMDC), sequentially moves from the target device (such as ClientDC) to display the first scan line of the bitmap to the last one, and Keep the trajectory left when this scan line moves on the screen. Then, the diagram of the diagram data of the MEMDC mediate data is then moved to the position where the first scan line of the target device (such as ClientDC) is displayed to the position of the position of the bitmap. The remaining scan 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,

// Target device logic horizontal, longitudinal coordinate

m_bm.bmwidth, 1,

// Display the pixel wide, height of the bitmap

& m_memdc,

// Source bit map device situation object

0, m_bm.bmheight-i,

// Source bit map start horizontal, ordinate

m_bm.bmwidth, 1,

// Pixel wide, height

Srccopy);

Sleep (20);

}

// for (i = 0; i <= m_bm.bmheight; i )

3. Blove effect

Principle: Divide the bitmap data in the memory device context object (such as MEMDC) into several groups, then move from the first group to the last set, and move the first scan line to the target device in each group, respectively. ClientDC) The corresponding position of the bitmap is to be displayed, and the second scan line in each group is moved second, then the third, fourth scan lines. Program algorithm:

INT I, Stepi, J;

Stepi = m_bm.bmheight / 10;

For (i = 0; i <= step; i )

{for (j = 0; j <10; j )

Clientdc.stretchblt (

0, J * STEPI I,

// Target device logic horizontal, longitudinal coordinate

m_bm.bmwidth, 1,

// Display the pixel wide, height of the bitmap

& m_memdc,

// Source bit map device situation object

0, J * STEPI I,

// Source bit map start horizontal, ordinate

m_bm.bmwidth, 1,

// Pixel wide, height

Srccopy);

Sleep (20);

} // for (i = 0; i <= step; i )

4. Random block effect

Principle: Divide the bitmap data in the memory device situation object (such as MEMDC) into a vertical and horizontal set of 100 sets of data, and then randomly removes a group in this hundred sets of data displayed in the target device (such as ClientDC). The corresponding position of the bitmap is displayed, so repeatedly until all 100 sets of data have been shown.

Program algorithm:

INT I, J, Stepx, Stepy, Dispnum, X, Y;

INT PXY [10] [10];

// Use the number of groups to record the displayed data group

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 (NULL);

DISPNUM = 0;

// Record the number of data groups that have been displayed

While (1)

{x = rand ()% 10;

Y = rand ()% 10;

IF (PXY [x] [y])

// Is it displayed by the data group represented by this group X, Y?

CONTINUE;

PXY [x] [y] = 1;

// Indicates that the data group represented by this group of X and Y has been displayed

Clientdc.stretchblt (

x * stepx, y * stepy,

// Target device logic horizontal, longitudinal coordinate

Stepx, STEPY,

// Display the pixel wide, height of the bitmap

& m_memdc,

// Source bit map device situation object

x * stepx, y * stepy,

// Source bit map start horizontal, ordinate

Stepx, STEPY,

// Pixel wide, height

Srccopy);

DISPNUM ;

IF (Dispnum> = 100)

Break;

Sleep (30);

} // while (1)

Conclusion

The above program code is debugged in Visual C 6.0, and all pieces can be written into independent functions, flexible. If you transform the above display effects, we can also achieve a variety of other stunt effects.

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

New Post(0)