Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // WinApiLAB2.cpp : Defines the entry point for the application.
- //
- #include "stdafx.h"
- #include "WinApiLAB2.h"
- #define w_width 400
- #define w_height 600
- #define MAX_LOADSTRING 100
- const int ScreenX = (GetSystemMetrics(SM_CXSCREEN) - w_width) / 2; //defines positions such that window will be in the middle
- const int ScreenY = (GetSystemMetrics(SM_CYSCREEN) - w_height) / 2;
- // Global Variables:
- HINSTANCE hInst; // current instance
- WCHAR szTitle[MAX_LOADSTRING]; // The title bar text
- WCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name
- HWND hMainWnd;
- HWND hBlockWnd;
- void UpdateBlock();
- struct Block {
- HWND BlockWnd;
- int size = 32;
- int x = 180;
- int y = 600;
- };
- Block block;
- void Jumping();
- bool jump = false;
- // Forward declarations of functions included in this code module:
- ATOM MyRegisterClass(HINSTANCE hInstance);
- ATOM MyRegisterBlockClass(HINSTANCE hInstance);
- BOOL InitInstance(HINSTANCE, int);
- LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
- LRESULT CALLBACK WndProcBlock(HWND, UINT, WPARAM, LPARAM);
- INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
- int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
- _In_opt_ HINSTANCE hPrevInstance,
- _In_ LPWSTR lpCmdLine,
- _In_ int nCmdShow)
- {
- UNREFERENCED_PARAMETER(hPrevInstance);
- UNREFERENCED_PARAMETER(lpCmdLine);
- // TODO: Place code here.
- // Initialize global strings
- LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
- LoadStringW(hInstance, IDC_WINAPILAB2, szWindowClass, MAX_LOADSTRING);
- MyRegisterClass(hInstance);
- MyRegisterBlockClass(hInstance);
- // Perform application initialization:
- if (!InitInstance(hInstance, nCmdShow))
- {
- return FALSE;
- }
- HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_WINAPILAB2));
- MSG msg;
- // Main message loop:
- while (GetMessage(&msg, nullptr, 0, 0))
- {
- if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- }
- return (int)msg.wParam;
- }
- //
- // FUNCTION: MyRegisterClass()
- //
- // PURPOSE: Registers the window class.
- //
- ATOM MyRegisterClass(HINSTANCE hInstance)
- {
- WNDCLASSEXW 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_WINAPILAB2));
- wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
- HBRUSH hb = CreateSolidBrush(RGB(0, 128, 255));
- wcex.hbrBackground = hb;
- wcex.lpszMenuName = MAKEINTRESOURCEW(IDC_WINAPILAB2);
- wcex.lpszClassName = szWindowClass;
- wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
- return RegisterClassExW(&wcex);
- }
- ATOM MyRegisterBlockClass(HINSTANCE hInstance)
- {
- WNDCLASSEXW 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_WINAPILAB2));
- wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
- HBRUSH hb = CreateSolidBrush(RGB(255, 0, 0));
- wcex.hbrBackground = hb;
- wcex.lpszMenuName = MAKEINTRESOURCEW(IDC_WINAPILAB2);
- wcex.lpszClassName = L"BlockWnd";
- wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
- return RegisterClassExW(&wcex);
- }
- //
- // FUNCTION: InitInstance(HINSTANCE, int)
- //
- // PURPOSE: Saves instance handle and creates main window
- //
- // COMMENTS:
- //
- // In this function, we save the instance handle in a global variable and
- // create and display the main program window.
- //
- BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
- {
- hInst = hInstance; // Store instance handle in our global variable
- hMainWnd = CreateWindowW(szWindowClass, L"IcyTower", WS_OVERLAPPEDWINDOW ^ WS_THICKFRAME ^ WS_MAXIMIZEBOX | WS_CLIPCHILDREN, //WS_OVERLAPPED | WS_MINIMIZEBOX | WS_SYSMENU,
- ScreenX, ScreenY, w_width, w_height, nullptr, nullptr, hInstance, nullptr);
- SetWindowPos(hMainWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
- if (!hMainWnd)
- {
- return FALSE;
- }
- ShowWindow(hMainWnd, nCmdShow);
- UpdateWindow(hMainWnd);
- hBlockWnd = CreateWindow(L"BlockWnd", L"Block",
- WS_CHILD,
- 180, 510, 32,32,
- hMainWnd, NULL, hInstance, NULL);
- if (!hBlockWnd)
- {
- return FALSE;
- }
- SetWindowRgn(hBlockWnd, CreateRectRgn(0, 0, block.size, block.size), true);
- ShowWindow(hBlockWnd, nCmdShow);
- UpdateWindow(hBlockWnd);
- return TRUE;
- }
- //
- // FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
- //
- // PURPOSE: Processes messages for the main window.
- //
- // WM_COMMAND - process the application menu
- // WM_PAINT - Paint the main window
- // WM_DESTROY - post a quit message and return
- //
- //
- LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
- {
- switch (message)
- {
- case WM_COMMAND:
- {
- int wmId = LOWORD(wParam);
- // Parse the menu selections:
- switch (wmId)
- {
- case IDM_ABOUT:
- DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
- break;
- case IDM_EXIT:
- DestroyWindow(hWnd);
- break;
- default:
- return DefWindowProc(hWnd, message, wParam, lParam);
- }
- }
- break;
- case WM_SETFOCUS: {SetWindowLong(hWnd, GWL_EXSTYLE, GetWindowLong(hWnd, GWL_EXSTYLE) |
- WS_EX_LAYERED);
- // Make this window 50% alpha
- SetLayeredWindowAttributes(hWnd, 0, (500 * 50) / 100, LWA_ALPHA); }break;
- case WM_KILLFOCUS: { SetWindowLong(hWnd, GWL_EXSTYLE, GetWindowLong(hWnd, GWL_EXSTYLE) |
- WS_EX_LAYERED);
- // Make this window 50% alpha
- SetLayeredWindowAttributes(hWnd, 0, (255 * 50) / 100, LWA_ALPHA); }break;
- case WM_PAINT:
- {
- PAINTSTRUCT ps;
- HDC hdc = BeginPaint(hWnd, &ps);
- // TODO: Add any drawing code that uses hdc here...
- EndPaint(hWnd, &ps);
- }
- break;
- case WM_KEYDOWN: {
- if (wParam == 65) // A
- {
- block.x -= 20;
- UpdateBlock();
- }
- if (wParam == 87) //W
- {
- jump = true;
- }
- if (wParam == 68) // D
- {
- block.x += 20;
- UpdateBlock();
- }
- }break;
- case WM_CREATE: {
- SetTimer(hMainWnd, NULL, 10, NULL);
- }break;
- case WM_TIMER: {
- Jumping();
- }break;
- case WM_DESTROY:
- PostQuitMessage(0);
- break;
- default:
- return DefWindowProc(hWnd, message, wParam, lParam);
- }
- return 0;
- }
- // Message handler for about box.
- INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
- {
- UNREFERENCED_PARAMETER(lParam);
- switch (message)
- {
- case WM_INITDIALOG:
- return (INT_PTR)TRUE;
- case WM_COMMAND:
- if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
- {
- EndDialog(hDlg, LOWORD(wParam));
- return (INT_PTR)TRUE;
- }
- break;
- }
- return (INT_PTR)FALSE;
- }
- LRESULT CALLBACK WndProcBlock(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
- {
- RECT rc;
- static int X, Y;
- switch (message)
- {
- case WM_PAINT:
- {
- PAINTSTRUCT ps;
- HDC hdc = BeginPaint(hWnd, &ps);
- EndPaint(hWnd, &ps);
- }
- break;
- case WM_DESTROY:
- PostQuitMessage(0);
- break;
- default:
- return DefWindowProc(hWnd, message, wParam, lParam);
- }
- return 0;
- return 0;
- }
- void UpdateBlock() {
- if (block.x < 0)
- block.x = 0;
- if (block.x > 400 - 15- block.size)
- block.x = 400 - 15 - block.size;
- if (block.y < 0)
- block.y = 0;
- if (block.y > 542 - block.size)
- block.y = 542 - block.size;
- MoveWindow(hBlockWnd, block.x, block.y, block.size, block.size, true);
- UpdateWindow(hBlockWnd);
- SetLayeredWindowAttributes(hMainWnd, 0, (255 * 100) / 100, LWA_ALPHA);
- }
- void Jumping() {
- if (jump == true) {
- if (block.y == 510)
- {
- block.y -= 100;
- UpdateBlock();
- jump = false;
- }
- if (block.y != 510) {
- block.y += 5;
- UpdateBlock();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement