daily pastebin goal
42%
SHARE
TWEET

Untitled

a guest Oct 12th, 2017 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <windows.h>
  2. #include <string.h>
  3.  
  4. const char windowClass[] = "win32app";
  5. const char windowTitle[] = "Заголовок окна";
  6. const char stringToDraw[] = "ПКИМС правит миром!";
  7.  
  8. bool move_up = false;
  9. int X_WINDOW = 700, Y_WINDOW = 300;
  10. int BANG_TIMER = 250, WALL_TIMER = 50;
  11. int BANG_TIMER_ID = 38, WALL_TIMER_ID = 39;
  12. int r1x1 = NULL, r1y1 = NULL, r1x2 = NULL, r1y2 = NULL;
  13. int r2x1 = X_WINDOW - 10, r2y1 = 30, r2x2 = X_WINDOW, r2y2 = 70;
  14.  
  15. long __stdcall WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
  16.     PAINTSTRUCT ps;
  17.     int x_click, y_click;
  18.     HDC hdc;
  19.     HPEN pen, old_pen;
  20.  
  21.     RECT rect;
  22.     GetClientRect(hWnd, &rect);
  23.  
  24.     switch (message) {
  25.  
  26.     case WM_PAINT:
  27.         hdc = BeginPaint(hWnd, &ps);
  28.         pen = CreatePen(PS_DASH, 1, RGB(0, 0, 0));
  29.         old_pen = (HPEN)SelectObject(hdc, pen);
  30.         MoveToEx(hdc, X_WINDOW / 2, 0, NULL);
  31.         LineTo(hdc, X_WINDOW / 2, Y_WINDOW);
  32.         SelectObject(hdc, old_pen);
  33.         DeleteObject(pen);
  34.  
  35.         Rectangle(hdc, r2x1, r2y1, r2x2, r2y2);
  36.         Rectangle(hdc, r1x1, r1y1, r1x2, r1y2);
  37.  
  38.         EndPaint(hWnd, &ps);
  39.         break;
  40.     case WM_LBUTTONDOWN:
  41.         x_click = LOWORD(lParam);
  42.         y_click = HIWORD(lParam);
  43.         if (x_click < X_WINDOW / 2) {
  44.             r1x1 = x_click;
  45.             r1y1 = y_click;
  46.             r1x2 = x_click + 10;
  47.             r1y2 = y_click + 10;
  48.             InvalidateRect(hWnd, NULL, true);
  49.         }
  50.         break;
  51.     case WM_SIZE:
  52.         X_WINDOW = rect.right;
  53.         Y_WINDOW = rect.bottom;
  54.         r2x1 = X_WINDOW - 10;
  55.         r2x2 = X_WINDOW;
  56.  
  57.         if (r1y1 >= Y_WINDOW && r1y1 != NULL) {
  58.             r1x1 = NULL;
  59.             r1y1 = NULL;
  60.             r1x2 = NULL;
  61.             r1y2 = NULL;
  62.             MessageBox(hWnd, "Ooops! We lost the shot!", "Oooops!", NULL);
  63.         }
  64.         InvalidateRect(hWnd, NULL, true);
  65.         break;
  66.  
  67.     case WM_TIMER:
  68.         if (wParam == 38) {
  69.             if (r1x1 != NULL) {
  70.                 r1x1 = r1x1 + 10;
  71.                 r1x2 = r1x2 + 10;
  72.                 InvalidateRect(hWnd, NULL, true);
  73.             }
  74.         }
  75.         if (wParam == 39) {
  76.             if (move_up) {
  77.                 r2y1 = r2y1 - 10;
  78.                 r2y2 = r2y2 - 10;
  79.             }
  80.             else {
  81.                 r2y1 = r2y1 + 10;
  82.                 r2y2 = r2y2 + 10;
  83.             }
  84.  
  85.             if (r2y2 >= Y_WINDOW) {
  86.                 move_up = true;
  87.             }
  88.             if (r2y1 <= 0) {
  89.                 move_up = false;
  90.             }
  91.             InvalidateRect(hWnd, NULL, true);
  92.         }
  93.  
  94.         if (((r1x2 >= r2x1) && (r1x1 <= r2x2)) && ((r1y2 >= r2y1) && (r1y1 <= r2y2))) {
  95.             r1x1 = NULL;
  96.             r1y1 = NULL;
  97.             r1x2 = NULL;
  98.             r1y2 = NULL;
  99.             MessageBox(hWnd, "Bang", "Bang", NULL);
  100.         }
  101.  
  102.         if (r1x1 >= X_WINDOW) {
  103.             r1x1 = NULL;
  104.             r1y1 = NULL;
  105.             r1x2 = NULL;
  106.             r1y2 = NULL;
  107.         }
  108.         break;
  109.     case WM_DESTROY:
  110.         PostQuitMessage(0);
  111.         break;
  112.     default:
  113.         return DefWindowProc(hWnd, message, wParam, lParam);
  114.     }
  115.  
  116.     return 0;
  117. }
  118.  
  119.  
  120. int __stdcall WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
  121.     WNDCLASSEX wcex;
  122.  
  123.     wcex.cbSize = sizeof(WNDCLASSEX);
  124.     wcex.style = CS_HREDRAW | CS_VREDRAW;
  125.     wcex.lpfnWndProc = WndProc;
  126.     wcex.cbClsExtra = 0;
  127.     wcex.cbWndExtra = 0;
  128.     wcex.hInstance = hInstance;
  129.     wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
  130.     wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
  131.     wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
  132.     wcex.lpszMenuName = NULL;
  133.     wcex.lpszClassName = windowClass;
  134.     wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
  135.  
  136.     if (!RegisterClassEx(&wcex)) {
  137.         MessageBox(NULL, "Can’t register window class!", "Win32 API Test", NULL);
  138.         return 1;
  139.     }
  140.  
  141.     HWND hWnd = CreateWindow(windowClass, windowTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, X_WINDOW, Y_WINDOW, NULL, NULL, hInstance, NULL);
  142.  
  143.     if (!hWnd) {
  144.         MessageBox(NULL, "Can’t create window!", "Win32 API Test", NULL);
  145.         return 1;
  146.     }
  147.  
  148.     ShowWindow(hWnd, nCmdShow);
  149.  
  150.     SetTimer(hWnd, BANG_TIMER_ID, BANG_TIMER, NULL);
  151.     SetTimer(hWnd, WALL_TIMER_ID, WALL_TIMER, NULL);
  152.  
  153.     UpdateWindow(hWnd);
  154.     MSG msg;
  155.  
  156.     while (GetMessage(&msg, NULL, 0, 0)) {
  157.         TranslateMessage(&msg);
  158.         DispatchMessage(&msg);
  159.     }
  160.     KillTimer(hWnd, BANG_TIMER_ID);
  161.     KillTimer(hWnd, WALL_TIMER_ID);
  162.     return msg.wParam;
  163. }
RAW Paste Data
Top