📜 ⬆️ ⬇️

Drawing under Windows in C ++, or “Guys, I'm crazy too!” (30+ lines of code)

Recently, Habr simply replete with "30-line posts":

- Gonochka on JavaScript (30 lines of code)
- Tiny Arkanoid in JavaScript (30 lines of code)
- Tiny Excel in pure javascript (30 lines of code)
- Tiny Snake JavaScript (30 lines of code)
- Tetris in javascript (in 30+ lines)

Obsessed with the desire to follow fashion, I also decided to write something. But since my relationship with JS is complex, I decided to write in C ++ using the API that Windows OS (in the people called WinAPI ) so kindly provided me.
')
So, closer to the body of the case:

C ++ code (31 lines):
#include <Windows.h> #include <vector> #include "resource.h" BOOL CALLBACK PaintProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam){ POINT t; static std::vector<POINT> pos; HDC hdc; static bool status; PAINTSTRUCT pt; switch(uMsg) { case WM_INITDIALOG: status = false; return TRUE; break; case WM_PAINT: hdc = BeginPaint(hwnd, &pt); (!pos.size()) ? NULL : MoveToEx(hdc, pos[0].x, pos[0].y, NULL); for(size_t i=0;i<pos.size();++i) (pos[i].x==-1 && i<pos.size()-1) ? MoveToEx(hdc, pos[i+1].x, pos[i+1].y, NULL) : LineTo(hdc, pos[i].x, pos[i].y); EndPaint(hwnd, &pt); break; case WM_LBUTTONDOWN: status = true; break; case WM_LBUTTONUP: status = false; tx=-1; pos.push_back(t); break; case WM_MOUSEMOVE: GetCursorPos(&t); ScreenToClient (hwnd, &t); (status) ? pos.push_back(t) : NULL; (status) ? InvalidateRect(hwnd, NULL, true) : NULL; break; case WM_CLOSE: EndDialog(hwnd, 0); break; } return FALSE; } int WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR lpCmdLine, int nCmdShow) { DialogBox(hInst, MAKEINTRESOURCE(IDD_PAINT), NULL, PaintProc); return 0; } 


Since we are measured by the basic code, without taking into account the “layout”, I do not take into account the description of the dialogue as a resource, but for those who want to reproduce this example, it is necessary to create a dialogue with ID - IDD_PAINT .

Actually, for the sake of which it all began:

image

Special features


- 31 line C ++ shit code
- Libraries used: STL

disadvantages


- Screen flickering with direct drawing (double buffering was omitted due to string savings).
- In the real task of writing a program (ala Paint ), using the vector container may not be the best option, but still.

Total


It agree, it is a little wrong to stack the declaration of variables in one line. But I think that this will not affect the overall impression of the code.
Thank you all for your attention. +1 to the total number of "abnormal."

Source: https://habr.com/ru/post/202678/


All Articles