VC realizes animation application two

xiaoxiao2021-03-06  88

Windows (9x) supported VCs have been 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 to put it to the small pattern P-> handle = loadingBitmap (Hinstance, "AAA") on the form; / / from the resource file Pattern Q = New Graphics :: Tbitmap; // Defines buffer QQ-> width = width; // Make the size of the buffer with the same Q-> height = height; Patblt (Q-> Canvas-> Handle, 0 , 0, q-> width, q-> height, 0); // transform the background of the buffer to black} // ------- ??? Void __fastcall tform1 :: formDestroy (TOBJECT * SENDER) {// release memory delete p; delete} // ------------------- void __fastcall tform1 :: timer1timer (TOBJECT * Sender) {/ / Add a Timer Control XX = 2 on the form; Patblt (Q-> Canvas-> Handle, 0, 0, Width, Height, 0); // Take the background of the buffer into black, and wipe it off Old picture Bitblt (Q-> Canvas-> Handle, XX, 0, P-> Width, P-> HEIGHT, P-> Canvas-> Handle, 0, 0, Srccopy); / / is making one on the buffer The frame of the frame, these sentences are invisible Bitblt (Q-> Canvas-> Handle, XX, 50, P-> Width, P-> Height, P-> Canvas-> Handle, 0, 0, Srccopy); Bitblt (Q-> Canvas-> Handle, XX, 100, P-> Width, P-> Height, P-> Canvas-> Handle, 0, 0, Srccopy); Bitblt (Q-> Canvas-> Handle, XX , 150, P-> Width, P-> Height, P-> Canvas-> Handle, 0, 0, SRCC Opy); Bitblt (Q-> Canvas-> Handle, XX, 200, P-> Width, P-> Height, P-> Canvas-> Handle, 0, 0, Srccopy; Bitblt (Q-> Canvas-> Handle, XX, 250, P-> Width, P-> Height, P-> Canvas->

Handle, 0,0, srcopy; bitblt (Q-> Canvas-> Handle, XX, 300, P-> Width, P-> Height, P-> Canvas-> Handle, 0,0, Srccopy; Bitblt Canvas-> Handle, 0, 0, Width, Height, Q-> Canvas-> Handle, 0, 0, Srccopy; // Put the buffer's picture 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 used herein:

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 Extractic; uses this function to get a handle of a function: 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 position int m_play; // will display an index void createImageList (); // Create a function cimagelist m_imagelist1; // image list object int m_imagenumber; // Image list First initialize Pt1.m_Play, m_imagenumber: cimageview :: cimageview () {// Todo: add construction code herept1.x = 1; pt1.y = 1; m_play = 0; m_imagenumber = 0;} CreateImageList () is implemented as follows : void codeview :: CreateImageList () {m_imagelist1.create (64, 32, ILC_COLOR, 5, 2); HICON HICON = :: Loadicon (AFXGETRESOURCE-HANDLE (), makeintResource (IDi_icon1)); m_imagelist1.add (Hicon); m_ImageNumber ; hIcon = :: LoadIcon (AfxGetResourceHandle (), MAKEINTRESOURCE (IDI_ICON2)); m_ImageList1.Add (hIcon); m_ImageNumber ; hIcon = :: LoadIcon (AfxGetResourceHandle (), MAKEINTRESOURCE (IDI_ICON3)); m_ImageList1.Add (hIcon); m_imagenumber ; // add all resources you want to play to the image list. } OnCreate timer function is provided, and creates the image list: int CImageView :: OnCreate (LPCREATESTRUCTlpCreateStruct) {if (CView :: OnCreate (lpCreateStruct) == -1) return -1; // TODO: Add your specialized creation code hereCreateImageList (); SetTimer (1,500, NULL); return 0;} ON_TIMER response message, displaying an animation: void CImageView :: OnTimer (UINT nIDEvent) {// TODO: Add your message handler code here and / or call defaultCDC * 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);} ---- Final Don't forget In the OnDestroy function, the code is added to the time of the timer when the window is revoked.

---- 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 MEMBERSCSTATUSBAR 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 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 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 defaultif (m_play> m_imagenumber) m_play = 0; // If the drawing is the last one, display the first picture CRECT RECT; CDC * PDC; PDC = this-> m_wndtoolbar.getdc (); assert (pdc! = Null); this-> m_wndtoolbar.getClientRect (& Rect); // Get display valid area Pt1.x = Rect.right -64; / / Set the display position at the rightmost 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 rightmost edge of the window, because the window has a boundary, use the following method, you can move the screen to the right:

PDC = getDC (); // Get the drawing device pointer of CMAINFRAME (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 Bbigic); you can change the icon of the window, so you can continue to change the icon by using this function, the effect is like getRight. 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-96033.html

New Post(0)