Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <tchar.h>
- #include <Windows.h>
- #pragma comment(linker, "\"/manifestdependency:type='win32' \
- name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' \
- publicKeyToken='6595b64144ccf1df' language='*'\"")
- LRESULT CALLBACK WindowProcedure(HWND, UINT, WPARAM, LPARAM);
- void CALLBACK Move(HWND hWnd, int dx, int dy, bool center = false);
- INT WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR szCmdLine, INT nCmdShow)
- {
- TCHAR szClassWindow[] = TEXT("Win32Application");
- WNDCLASSEX wc = {};
- wc.cbClsExtra = 0;
- wc.cbSize = sizeof(wc);
- wc.cbWndExtra = 0;
- wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
- wc.hCursor = LoadCursor(NULL, IDC_ARROW);
- wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
- wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
- wc.hInstance = hInstance;
- wc.lpfnWndProc = WindowProcedure;
- wc.lpszClassName = szClassWindow;
- wc.lpszMenuName = NULL;
- wc.style = CS_HREDRAW | CS_VREDRAW;
- INT nExitCode = 0;
- if (RegisterClassEx(&wc))
- {
- HWND hWindow = CreateWindowEx(0, szClassWindow, TEXT("Поиграй со мной"),
- WS_OVERLAPPEDWINDOW, 250, 250, 200, 200, NULL,
- NULL, hInstance, NULL);
- ShowWindow(hWindow, nCmdShow);
- UpdateWindow(hWindow);
- MSG message = {};
- while (GetMessage(&message, NULL, 0, 0))
- {
- TranslateMessage(&message);
- DispatchMessage(&message);
- }
- nExitCode = message.wParam;
- }
- return nExitCode;
- }
- LRESULT CALLBACK WindowProcedure(HWND hWindow, UINT nMessage, WPARAM wParam, LPARAM lParam)
- {
- LRESULT result = 0;
- switch (nMessage)
- {
- case WM_CHAR:
- break;
- case WM_KEYDOWN:
- if (wParam == VK_ESCAPE)
- {
- ShowWindow(hWindow, SW_NORMAL);
- SetWindowPos(hWindow, NULL, 0, 0, CW_USEDEFAULT, CW_USEDEFAULT, 1);
- }
- else if (wParam == VK_LEFT)
- Move(hWindow, -50, 0);
- else if (wParam==VK_RIGHT)
- Move(hWindow, 50, 0);
- else if (wParam == VK_UP)
- Move(hWindow, 0, -50);
- else if (wParam == VK_DOWN)
- Move(hWindow, 0, 50);
- else if (wParam == VK_RETURN)
- ShowWindow(hWindow, SW_SHOWMAXIMIZED);
- else if (wParam == VK_SPACE)
- {
- Move(hWindow, 0, 0, true);
- }
- break;
- case WM_DESTROY:
- PostQuitMessage(0);
- break;
- default:
- result = DefWindowProc(hWindow, nMessage, wParam, lParam);
- break;
- }
- return result;
- }
- void CALLBACK Move(HWND hWnd, int dx, int dy, bool center)
- {
- int width = GetSystemMetrics(SM_CXSCREEN); // граница ширины экрана
- int height = GetSystemMetrics(SM_CYSCREEN); // граница высоты экрана
- RECT rect;
- GetWindowRect(hWnd, &rect);
- if (center == false)
- {
- if (rect.top <= 0 && dy < 0) dy = 0;
- else if (rect.bottom >= height&&dy>0)dy = 0;
- if (rect.left <= 0 && dx < 0) dx = 0;
- else if (rect.right >= width&&dx>0)dx = 0;
- dx += rect.left;
- dy += rect.top;
- SetWindowPos(hWnd, NULL, dx, dy, CW_USEDEFAULT, CW_USEDEFAULT, 1);
- }
- else
- SetWindowPos(hWnd, NULL, ((width/2)-((rect.right-rect.left))/2), ((height/2)-((rect.bottom-rect.top))/2), CW_USEDEFAULT, CW_USEDEFAULT, 1);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement