1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
|
#include <windows.h>
float x = 100.0f; float y = 100.0f; float speed = 10.0f; float friction = 0.99f; float dx = 0.0f; float dy = 0.0f;
LRESULT CALLBACK MainWndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int nCmdShow) { WNDCLASS wc; MSG msg; HWND hWnd;
if( !hPrevInstance ) { wc.lpszClassName = "GenericAppClass"; wc.lpfnWndProc = MainWndProc; wc.style = CS_OWNDC | CS_VREDRAW | CS_HREDRAW; wc.hInstance = hInstance; wc.hIcon = LoadIcon( NULL, IDI_APPLICATION ); wc.hCursor = LoadCursor( NULL, IDC_ARROW ); wc.hbrBackground = (HBRUSH)( COLOR_WINDOW+1 ); wc.lpszMenuName = "GenericAppMenu"; wc.cbClsExtra = 0; wc.cbWndExtra = 0;
RegisterClass( &wc ); }
hWnd = CreateWindow( "GenericAppClass", "Happy Ball", WS_OVERLAPPEDWINDOW & (~WS_MAXIMIZEBOX) & (~WS_THICKFRAME), 100, 100, 800, 600, NULL, NULL, hInstance, NULL );
ShowWindow( hWnd, nCmdShow );
while( GetMessage( &msg, NULL, 0, 0 ) ) { TranslateMessage( &msg ); DispatchMessage( &msg ); }
return msg.wParam; }
LRESULT CALLBACK MainWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { static PAINTSTRUCT ps; static HDC hDC; static HBRUSH hBrush; static RECT rect; GetClientRect(hwnd, &rect); const int nRadius = 16;
switch (message) { case WM_CREATE: SetTimer(hwnd, 1, 5, NULL); hBrush = CreateSolidBrush(RGB(0,0,0)); return 0;
case WM_PAINT: hDC = BeginPaint(hwnd, &ps); SelectObject(hDC, hBrush); Ellipse(hDC, x - nRadius, y - nRadius, x + nRadius, y + nRadius); EndPaint(hwnd, &ps); break;
case WM_KEYDOWN: switch (wParam) { case VK_UP: dy -= speed; break; case VK_DOWN: dy += speed; break; case VK_LEFT: dx -= speed; break; case VK_RIGHT: dx += speed; break; } break;
case WM_TIMER: dx *= friction; dy *= friction; x += dx; y += dy; if (x > rect.right - nRadius) { x = (rect.right - nRadius) - (x - (rect.right - nRadius)); dx = -dx; } if (x < nRadius) { x = nRadius + nRadius - x; dx = -dx; } if (y > rect.bottom - nRadius) { y = rect.bottom - nRadius - (y - (rect.bottom - nRadius)); dy = -dy; } if (y < nRadius) {y = nRadius + nRadius - y; dy = -dy; } InvalidateRect(hwnd, &rect, TRUE);
break;
case WM_DESTROY: KillTimer(hwnd, 1); DeleteObject(hBrush); PostQuitMessage(0); return 0; } return DefWindowProc(hwnd, message, wParam, lParam); }
|