Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma warning(disable:4996)
- #undef UNICODE
- #include <windows.h>
- #include <string>
- #include "KWnd.h"
- using namespace std;
- void updateRegion(HRGN& hRgn, int left, int top, int right, int bottom);
- LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
- const int INTERVAL = 110;
- bool isOdd(int i);
- int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
- {
- MSG msg;
- KWnd mainWnd("MyWin", hInstance, nCmdShow, WndProc);
- while (GetMessage(&msg, NULL, 0, 0))
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- return (msg.wParam);
- }
- LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
- {
- PAINTSTRUCT ps;
- HDC hDC;
- int userReply;
- //timer pause
- static bool paused = false;
- static bool isEpeleptic1, isEpeleptic2, isEpeleptic3;
- //sx,sy
- static int sx, sy;
- //point for mouse
- static POINT mousePoint;
- //timer t
- static int t = 0;
- //pen
- static HPEN hPenCommon;
- //brushed
- static HBRUSH hBrushBlack, hBrushWhite, hBrushRed, hBrushYellow, hBrushGreen;
- //regions
- static HRGN ellipse1, ellipse2, ellipse3;
- switch (message)
- {
- case WM_CREATE:
- //init values
- paused = false;
- isEpeleptic1 = false;
- isEpeleptic2 = false;
- isEpeleptic3 = false;
- //create regions
- ellipse1 = CreateEllipticRgn(sx / 10, 2 * sy / 10, 3 * sx / 10, 4 * sy / 10);
- ellipse2 = CreateEllipticRgn(4 * sx / 10, 2 * sy / 10, 6 * sx / 10, 4 * sy / 10);
- ellipse3 = CreateEllipticRgn(7 * sx / 10, 2 * sy / 10, 9 * sx / 10, 4 * sy / 10);
- //create pen
- hPenCommon = (HPEN)GetStockObject(1);
- //create brushes
- hBrushBlack = CreateSolidBrush(RGB(0, 0, 0));
- hBrushWhite = CreateSolidBrush(RGB(255, 255, 255));
- hBrushRed = CreateSolidBrush(RGB(255, 0, 0));
- hBrushYellow = CreateSolidBrush(RGB(255, 160, 0));
- hBrushGreen = CreateSolidBrush(RGB(0, 255, 0));
- //create timer
- SetTimer(hWnd, 1, INTERVAL, NULL);
- break;
- case WM_SIZE:
- sx = LOWORD(lParam);
- sy = HIWORD(lParam);
- break;
- case WM_LBUTTONDOWN:
- mousePoint.x = LOWORD(lParam);
- mousePoint.y = HIWORD(lParam);
- //check for point in region
- if (PtInRegion(ellipse1, mousePoint.x, mousePoint.y)) {
- isEpeleptic1 = true;
- }
- if (PtInRegion(ellipse2, mousePoint.x, mousePoint.y)) {
- isEpeleptic2 = true;
- }
- if (PtInRegion(ellipse3, mousePoint.x, mousePoint.y)) {
- isEpeleptic3 = true;
- }
- break;
- case WM_RBUTTONDOWN:
- mousePoint.x = LOWORD(lParam);
- mousePoint.y = HIWORD(lParam);
- //check for point in region
- if (PtInRegion(ellipse1, mousePoint.x, mousePoint.y)) {
- isEpeleptic1 = false;
- }
- if (PtInRegion(ellipse2, mousePoint.x, mousePoint.y)) {
- isEpeleptic2 = false;
- }
- if (PtInRegion(ellipse3, mousePoint.x, mousePoint.y)) {
- isEpeleptic3 = false;
- }
- break;
- case WM_PAINT:
- //begin paint
- hDC = BeginPaint(hWnd, &ps);
- //draw circles
- SelectObject(hDC, hPenCommon);
- if (isOdd(t) && isEpeleptic1) {
- updateRegion(ellipse1, sx / 10 + sx / 20, 2 * sy / 10 + sy / 20, 3 * sx / 10 - sx / 20, 4 * sy / 10 - sy / 20);
- }
- else {
- updateRegion(ellipse1, sx / 10, 2 * sy / 10, 3 * sx / 10, 4 * sy / 10);
- }
- FillRgn(hDC, ellipse1, hBrushRed);
- if (isOdd(t) && isEpeleptic2) {
- updateRegion(ellipse2, 4 * sx / 10 + sx / 20, 2 * sy / 10 + sy / 20, 6 * sx / 10 - sx / 20, 4 * sy / 10 - sy / 20);
- }
- else {
- updateRegion(ellipse2, 4 * sx / 10, 2 * sy / 10, 6 * sx / 10, 4 * sy / 10);
- }
- FillRgn(hDC, ellipse2, hBrushYellow);
- if (isOdd(t) && isEpeleptic3) {
- updateRegion(ellipse3,7 * sx / 10 + sx / 20, 2 * sy / 10 + sy / 20, 9 * sx / 10 - sx / 20, 4 * sy / 10 - sy / 20);
- }
- else {
- updateRegion(ellipse3, 7 * sx / 10, 2 * sy / 10, 9 * sx / 10, 4 * sy / 10);
- }
- FillRgn(hDC, ellipse3, hBrushGreen);
- //update regions
- updateRegion(ellipse1, sx / 10, 2 * sy / 10, 3 * sx / 10, 4 * sy / 10);
- updateRegion(ellipse2, 4 * sx / 10, 2 * sy / 10, 6 * sx / 10, 4 * sy / 10);
- updateRegion(ellipse3, 7 * sx / 10, 2 * sy / 10, 9 * sx / 10, 4 * sy / 10);
- //end paint
- EndPaint(hWnd, &ps);
- break;
- case WM_TIMER:
- if (t >= INTERVAL)
- t = 0;
- //InvalidateRgn(hWnd, NULL, true);
- if (isEpeleptic1) {
- InvalidateRgn(hWnd, ellipse1, true);
- }
- if (isEpeleptic2) {
- InvalidateRgn(hWnd, ellipse2, true);
- }
- if (isEpeleptic3) {
- InvalidateRgn(hWnd, ellipse3, true);
- }
- t++;
- break;
- case WM_KEYDOWN:
- {
- switch (wParam)
- {
- case VK_SPACE:
- {
- if (!paused) {
- KillTimer(hWnd, 1);
- paused = true;
- }
- else if (paused) {
- SetTimer(hWnd, 1, INTERVAL, NULL);
- paused = false;
- }
- break;
- }
- }
- break;
- }
- case WM_CLOSE:
- userReply = MessageBox(hWnd, "Are you sure?", "", MB_YESNO | MB_ICONQUESTION);
- if (IDYES == userReply)
- {
- DestroyWindow(hWnd);
- }
- break;
- case WM_DESTROY:
- //pen
- DeleteObject(hPenCommon);
- //brushes
- DeleteObject(hBrushBlack);
- DeleteObject(hBrushWhite);
- DeleteObject(hBrushRed);
- DeleteObject(hBrushYellow);
- DeleteObject(hBrushGreen);
- //delete regions
- DeleteObject(ellipse1);
- DeleteObject(ellipse2);
- DeleteObject(ellipse3);
- //delete timer
- KillTimer(hWnd, 1);
- PostQuitMessage(0);
- break;
- default:
- return DefWindowProc(hWnd, message, wParam, lParam);
- }
- return 0;
- }
- bool isOdd(int i){
- return i & 1;
- }
- void updateRegion(HRGN& hRgn, int left, int top, int right, int bottom) {
- DeleteObject(hRgn);
- hRgn = CreateEllipticRgn(left, top, right, bottom);
- }
Add Comment
Please, Sign In to add comment