Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Simple animation effect of Windows programming (small ball bounce)

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

Construction of a rectangular bitmap, bitmap with a shadow and purple gap between the ball, the program uses a timer to control the ball's action, in fact, whenever received a timer message when the bitmap is copied to the client area through the BitBlt function, whenever the ball hit the client area up and down the left and right sides of the bounce back. The following code has my own understanding notes for reference, I am not talented enough to learn, please forgive me. The effect picture is as follows:

At the beginning (it actually started in the center of the client area, so you can easily find a screenshot of the difficult screenshot, but you can OK if you understand it. It doesn't matter):

After moving:

# include#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 szAppName [] = TEXT ("Bounce"); HWND hWnd;// window handle MSG mSg;// message structure / / create window class WNDCLASSEX wndClass; / / set window class properties wndClass.cbSize = sizeof (WNDCLASSEX) / / set the window class structure body size wndClass.cbClsExtra = 0; the extra space at the end of the window class wndClass.cbWndExtra = 0; wndClass.hInstance = the current instance handle of the hInstance;// 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 = the name of the szAppName;// window class, that is, the identity of the window, followed by the parameters used to create the window function. WndClass.lpszMenuName = the name of the NULL;// menu, not NULL. WndClass.style = CS_HREDRAW | style of the CS_VREDRAW;// 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 dialog 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, 200,200,800,500, NULL, NULL, hInstance, NULL); ShowWindow (hWnd, iCmdShow) UpdateWindow (hWnd); while (GetMessage (& mSg, NULL, 0,0)) {TranslateMessage (& mSg); DispatchMessage (& 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: / / initialization data hdc = GetDC (hwnd); xPixel = GetDeviceCaps (hdc, ASPECTX); / / 36 yPixel = GetDeviceCaps (hdc, ASPECTY); / / 36 ReleaseDC (hwnd, hdc); SetTimer (hwnd, ID_TIMER, 50, NULL); return 0 Case WM_SIZE: / / coordinates of the center of the client area xCenter = (cxClient = LOWORD (lParam)) / 2 lParam min 392 yCenter = (cyClient = HIWORD (lParam)) / 2 iScale = min (cxClient*xPixel, cyClient*yPixel) / 16 X iScale 1037 / / horizontal and vertical radius of the ball cxRadius = iScale / xPixel;//28 cyRadius = iScale / yPixel / / 28 / / half of the horizontal radius of the sphere and half of the vertical radius cxMove = max (1, cxRadius / 2); / / 14 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 memory device environment hdcMem = CreateCompatibleDC (hdc); / / create a GDI bitmap object compatible with the client area, with a width of cxTotal and a height of cyTotal hBitmap = CreateCompatibleBitmap (hdc, cxTotal, cyTotal); ReleaseDC (hwnd, hdc) / GDI bitmap objects are selected into the memory device environment, expanding the display surface SelectObject (hdcMem, hBitmap); / / rectangular frames are drawn outside the bitmap Rectangle (hdcMem,-1,-1, cxTotal + 1, cyTotal + 1); / / create shadow brushes hBrush = CreateHatchBrush (HS_DIAGCROSS, 0L); SelectObject (hdcMem, hBrush) / / set the space between shadow brushes as purple SetBkColor (hdcMem, RGB (255,0,255)); / * draw the ball in the center of the bitmap. It is worth noting that the purple effect of shadow brush and shadow gap is after the Rectangle function, indicating that only the ball has a shadow brush and purple gap, while the edge space 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 memory device environment hdcMem = CreateCompatibleDC (hdc) / / Select the drawn bitmap objects into the memory device environment SelectObject (hdcMem, hBitmap); / / if you analyze the bitmap carefully, you will find that the center of the bitmap, horizontal and vertical cxMove pixels, there will be a new bitmap covering the ball of the previous bitmap, so there will be no residual shadow of the ball BitBlt (hdc, xCenter-cxTotal / 2, yCenter-cyTotal / 2, cxTotal, cyTotal, hdcMem, 0,0, SRCCOPY). ReleaseDC (hwnd, hdc); DeleteDC (hdcMem); xCenter + = cxMove; yCenter + = cyMove; / / collision detection if ((xCenter + cxRadius > = cxClient) on the left and right sides of the client area | | (xCenter-cxRadius = cyClient) | (yCenter-cyRadius)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report