-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
203 lines (172 loc) · 7.03 KB
/
main.cpp
File metadata and controls
203 lines (172 loc) · 7.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#ifndef _UNICODE
#define _UNICODE
#endif
#ifndef UNICODE
#define UNICODE
#endif
#include <windows.h>
#include <gdiplus.h>
#include <string>
#include <vector>
#include "TableCell.h"
using namespace std;
using namespace Gdiplus;
const SIZE MIN_WINDOW_SIZE = SIZE{400, 350};
const SIZE FIRST_WINDOW_SIZE = SIZE{800, 600};
const COLORREF BACKGROUND_COLOR = RGB(255, 255, 255);
const INT COL_COUNT = 6;
vector<wstring> strings;
vector<TableCell> getCellsPositions(Graphics *graphics, const Font &font, RectF drawRect) {
REAL cellWidth = drawRect.Width / (REAL) COL_COUNT;
vector<TableCell> cells = {};
for (const wstring &string: strings) {
TableCell cell = TableCell(graphics, string, font, cellWidth, drawRect.Height);
cells.emplace_back(cell);
}
int emptyCellsCount = (int)strings.size() % COL_COUNT;
for (int i = 0; i < emptyCellsCount; ++i) {
TableCell cell = TableCell(graphics, L"", font, cellWidth, drawRect.Height);
cells.emplace_back(cell);
}
int rowCount = (int)cells.size() / COL_COUNT;
REAL rowHeights[rowCount];
for (int i = 0; i < rowCount; ++i) {
REAL maxHeight = 0;
for (int j = 0; j < COL_COUNT; ++j) {
REAL height = cells[i * COL_COUNT + j].bounds.Height;
if (height > maxHeight) {
maxHeight = height;
}
}
rowHeights[i] = maxHeight;
for (int j = 0; j < COL_COUNT; ++j) {
cells[i * COL_COUNT + j].bounds.Height = rowHeights[i];
}
REAL offsetY = 0;
for (int q = 0; q < i; ++q) {
offsetY += rowHeights[q];
}
for (int j = 0; j < COL_COUNT; ++j) {
cells[i * COL_COUNT + j].bounds.X = drawRect.X + cellWidth * (REAL)j;
cells[i * COL_COUNT + j].bounds.Y = drawRect.Y + offsetY;
}
}
return cells;
}
void drawCells(Graphics *graphics, const vector<TableCell> &cells, const Font &font) {
for (const TableCell &cell: cells) {
Pen pen(Color::Black, 2);
SolidBrush solidBrush(Color::Black);
graphics->DrawString(cell.string.c_str(), -1, &font, cell.bounds, nullptr, &solidBrush);
graphics->DrawRectangle(&pen, cell.bounds);
}
}
void draw(Gdiplus::Graphics *graphics, Gdiplus::RectF drawRect) {
if (!strings.empty()) {
FontFamily fontFamily = Gdiplus::FontFamily(L"Comic Sans MS");
REAL fontSize = 12;
FontStyle fontStyle = Gdiplus::FontStyleBold;
Unit fontUnit = Gdiplus::UnitPoint;
Font font = Gdiplus::Font(&fontFamily, fontSize, fontStyle, fontUnit);
drawCells(graphics, getCellsPositions(graphics, font, drawRect), font);
}
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow) {
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, nullptr);
strings.emplace_back(L"Rattlesnake, rattlesnake\nRattlesnake, rattles me");
strings.emplace_back(L"Rattlesnake, rattlesnake\nRattlesnake, rattles me");
strings.emplace_back(L"Rattlesnake, rattlesnake\nRattlesnake, rattles me");
strings.emplace_back(L"Rattlesnake, rattlesnake\nRattlesnake, rattles me");
strings.emplace_back(L"Isolation\nTrepidation\nDon't fear nothing\nSnake is bluffing");
strings.emplace_back(L"Whips his tail\nSends you running");
strings.emplace_back(L"Rattlesnake, rattlesnake\nRattlesnake, rattles me");
strings.emplace_back(L"Rattlesnake, rattlesnake\nRattlesnake, rattles me");
strings.emplace_back(L"Rattlesnake, rattlesnake\nRattlesnake, rattles me");
strings.emplace_back(L"Rattlesnake, rattlesnake\nRattlesnake, rattles me");
strings.emplace_back(L"Vegetation\nAggravation\nFound him hiding\nSnake is smiling");
strings.emplace_back(L"Whips his tail\nLeaves you riling");
strings.emplace_back(L"Rattlesnake, rattlesnake\nRattlesnake, rattles me");
strings.emplace_back(L"Rattlesnake, rattlesnake\nRattlesnake, rattles me");
strings.emplace_back(L"Rattlesnake, rattlesnake\nRattlesnake, rattles me");
strings.emplace_back(L"Rattlesnake, rattlesnake\nRattlesnake, rattles me");
strings.emplace_back(L"Hibernation\nAltercation\nDon't get angry\nSnake is cranky");
strings.emplace_back(L"Whips his tail\nIn a frenzy");
strings.emplace_back(L"Rattlesnake, rattlesnake\nRattlesnake, rattles me");
strings.emplace_back(L"Rattlesnake, rattlesnake\nRattlesnake, rattles me");
strings.emplace_back(L"Rattlesnake, rattlesnake\nRattlesnake, rattles me");
strings.emplace_back(L"Rattlesnake, rattlesnake\nRattlesnake, rattles me");
const wchar_t WINDOW_CLASS[] = L"MAIN_WINDOW_CLASS";
const wchar_t WINDOW_TITLE[] = L"Table Drawing";
WNDCLASSEXW wc;
wc.cbSize = sizeof(wc);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WindowProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIconW(nullptr, IDI_APPLICATION);
wc.hCursor = LoadCursorW(nullptr, IDC_ARROW);
wc.hbrBackground = CreateSolidBrush(BACKGROUND_COLOR);
wc.lpszMenuName = nullptr;
wc.lpszClassName = WINDOW_CLASS;
wc.hIconSm = LoadIconW(nullptr, IDI_APPLICATION);
RegisterClassExW(&wc);
HWND hwnd = CreateWindowExW(
0x0,
WINDOW_CLASS,
WINDOW_TITLE,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, FIRST_WINDOW_SIZE.cx, FIRST_WINDOW_SIZE.cy,
nullptr,
nullptr,
hInstance,
nullptr
);
if (hwnd == nullptr) {
MessageBoxW(nullptr, L"Error creating window", L"Attention", MB_OK);
return 0;
}
ShowWindow(hwnd, nCmdShow);
MSG msg = {};
while (GetMessageW(&msg, nullptr, 0, 0)) {
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
GdiplusShutdown(gdiplusToken);
return 0;
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
switch (uMsg) {
case WM_PAINT: {
RECT rcClientRect;
GetClientRect(hwnd, &rcClientRect);
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
Graphics graphics = Graphics(hdc);
LONG edgeOffset = 5;
LONG tableWidth = rcClientRect.right - rcClientRect.left - edgeOffset * 2;
LONG tableHeight = rcClientRect.bottom - rcClientRect.top - edgeOffset * 2;
RectF clientRectF(edgeOffset, edgeOffset, tableWidth, tableHeight);
draw(&graphics, clientRectF);
EndPaint(hwnd, &ps);
break;
}
case WM_DESTROY: {
PostQuitMessage(0);
break;
}
case WM_GETMINMAXINFO: {
LPMINMAXINFO lpMMI = (LPMINMAXINFO) lParam;
lpMMI->ptMinTrackSize.x = MIN_WINDOW_SIZE.cx;
lpMMI->ptMinTrackSize.y = MIN_WINDOW_SIZE.cy;
break;
}
default: {
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
}
return 0;
}