En version hvor bolden triller til venstre eller højre når du trykker på venstre/højre piletast:
#include <windows.h>
#include <sstream>
#include <string>
HINSTANCE InstanceHandle;
HWND MainWindow;
template <typename T>
std::string ToString(T aValue)
{
std::stringstream ss;
ss << aValue;
return ss.str();
}
HBITMAP Image[4];
signed int ImageIdx;
int Direction;
bool TimerRunning;
HBRUSH BackBrush;
int XOffset;
enum CtrlIdEnum
{
AnimationTimer = 1050
};
void DrawBall(HWND hwnd, int NewPos)
{
HDC dc = GetDC(hwnd);
// First remove old ball
RECT R = {XOffset, 0, XOffset + 64, 64};
FillRect(dc, &R, BackBrush);
// Then draw the ball at new pos
XOffset = NewPos;
HDC MemDC = CreateCompatibleDC(dc);
HBITMAP OldBitmap = (HBITMAP )SelectObject(MemDC, Image[ImageIdx % 4]);
BitBlt(dc, NewPos, 0, 64, 64, MemDC, 0, 0, SRCCOPY);
SelectObject(MemDC, OldBitmap);
DeleteObject(MemDC);
ReleaseDC(hwnd, dc);
ImageIdx += Direction;
}
LRESULT CALLBACK MainWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_CREATE:
{
int i;
std::string FileName;
for(i = 1; i < 5; i++)
{
FileName = "Image" + ToString(i) + ".bmp";
Image[i - 1] = (HBITMAP )LoadImage(0, FileName.c_str(), IMAGE_BITMAP, 64, 64, LR_LOADFROMFILE);
if(Image[i - 1] == 0)
MessageBox(hwnd, "Failed to load image", "Animation", MB_OK);
}
XOffset = 300;
BackBrush = CreateSolidBrush(RGB(255, 255, 255));
break;
}
case WM_PAINT:
DrawBall(hwnd, XOffset);
ValidateRect(hwnd, 0);
break;
case WM_TIMER:
{
DrawBall(hwnd, XOffset + 16*Direction);
if(ImageIdx > 35 || ImageIdx < 5)
KillTimer(hwnd, AnimationTimer);
break;
}
case WM_KEYDOWN:
switch(wParam)
{
case VK_LEFT:
DrawBall(hwnd, 300);
SetTimer(hwnd, AnimationTimer, 250, 0);
ImageIdx = 20;
Direction = -1;
break;
case VK_RIGHT:
DrawBall(hwnd, 300);
SetTimer(hwnd, AnimationTimer, 250, 0);
ImageIdx = 20;
Direction = 1;
break;
}
break;
default:
return DefWindowProc(hwnd,msg,wParam,lParam);
}
return 0;
}
HWND CreateMainWindow()
{
WNDCLASS wc;
memset(&wc, 0, sizeof(WNDCLASS));
wc.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS ;
wc.lpfnWndProc = (WNDPROC )MainWndProc;
wc.hInstance = InstanceHandle;
wc.hbrBackground = (HBRUSH )(COLOR_WINDOW + 1);
wc.lpszClassName = "SimpleWinWndClass";
wc.lpszMenuName = 0;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
if(!RegisterClass(&wc))
return 0;
return CreateWindow("SimpleWinWndClass",
"Simple-Window",
WS_MINIMIZEBOX | WS_VISIBLE |
WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_MAXIMIZEBOX |
WS_CAPTION | WS_BORDER | WS_SYSMENU | WS_THICKFRAME,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0,
0,
0,
InstanceHandle,
0);
}
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
INT nCmdShow)
{
InstanceHandle = hInstance;
if((MainWindow = CreateMainWindow()) == (HWND )0)
{
MessageBox(0, "Failed to create MainWindow!", "Warning", MB_OK);
return 0;
}
ShowWindow(MainWindow, SW_SHOW);
MSG Msg;
while(GetMessage(&Msg, 0, 0, 0))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}
Du skal bruge bmp-filerne fra .zip-filen