Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <windows.h>
- #include <string.h>
- const char windowClass[] = "win32app";
- const char windowTitle[] = "Заголовок окна";
- const char stringToDraw[] = "ПКИМС правит миром!";
- bool move_up = false;
- int X_WINDOW = 700, Y_WINDOW = 300;
- int BANG_TIMER = 250, WALL_TIMER = 50;
- int BANG_TIMER_ID = 38, WALL_TIMER_ID = 39;
- int r1x1 = NULL, r1y1 = NULL, r1x2 = NULL, r1y2 = NULL;
- int r2x1 = X_WINDOW - 10, r2y1 = 30, r2x2 = X_WINDOW, r2y2 = 70;
- long __stdcall WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
- PAINTSTRUCT ps;
- int x_click, y_click;
- HDC hdc;
- HPEN pen, old_pen;
- RECT rect;
- GetClientRect(hWnd, &rect);
- switch (message) {
- case WM_PAINT:
- hdc = BeginPaint(hWnd, &ps);
- pen = CreatePen(PS_DASH, 1, RGB(0, 0, 0));
- old_pen = (HPEN)SelectObject(hdc, pen);
- MoveToEx(hdc, X_WINDOW / 2, 0, NULL);
- LineTo(hdc, X_WINDOW / 2, Y_WINDOW);
- SelectObject(hdc, old_pen);
- DeleteObject(pen);
- Rectangle(hdc, r2x1, r2y1, r2x2, r2y2);
- Rectangle(hdc, r1x1, r1y1, r1x2, r1y2);
- EndPaint(hWnd, &ps);
- break;
- case WM_LBUTTONDOWN:
- x_click = LOWORD(lParam);
- y_click = HIWORD(lParam);
- if (x_click < X_WINDOW / 2) {
- r1x1 = x_click;
- r1y1 = y_click;
- r1x2 = x_click + 10;
- r1y2 = y_click + 10;
- InvalidateRect(hWnd, NULL, true);
- }
- break;
- case WM_SIZE:
- X_WINDOW = rect.right;
- Y_WINDOW = rect.bottom;
- r2x1 = X_WINDOW - 10;
- r2x2 = X_WINDOW;
- if (r1y1 >= Y_WINDOW && r1y1 != NULL) {
- r1x1 = NULL;
- r1y1 = NULL;
- r1x2 = NULL;
- r1y2 = NULL;
- MessageBox(hWnd, "Ooops! We lost the shot!", "Oooops!", NULL);
- }
- InvalidateRect(hWnd, NULL, true);
- break;
- case WM_TIMER:
- if (wParam == 38) {
- if (r1x1 != NULL) {
- r1x1 = r1x1 + 10;
- r1x2 = r1x2 + 10;
- InvalidateRect(hWnd, NULL, true);
- }
- }
- if (wParam == 39) {
- if (move_up) {
- r2y1 = r2y1 - 10;
- r2y2 = r2y2 - 10;
- }
- else {
- r2y1 = r2y1 + 10;
- r2y2 = r2y2 + 10;
- }
- if (r2y2 >= Y_WINDOW) {
- move_up = true;
- }
- if (r2y1 <= 0) {
- move_up = false;
- }
- InvalidateRect(hWnd, NULL, true);
- }
- if (((r1x2 >= r2x1) && (r1x1 <= r2x2)) && ((r1y2 >= r2y1) && (r1y1 <= r2y2))) {
- r1x1 = NULL;
- r1y1 = NULL;
- r1x2 = NULL;
- r1y2 = NULL;
- MessageBox(hWnd, "Bang", "Bang", NULL);
- }
- if (r1x1 >= X_WINDOW) {
- r1x1 = NULL;
- r1y1 = NULL;
- r1x2 = NULL;
- r1y2 = NULL;
- }
- break;
- case WM_DESTROY:
- PostQuitMessage(0);
- break;
- default:
- return DefWindowProc(hWnd, message, wParam, lParam);
- }
- return 0;
- }
- int __stdcall WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
- WNDCLASSEX wcex;
- wcex.cbSize = sizeof(WNDCLASSEX);
- wcex.style = CS_HREDRAW | CS_VREDRAW;
- wcex.lpfnWndProc = WndProc;
- wcex.cbClsExtra = 0;
- wcex.cbWndExtra = 0;
- wcex.hInstance = hInstance;
- wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
- wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
- wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
- wcex.lpszMenuName = NULL;
- wcex.lpszClassName = windowClass;
- wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
- if (!RegisterClassEx(&wcex)) {
- MessageBox(NULL, "Can’t register window class!", "Win32 API Test", NULL);
- return 1;
- }
- HWND hWnd = CreateWindow(windowClass, windowTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, X_WINDOW, Y_WINDOW, NULL, NULL, hInstance, NULL);
- if (!hWnd) {
- MessageBox(NULL, "Can’t create window!", "Win32 API Test", NULL);
- return 1;
- }
- ShowWindow(hWnd, nCmdShow);
- SetTimer(hWnd, BANG_TIMER_ID, BANG_TIMER, NULL);
- SetTimer(hWnd, WALL_TIMER_ID, WALL_TIMER, NULL);
- UpdateWindow(hWnd);
- MSG msg;
- while (GetMessage(&msg, NULL, 0, 0)) {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- KillTimer(hWnd, BANG_TIMER_ID);
- KillTimer(hWnd, WALL_TIMER_ID);
- return msg.wParam;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement