Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Windows.h>
- #define MAX_LENGTH 255
- #define BTN_WIDTH 60
- #define BTN_HEIGHT 60
- #define ID_BTN_DRAW 1
- #define ID_BTN_CLEAR 2
- LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
- HDC hDC;
- PAINTSTRUCT ps;
- RECT rectWindow, rectText;
- HINSTANCE hInst;
- bool startDraw = false, clear = false;
- int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nCmdShow)
- {
- TCHAR szClassName[] = L"Мой класс";
- MSG msg;
- HWND hWnd;
- WNDCLASSEX wc;
- hInst = hInstance;
- wc.cbSize = sizeof(wc);
- wc.style = CS_HREDRAW | CS_VREDRAW;
- wc.lpfnWndProc = WndProc;
- wc.lpszMenuName = NULL;
- wc.lpszClassName = szClassName;
- wc.cbWndExtra = NULL;
- wc.cbClsExtra = NULL;
- wc.hIcon = LoadIcon(NULL, IDC_ICON);
- wc.hIconSm = LoadIcon(NULL, IDC_ICON);
- wc.hCursor = LoadCursor(NULL, IDC_ARROW);
- wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
- wc.hInstance = hInstance;
- if (!RegisterClassEx(&wc))
- {
- MessageBox(NULL, L"Не получилось зарегистрировать класс!", L"Ошибка", MB_OK);
- return NULL;
- }
- hWnd = CreateWindow(
- szClassName,
- L"Полноценная оконная процедура",
- WS_OVERLAPPEDWINDOW,
- 600,
- 150,
- 505,
- 460,
- (HWND)NULL,
- NULL,
- hInstance,
- NULL);
- if (!hWnd)
- {
- MessageBox(NULL, L"Не получилось создать окно!", L"Ошибка", MB_OK);
- return NULL;
- }
- ShowWindow(hWnd, nCmdShow);
- UpdateWindow(hWnd);
- while (GetMessage(&msg, NULL, NULL, NULL))
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- return msg.wParam;
- }
- LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
- {
- static HWND hBtnDraw, hBtnClear;
- static HPEN hPenRed, hPenGreen;
- static int cxClient, cyClient;
- LPDRAWITEMSTRUCT pDIS;
- POINT pt5[5];
- PAINTSTRUCT ps;
- HDC hdc;
- RECT rect;
- switch (uMsg)
- {
- case WM_CREATE:
- hBtnDraw = CreateWindow(
- L"button",
- L"",
- WS_CHILD | WS_VISIBLE | BS_OWNERDRAW,
- 10,
- 10,
- BTN_WIDTH,
- BTN_HEIGHT,
- hWnd,
- (HMENU)ID_BTN_DRAW,
- hInst,
- NULL);
- hBtnClear = CreateWindow(
- L"button",
- L"",
- WS_CHILD | WS_VISIBLE | BS_OWNERDRAW,
- 10,
- 80,
- BTN_WIDTH,
- BTN_HEIGHT,
- hWnd,
- (HMENU)ID_BTN_CLEAR,
- hInst,
- NULL);
- startDraw = false;
- hPenRed = CreatePen(PS_SOLID, 2, RGB(255, 0, 0));
- break;
- case WM_SIZE:
- cxClient = LOWORD(lParam);
- cyClient = HIWORD(lParam);
- return 0;
- case WM_PAINT:
- hdc = BeginPaint(hWnd, &ps);
- if (startDraw)
- {
- SetViewportOrgEx(hdc, 80, 10, NULL);
- Rectangle(hdc, 0, 0, 400, 400); //frame
- Rectangle(hdc, 100, 220, 220, 280); //engine
- Ellipse(hdc, 80, 280, 120, 320); //whell
- Ellipse(hdc, 260, 280, 300, 320); //whell
- Rectangle(hdc, 220, 140, 300, 280); //cabine
- pt5[0].x = 240; pt5[0].y = 160;
- pt5[1].x = 280; pt5[1].y = 160;
- pt5[2].x = 280; pt5[2].y = 220;
- pt5[3].x = 240; pt5[3].y = 220;
- pt5[4].x = 240; pt5[4].y = 160;
- Polyline(hdc, pt5, 5); //window
- pt5[0].x = 120; pt5[0].y = 230;
- pt5[1].x = 200; pt5[1].y = 230;
- pt5[2].x = 200; pt5[2].y = 250;
- pt5[3].x = 120; pt5[3].y = 250;
- pt5[4].x = 120; pt5[4].y = 230;
- Polyline(hdc, pt5, 5); //engine
- MoveToEx(hdc, 140, 250, NULL);
- LineTo(hdc, 140, 230);
- MoveToEx(hdc, 160, 250, NULL);
- LineTo(hdc, 160, 230);
- MoveToEx(hdc, 180, 250, NULL);
- LineTo(hdc, 180, 230);
- //антены
- Arc(hdc, 240, 100, 360, 180, 320, 100, 260, 140);
- Arc(hdc, 260, 100, 380, 180, 340, 100, 280, 140);
- //литье
- MoveToEx(hdc, 260, 300, NULL);
- LineTo(hdc, 300, 300);
- MoveToEx(hdc, 80, 300, NULL);
- LineTo(hdc, 120, 300);
- MoveToEx(hdc, 100, 280, NULL);
- LineTo(hdc, 100, 320);
- MoveToEx(hdc, 280, 280, NULL);
- LineTo(hdc, 280, 320);
- startDraw = false;
- }
- if (clear)
- {
- SelectObject(hdc, CreatePen(BS_SOLID, 0, RGB(255, 255, 255)));
- Rectangle(hdc, 75, 5, 600, 600);
- clear = false;
- }
- EndPaint(hWnd, &ps);
- break;
- case WM_DESTROY:
- PostQuitMessage(NULL);
- break;
- case WM_COMMAND:
- switch (LOWORD(wParam))
- {
- case ID_BTN_DRAW:
- startDraw = true;
- InvalidateRect(hWnd, NULL, true);
- break;
- case ID_BTN_CLEAR:
- clear = true;
- InvalidateRect(hWnd, NULL, true);
- break;
- }
- break;
- case WM_DRAWITEM:
- pDIS = (LPDRAWITEMSTRUCT)lParam;
- switch (pDIS->CtlID)
- {
- case ID_BTN_DRAW:
- FillRect(pDIS->hDC, &pDIS->rcItem, CreateSolidBrush(RGB(150, 150, 150)));
- FrameRect(pDIS->hDC, &pDIS->rcItem, CreateSolidBrush(RGB(0, 0, 0)));
- SelectObject(pDIS->hDC, CreatePen(PS_SOLID, 1, RGB(0, 255, 0)));
- SelectObject(pDIS->hDC, CreateSolidBrush(RGB(255, 0, 0)));
- Ellipse(pDIS->hDC, 20, 20, 40, 40);
- break;
- case ID_BTN_CLEAR:
- FillRect(pDIS->hDC, &pDIS->rcItem, CreateSolidBrush(RGB(255, 255, 255)));
- FrameRect(pDIS->hDC, &pDIS->rcItem, CreateSolidBrush(RGB(0, 0, 0)));
- SelectObject(pDIS->hDC, CreatePen(PS_SOLID, 1, RGB(0, 0, 0)));
- SelectObject(pDIS->hDC, CreateSolidBrush(RGB(150, 150, 150)));
- pt5[0].x = 10; pt5[0].y = 10;
- pt5[1].x = 20; pt5[1].y = 50;
- pt5[2].x = 40; pt5[2].y = 50;
- pt5[3].x = 50; pt5[3].y = 10;
- Polygon(pDIS->hDC, pt5, 4);
- break;
- }
- break;
- default:
- return DefWindowProc(hWnd, uMsg, wParam, lParam);
- }
- return NULL;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement