Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <windows.h>
- #include <string.h>
- #include <stdio.h>
- #include <math.h>
- // Global variable
- HINSTANCE hInst;
- UINT MessageCount = 0;
- UINT Count = 0;
- int posX = 0;
- int posY = 0;
- int pom = 0;
- // Function prototypes.
- int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int);
- LRESULT CALLBACK MainWndProc(HWND, UINT, WPARAM, LPARAM);
- void paintObject(HWND hWnd, HDC hDC, PAINTSTRUCT ps, int posX, int posY, POINT cursorPosition);
- void paintPosition(HWND hWnd, HDC hDC, PAINTSTRUCT ps);
- // Application entry point. This is the same as main() in standart C.
- int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
- {
- MSG msg;
- BOOL bRet;
- WNDCLASS wcx; // register class
- HWND hWnd;
- hInst = hInstance; // Save the application-instance handle.
- // Fill in the window class structure with parameters that describe the main window.
- wcx.style = CS_HREDRAW | CS_VREDRAW; // redraw if size changes
- wcx.lpfnWndProc = (WNDPROC)MainWndProc; // points to window procedure
- wcx.cbClsExtra = 0; // no extra class memory
- wcx.cbWndExtra = 0; // no extra window memory
- wcx.hInstance = hInstance; // handle to instance
- wcx.hIcon = LoadIcon(NULL, IDI_APPLICATION); // predefined app. icon
- wcx.hCursor = LoadCursor(NULL, IDC_ARROW); // predefined arrow
- wcx.hbrBackground = GetStockObject(WHITE_BRUSH); // white background brush
- wcx.lpszMenuName = (LPCSTR)"MainMenu"; // name of menu resource
- wcx.lpszClassName = (LPCSTR)"MainWClass"; // name of window class
- // Register the window class.
- if (!RegisterClass(&wcx)) return FALSE;
- // create window of registered class
- hWnd = CreateWindow(
- "MainWClass", // name of window class
- "ITU", // title-bar string
- WS_OVERLAPPEDWINDOW, // top-level window
- 200, // default horizontal position
- 25, // default vertical position
- 1000, // default width
- 700, // default height
- (HWND)NULL, // no owner window
- (HMENU)NULL, // use class menu
- hInstance, // handle to application instance
- (LPVOID)NULL); // no window-creation data
- if (!hWnd) return FALSE;
- // Show the window and send a WM_PAINT message to the window procedure.
- // Record the current cursor position.
- ShowWindow(hWnd, nCmdShow);
- UpdateWindow(hWnd);
- // loop of message processing
- while ((bRet = GetMessage(&msg, NULL, 0, 0)) != 0)
- {
- if (bRet == -1)
- {
- // handle the error and possibly exit
- }
- else
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- }
- return (int)msg.wParam;
- }
- LRESULT CALLBACK MainWndProc(
- HWND hWnd, // handle to window
- UINT uMsg, // message identifier
- WPARAM wParam, // first message parameter
- LPARAM lParam) // second message parameter
- {
- HDC hDC;
- PAINTSTRUCT ps;
- POINT cursorPosition;
- HWND hwndTmp; // window handle where mouse button was released
- // init cursor position
- GetCursorPos(&cursorPosition);
- ScreenToClient(hWnd, &cursorPosition);
- switch (uMsg)
- {
- case WM_CREATE:
- break;
- // character input
- case WM_CHAR:
- switch (wParam) {
- case 0x08: // backspace
- case 0x0A: // linefeed
- case 0x1B: // escape
- break;
- case 0x09: // tab
- break;
- default:
- break;
- }
- break;
- // key input
- case WM_KEYDOWN:
- switch (wParam) {
- // update posX and posY in order to move object
- case VK_LEFT: // left arrow
- posX -= 10;
- InvalidateRect(hWnd, NULL, TRUE);
- break;
- case VK_RIGHT: // right arrow
- posX += 10;
- InvalidateRect(hWnd, NULL, TRUE);
- break;
- case VK_UP: // up arrow
- posY -= 10;
- InvalidateRect(hWnd, NULL, TRUE);
- break;
- case VK_DOWN: // down arrow
- posY += 10;
- InvalidateRect(hWnd, NULL, TRUE);
- break;
- // react on the other pressed keys
- case VK_SPACE: // space
- pom = 1;
- InvalidateRect(hWnd, NULL, TRUE);
- break;
- case VK_BACK: // backspace
- break;
- case VK_TAB: // tab
- break;
- // more virtual codes can be found here: https://docs.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes
- }
- break;
- // get cursor position
- case WM_MOUSEMOVE:
- GetCursorPos(&cursorPosition);
- ScreenToClient(hWnd, &cursorPosition);
- InvalidateRect(hWnd, NULL, TRUE);
- break;
- // react on mouse clicks
- case WM_LBUTTONDOWN:
- SetCapture(hWnd);
- InvalidateRect(hWnd, NULL, TRUE);
- break;
- case WM_LBUTTONUP:
- cursorPosition.x = (int)LOWORD(lParam);
- cursorPosition.y = (int)HIWORD(lParam);
- ClientToScreen(hWnd, &cursorPosition);
- hwndTmp = WindowFromPoint(cursorPosition);
- ReleaseCapture();
- break;
- // paint objects
- case WM_PAINT:
- hDC = BeginPaint(hWnd, &ps);
- paintObject(hWnd, hDC, ps, posX, posY, cursorPosition);
- paintPosition(hWnd, hDC, ps);
- // paint other objects
- // paintObject2(hWnd, hDC, ps, posX, posY, cursorPosition);
- // paintObject3(hWnd, hDC, ps, posX, posY, cursorPosition);
- EndPaint(hWnd, &ps);
- DeleteDC(hDC);
- break;
- //
- // Process other messages.
- //
- default:
- return DefWindowProc(hWnd, uMsg, wParam, lParam);
- }
- }
- void paintObject(HWND hWnd, HDC hDC, PAINTSTRUCT ps, int posX, int posY, POINT cursorPosition)
- {
- GetCursorPos(&cursorPosition);
- HBRUSH brush, brush2,brush3, oldbrush, oldbrush2, oldbrush3;
- brush = CreateSolidBrush(RGB(0, 100, 255));
- brush2 = CreateSolidBrush(RGB(0, 255, 0));
- brush3 = CreateSolidBrush(RGB(0, 0, 0));
- oldbrush = SelectObject(hDC, brush);
- oldbrush2 = SelectObject(hDC, brush2);
- oldbrush3 = SelectObject(hDC, brush3);
- if (pom) {
- brush2 = CreateSolidBrush(RGB(
- (BYTE)(rand() % 255), // red component of color
- (BYTE)(rand() % 255), // green component of color
- (BYTE)(rand() % 255) // blue component of color
- ));
- oldbrush2 = SelectObject(hDC, brush2);
- SelectObject(hDC, oldbrush2);
- pom = 0;
- }
- Ellipse(hDC, posX + 330, posY + 120, posX + 270, posY + 150);
- Ellipse(hDC, posX + 310, posY + 150, posX + 290, posY + 290);
- MoveToEx(hDC, posX + 300, posY + 200, NULL);
- LineTo(hDC, posX + 400, posY + 150);
- MoveToEx(hDC, posX + 300, posY + 200, NULL);
- LineTo(hDC, posX + 200, posY + 150);
- MoveToEx(hDC, posX + 300, posY + 290, NULL);
- LineTo(hDC, posX + 280, posY + 400);
- MoveToEx(hDC, posX + 300, posY + 290, NULL);
- LineTo(hDC, posX + 320, posY + 400);
- SelectObject(hDC, oldbrush2);
- Ellipse(hDC, cursorPosition.x + 100, cursorPosition.y + 100, cursorPosition.x + 200, cursorPosition.y + 200);
- Ellipse(hDC, cursorPosition.x + 400, cursorPosition.y + 100, cursorPosition.x + 500, cursorPosition.y + 200);
- SelectObject(hDC, oldbrush3);
- Ellipse(hDC, cursorPosition.x + 50, cursorPosition.y + 0, cursorPosition.x + 550, cursorPosition.y + 150);
- MoveToEx(hDC, cursorPosition.x + 250, cursorPosition.y + 20, NULL);
- LineTo(hDC, cursorPosition.x + 280, cursorPosition.y - 50);
- MoveToEx(hDC, cursorPosition.x + 310, cursorPosition.y + 20, NULL);
- LineTo(hDC, cursorPosition.x + 280, cursorPosition.y - 50);
- MoveToEx(hDC, cursorPosition.x + 280, cursorPosition.y - 50, NULL);
- LineTo(hDC, cursorPosition.x + 280, cursorPosition.y - 150);
- MoveToEx(hDC, cursorPosition.x + 250, cursorPosition.y -100, NULL);
- LineTo(hDC, cursorPosition.x + 310, cursorPosition.y - 100);
- Ellipse(hDC, cursorPosition.x + 270, cursorPosition.y -150, cursorPosition.x + 290, cursorPosition.y - 120);
- return;
- }
- void paintPosition(HWND hWnd, HDC hDC, PAINTSTRUCT ps)
- {
- char text[40]; // buffer to store an output text
- HFONT font; // new large font
- HFONT oldFont; // saves the previous font
- font = CreateFont(25, 0, 0, 0, 0, FALSE, FALSE, FALSE, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, FF_DONTCARE, 0);
- oldFont = (HFONT)SelectObject(hDC, font);
- sprintf(text, "Position -- x:%d, y:%d", posX, posY);
- TextOut(hDC, 50, 600, text, (int)strlen(text));
- SelectObject(hDC, oldFont);
- DeleteObject(font);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement