VC realizes animation application two

zhaozj2021-02-11  210

VC realizes animation application two

Li Lili, North China Petroleum Research Institute

---- Windows (9x) supported VC is one of the most widely used languages, and now there is still a major user. The author is one of the "believers" of the VC, which has spent a lot of time here, introducing the two application instances of the author, hoping to communicate with you.

---- 1. Implement fast animation in VC

---- Fast Animation refers to rapidly wiped the original picture every other period of time and reorganizes the animation technology of the new screen. The key to the success of the fast animation is that the speed of wiping and heavy painting must be very fast, otherwise the picture will flash.

---- Creating a quick animation in VB is relatively simple, just set the AUTOREDRAW property to true, then call the API function Bitblt, draw a frame of Refresh. However, the form of the VC does not have an AutoreRAW attribute. As long as one Bitblt is used, the form will be automatically refreshed. Since the frame is often used to use Bitblt several times, the screen flashes. The solution is to define an invisible buffer that should be the same as the target form, first draw a frame of the frame on the buffer, and use the bitblt function to post the buffer pattern onto the form. . Please see the case: // Add this three sentences in TFORM1 * FORM1;

Graphics :: tbitmap * p; graphics :: tbitmap * q;

INT xx = 0; // ---------------------

Void __fastcall tform1 :: formcreate

(TOBJECT * Sender)

{P = new graphics :: tbitmap;

// This is put to the small pattern to be attached to the form.

P-> Handle = loadingBitmap (Hinstance, "AAA");

// load small pattern from the resource file

Q = new graphics :: tbitmap; // Define buffer QQ-> width = width;

/ / The size of the buffer is the same as the form

Q-> Height = Height; Patblt (Q-> Canvas->

Handle, 0, 0, q-> width, q-> height, 0);

// turn the background of the buffer to black}

/ / -------?

Void __fastcall tform1 :: formdestroy

(TOBJECT * Sender)

{// Release the memory delete P at the end of the program

Delete} // --------------------

Void __fastcall tform1 :: Timer1Timer

(TOBJECT * Sender)

{// Add a Timer control on the form

XX = 2; Patblt (Q-> Canvas-> Handle,

0,0, width, height, 0);

// turn the background of the buffer into black while wiped the old picture

Bitblt (Q-> Canvas-> Handle, XX, 0, P->

Width, P-> Height,

P-> Canvas-> Handle, 0, 0, SRCCPY;

/ / Is making a frame on the buffer, which is invisible.

Bitblt (Q-> Canvas-> Handle, XX, 50, P->

Width, P-> Height,

P-> canvas-> handle, 0,0, srcopy; Bitblt (Q-> Canvas-> Handle, XX, 100, P->

Width, P-> Height,

P-> Canvas-> Handle, 0, 0, SRCCPY;

Bitblt (Q-> Canvas-> Handle, XX, 150, P->

Width, P-> Height,

P-> Canvas-> Handle, 0, 0, SRCCPY;

Bitblt (Q-> Canvas-> Handle, XX, 200, P->

Width, P-> Height,

P-> Canvas-> Handle, 0, 0, SRCCPY;

Bitblt (Q-> Canvas-> Handle, XX, 250, P->

Width, P-> Height,

P-> Canvas-> Handle, 0, 0, SRCCPY;

Bitblt (Q-> Canvas-> Handle, XX, 300, P->

Width, P-> Height,

P-> Canvas-> Handle, 0, 0, SRCCPY;

Bitblt (Canvas-> Handle, 0, 0, Width,

HEIGHT, Q-> Canvas->

Handle, 0,0, srcopy;

// Post the screen of the buffer to the form}

---- If the actual application, like the above program, there may be a small problem in the ONTIMER event of the Timer Control. If the user suspended an animation, the form is still a heavy draw event (such as the form being minimized and is restored), the screen on the form will disappear. This is because the form is scrutinized, only the universal part of the form is painted, and Windows does not know that there is a custom picture on the original form. To allow Windows to restore the picture, you must put the motion of the motion in the form of the form of the ONPAINT event, which only write the statement of the status position in the Timer control (such as XX = 2 in this example;) and a sentence Repaint. I will omit the specific programs after modifying, you can do it yourself.

---- II. Insert micro animation in the VC program

---- The following uses the CIMAGELIST class to save a few pictures, and the DRAW function is used to play at a certain time interval to form an effect similar to the GIF animation. This method can play an animation on the toolbar, the status bar on the WINDOW client area. Here is a method of playing an animation using the Seticon function on the window title bar.

---- (1), principle

---- There is a CIMAGELIST class in the VC to manage the image in the way, and the image size in the image list is the same. The index begins with 0, each image can be separately referenced. Microsoft's API provides a range of functions that you can use to create, destroy the image list, display images, add, and delete images, replace, merge, and drag images.

---- The CIMAGELIST class provides the WINDOWS image list universal control function. The following is a brief description of the functions of this article as follows: BOOL CREATE (int Cx, Int Cy, uint

NFLAGS, INT NINITIAL, INTNGROW

---- This function is used to create an image list. CX, CY is the width and height of each image; NFLAGS is the type of image list whose value can only contain an ILC_COLOR value. It takes details to see VC online help. Ninitial is the number of images that originally contained in the image list; NGROW is the number of images per dynamically when the number of images is changed.

Bool Draw (CDC * PDC, Int Nimage,

Point PT, uint nStyle;

---- This function is used to display an image. PDC is a pointer to the target device context; NIMAGE is the image index to display; Pt is the location of the image display; nStyle is an image display style, see Online Help.

Hicon Extracticon (Int nimage);

Use this function to get a function handle:

Int Add (Hicon Hicon);

This function adds an image to the image list.

---- (2), programming and implementation

---- First, establish a chart resource. Use the resource editor in VC6.0, build several charts, IDi_icon1, idi_icon2, idi_icon3 ... When editing icons, set the icon to set the icon to 64x32. Since the various components of Windows are not exactly the same, the method is not exactly the same, and the method of displaying the animation in different positions in the window is described separately.

---- 1. Draw animations in the View Customer Area

---- Add the following variables in the definition file of the class:

Point PT1; // Image displayed

INT m_play; // will display the index of the image

Void createImagelist (); // Create a function of the image list

CIMAGELIST M_IMAGELIST1; / / Image list object

INT m_imagenumber; // The total number of images in the image list

First initialize Pt1.m_Play, M_Imagenumber:

CIMAGEVIEW :: CIMAGEVIEW ()

{

// Todo: Add Construction Code Here

Pt1.x = 1;

Pt1.y = 1;

M_Play = 0;

m_imagenumber = 0;

}

The implementation of CreateImageList () is as follows:

Void CIMAGELIST ()

{

m_imagelist1.create (64, 32, ILC_COLOR, 5, 2);

Hicon Hicon = :: loadicon (AfxgetResource-

Handle (), makeintResource (IDi_ICON1));

m_imagelist1.add (hic);

m_imagenumber ;

Hicon = :: Loadicon (AfxgetResourceHandle (),

MakeintResource (IDi_ICON2));

m_imagelist1.add (hic);

m_imagenumber ;

Hicon = :: Loadicon (AfxgetResourceHandle (),

MakeintResource (IDI_ICON3));

m_imagelist1.add (hic);

m_imagenumber ;

// add all resources you want to play to the image list.

}

Set the timer in the oncreate function and create an image list:

INT CIMAGEVIEW :: OnCreate

lpcreateStruct)

{

IF (cview :: oncreate (lpcreatestructure) == -1)

Return -1;

// Todo: add Your specialized code code heRecreateImageList ();

Settimer (1,500, null);

Return 0;

}

Respond to the ON_TIMER message, display animation:

Void CIMAGEVIEW :: ONTIMER (uint nidevent)

{

// Todo: Add your message Handler Code

Here and / or Call Default

CDC * PDC = Getdc ();

IF (M_Play> m_imagenumber)

M_Play = 0;

m_imagelist1.draw (PDC, M_Play, PT1,

ILD_TRANSPARENT);

M_Play ;

ReleaseDC (PDC);

CView :: ONTIMER (Nidevent);

}

---- Don't forget that in the onDestroy function, increase the code of the timer when the window is canceled.

---- 2. Display animation on the status bar

---- Since the status bar is also a window, you can also display animation. You can see the following code in the CMAINFRAME class:

protected: // control bar Embedded Member

CSTATUSBAR M_WNDSTATUSBAR;

---- So in order to display animation on the status bar, its programming code should be added to the CMAINFRAME class. First create a resource file and image list class, specific methods and code See the View Class Customer District Drawing Animation, which is no longer repeated. The following is given the ON_TIMER's response function:

Void CMAINFRAME :: ONTIMER (uint nidevent)

{

// Todo: Add your message Handler Code

Here and / or Call Default

IF (M_Play> m_imagenumber)

M_Play = 0; // If the picture is the last one, display the first picture

CDC * PDC = this-> m_wndstatusbar. Getdc ();

ASSERT (PDC! = Null);

Pt1.x = 1;

Pt1.y = 1;

m_imagelist1.draw (PDC, M_Play, PT1,

ILD_TRANSPARENT);

ReleaseDC (PDC);

M_Play ;

CframeWnd :: ONTIMER (Nidevent);

}

---- The above code will play an animation in the upper left side of the status bar.

---- 3. Play animation on the toolbar

---- Since the nature of the toolbar is similar to the status bar, the method of playing animations is similar, and the ON_TIMER's response function is given:

Void CMAINFRAME :: ONTIMER (uint nidevent)

{

// Todo: Add your message handler

Code Here and / or Call Default

IF (M_Play> m_imagenumber)

M_Play = 0; // If the picture is the last one, display the first picture

CRECT RECT;

CDC * PDC;

PDC = this-> m_wndtoolbar.getdc ();

ASSERT (PDC! = Null);

This-> m_wndtoolbar.getClienceRect (& Re);

// Get the display effective area

Pt1.x = Rect.right -64; // Set the display position to the far right

Pt1.y = 1;

m_imagelist1.draw (PDC, M_Play, PT1, ILD_TRANSPARENT);

ReleaseDC (PDC);

M_Play ;

CframeWnd :: ONTIMER (Nidevent);

}

---- The above code will play an animation in the top right of the toolbar. However, if you look closely, the position of the animation is not approaching the right side of the window, because the window has a boundary, use the following method, you can move the screen to the right side: PDC = getdc (); // Get CMAINFRAME drawing device pointer

ASSERT (PDC! = Null);

This-> getClientRect (& Re);

Pt1.x = Rect.right-64;

Pt1.y = Rect.top 3;

m_imagelist1.draw (PDC, M_Play, PT1, ILD_TRANSPARENT);

ReleaseDC (PDC);

This is because the location occupied by the toolbar belongs to the CMAINFRAME client area.

---- 4. Make the icon into an animation

---- There is a function in the CWND class:

Hicon Seticon (HiCon Hicon, Bool Bbigicon);

You can change the icon of the window, so you can use it

The function constantly changing the map, which makes the icon move, effect like getRight

same. Add the following code in the ONTIMER function:

Seticon (m_imagelist1.extracticon (m_play), false;

---- You can move the icon. Of course, in order to make the program better, you'd better rebuild a set of icon resources.

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

New Post(0)