#include #include "spot.h" #include "objSpot.h" spot blob; PAINTSTRUCT ps; const char className[] = "Spot"; const char APP_TITLE[] = "Moving spot demo"; const unsigned short int APP_WIDTH = 800; const unsigned short int APP_HEIGHT = 600; LRESULT CALLBACK mainEventListener (HWND hwnd, UINT event, WPARAM object, LPARAM lParam); LRESULT CALLBACK mainEventListener (HWND hwnd, UINT event, WPARAM object, LPARAM lParam) { if (event == WM_CHAR) { HDC hdc = GetDC(hwnd); unsigned short int buffer; if (object == 54) //right { buffer = blob.getX(); if ((buffer + 20) < APP_WIDTH) { blob.move(hdc, (buffer + 20), blob.getY()); } } else if (object == 52) //left { buffer = blob.getX(); if ((buffer - 20) > 0) { blob.move(hdc, (buffer - 20), blob.getY()); } } else if (object == 50) //Down { buffer = blob.getY(); if ((buffer + 20) < APP_HEIGHT) { blob.move(hdc, blob.getX(), (buffer + 20)); } } else if (object == 56) //Up { buffer = blob.getY(); if ((buffer - 20) > 0) { blob.move(hdc, blob.getX(), (buffer - 20)); } } ReleaseDC(hwnd, hdc); } else if (event == WM_PAINT) { HDC hdc = BeginPaint(hwnd, &ps); blob.plot(hdc); EndPaint(hwnd, &ps); } else if (event == WM_CLOSE) { DestroyWindow(hwnd); } else if (event == WM_DESTROY) { PostQuitMessage(0); } else { return DefWindowProc(hwnd, event, object, lParam); } return 0; } int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR args, int nCmdShow) { WNDCLASSEX wc; //The class settings HWND hwnd; //Main window handler MSG Msg; //Here messages to the application are saved //Step 1: Registering the Window Class wc.cbSize = sizeof(WNDCLASSEX); wc.style = 0; wc.lpfnWndProc = mainEventListener; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = hInstance; wc.hIcon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(imgMainIcon)); wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = (HBRUSH)(GetStockObject(WHITE_BRUSH)); wc.lpszMenuName = NULL; wc.lpszClassName = className; wc.hIconSm = (HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(imgMainIcon), IMAGE_ICON, 16, 16, 0); if(!RegisterClassEx(&wc)) //If the class was not registered successfully { MessageBox(NULL, "Window Registration Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK); return 0; } spot blob(); //Create the window hwnd = CreateWindowEx(WS_EX_CLIENTEDGE, className, APP_TITLE, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, APP_WIDTH, APP_HEIGHT, NULL, NULL, hInstance, NULL); if(hwnd == NULL) //If a problem occured with creating the window { MessageBox(NULL, "Window Creation Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK); return 0; } ShowWindow(hwnd, nCmdShow); UpdateWindow(hwnd); //This keeps looping and processing messages when they become available //The while loop ends when a destroy message is sent while(GetMessage(&Msg, NULL, 0, 0) > 0) { TranslateMessage(&Msg); DispatchMessage(&Msg); } return Msg.wParam; }