Simple animated effect of Windows programming (small ball bouncing)

Source: Internet
Author: User

Constructs a rectangular bitmap, the bitmap has a shadow and the purple gap small ball, the program uses the timer to control the small ball movement, actually is each time receives the timer message to copy the bitmap through the BitBlt function to the customer area, each time the small ball collides to the customer area up and down four sides to bounce back. The following code has my understanding of the comments can be used for reference, I Caishuxueqian, please forgive me. As follows:
Just beginning (the initial is actually in the customer district center began, difficult to find a random, but understanding is OK, not the way):

After moving:

#include <windows.h> #define Id_timer 1LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM WPARAM, LPARAM LPARAM ) int WINAPI WinMain (hinstance hinstance, hinstance hprevinstance, LPSTR szcmdline, int icmdshow) {static TCHAR Szappna    Me[] = TEXT ("Bounce");    HWND hwnd;//Window handle MSG msg;//message struct//Create window class Wndclassex wndclass; Set the window class properties wndclass.cbsize = sizeof (wndclassex);//Set the window class structure body size Wndclass.cbclsextra = 0;//window class part of the trailer extra space wndclass.c    Bwndextra = 0;    Wndclass.hinstance = hinstance;//The current instance handle of the application Wndclass.hcursor = LoadCursor (NULL, idc_help);    Wndclass.hicon = NULL;    WNDCLASS.HICONSM = NULL;    Wndclass.hbrbackground = (hbrush) getstockobject (White_brush); Wndclass.lpfnwndproc = wndproc;//The address of the callback function (window message handler) Wndclass.lpszclassname = szappname;//The name of the window class, that is, the window's identity,    The parameters that are later used to create the window function.    Wndclass.lpszmenuname = null;//The name of the menu, not NULL. Wndclass.style = Cs_hredraw | cs_vredraw;//the style of the window class, whose value can be any combination of window style values.    Cs_hredraw Cs_vredraw, this is vertical refresh and horizontal refresh, window size change, redraw active area. Register toThe Word box class if (!        RegisterClassEx (&wndclass)) {DWORD Error_code = GetLastError (); MessageBox (NULL, TEXT ("This program requires Windows nt!"), Text ("Numrain"), Mb_iconerror |        Mb_okcancel);    return 0; HWnd = CreateWindow (Szappname, TEXT ("The Hello Program"), Ws_overlappedwindow, $, $, $, $, NULL, NULL, Hinsta    NCE, NULL);    ShowWindow (HWnd, icmdshow);    UpdateWindow (HWND);        while (GetMessage (&msg, NULL, 0, 0)) {translatemessage (&msg);    DispatchMessage (&AMP;MSG); } return (int) Msg.wparam;}    LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM WPARAM, LPARAM LPARAM) {HDC hdc,hdcmem;    Hbrush Hbrush;    int Iscale;    static Hbitmap hbitmap;    static int cxclient, Cyclient,xcenter,ycenter,cxtotal,cytotal,cxradius,cyradius,cxmove,cymove,xpixel,ypixel;        Switch (message) {case WM_CREATE://Initialize Data HDC = GetDC (hwnd); Xpixel = GetDeviceCaps (hdc, ASPECTX);//36 ypixel = GetDeviceCaps (HDc, aspecty);//36 ReleaseDC (hwnd, HDC);        SetTimer (hwnd, Id_timer, N, NULL);    return 0; Case WM_SIZE://client Area Center coordinates xcenter = (cxclient = LoWord (LParam))/2;//392 Ycenter = (cyclient = HiWord (LParam))  /2;//230 Iscale = min (cxclient*xpixel, cyclient*ypixel)/16;//1037//ball horizontal radius and vertical radius Cxradius = Iscale /XPIXEL;//28 Cyradius = ISCALE/YPIXEL;//28//ball horizontal half of the radius and half of the vertical radius cxmove = max (1, CXRADIUS/2);//1 4 Cymove = max (1, CYRADIUS/2);//14//Bitmap width and Height cxtotal = 2 * (Cxradius + cxmove);//84 cytotal =        2 * (Cyradius + cymove);//84 if (hbitmap) {DeleteObject (HBITMAP);        } HDC = GetDC (hwnd);        Create a memory device environment HDCMEM = CREATECOMPATIBLEDC (HDC);        Creates a GDI bitmap object that is compatible with the client area, the bitmap width is cxtotal, and the height is cytotal hbitmap = CreateCompatibleBitmap (hdc, cxtotal, cytotal);        ReleaseDC (hwnd, HDC); The GDI bitmap object is selected into the memory device environment, which expands the display surface SelectObject (Hdcmem, HBiTMAP);        The bounding rectangle is drawn outside the bitmap Rectangle (Hdcmem,-1,-1, cxtotal + 1, cytotal + 1);        Create shadow Brush Hbrush = Createhatchbrush (Hs_diagcross, 0L);        SelectObject (Hdcmem, hbrush);        Sets the space for the shadow brush interval to be purple SetBkColor (Hdcmem, RGB (255, 0, 255));        /* Draw small balls in the center of the bitmap it is worth noting that the purple effect of the shadow brush and Shadow gap is after the rectangle function, indicating that only the ball has a shadow brush and a purple gap, and the Benis outside the ball is still white.        */Ellipse (Hdcmem, Cxmove, Cymove, Cxtotal-cxmove, Cytotal-cymove);        DeleteDC (HDCMEM);        DeleteObject (Hbrush);    return 0;        Case Wm_timer:if (!hbitmap) {break;        } HDC = GetDC (hwnd);        Create a memory device environment HDCMEM = CREATECOMPATIBLEDC (HDC);        Select the drawn bitmap object into the memory device environment SelectObject (HDCMEM, HBITMAP); If the drawing is carefully analyzed, you will find that the center of the bitmap, horizontal and vertical each move cxmove pixels, there will be a new bitmap overlay the ball of the previous bitmap, there will be no shadow of the ball BitBlt (hdc, XCENTER-CXTOTAL/2, Ycenter-cyto        TAL/2, Cxtotal, Cytotal, Hdcmem, 0, 0, srccopy);        ReleaseDC (hwnd, HDC);        DeleteDC (HDCMEM); Xcenter + = Cxmove;        Ycenter + = Cymove; Collision detection on both sides of the client area if ((Xcenter + cxradius >= cxclient) | |        (Xcenter-cxradius <= 0))        {//Bounce the ball back cxmove =-cxmove; } if ((Ycenter + cyradius >= cyclient) | |        (Ycenter-cyradius <= 0))        {//Bounce the ball back cymove =-cymove;    } return 0; Case Wm_close:if (IDOK = = MessageBox (hwnd), TEXT ("Do you want to exit? "), TEXT (" dialog box "), Mb_okcancel | Mb_defbutton1 |        mb_iconquestion)) {DestroyWindow (HWND);        } else {return 0;        } Case Wm_destroy:if (HBITMAP) {DeleteObject (HBITMAP);        } KillTimer (hwnd, Id_timer);        PostQuitMessage (0);    return 0; } return DefWindowProc (HWND, message, WParam, LParam);}

Simple animation effect of Windows programming (small ball bouncing)

Related Article

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.