-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
207 lines (170 loc) · 6.49 KB
/
main.cpp
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
204
205
206
207
#include <windows.h>
#include <commctrl.h>
#include <stdio.h>
#include "resource.h"
#define PLAIN_RECT(rect) rect.left,rect.top,rect.right,rect.bottom
namespace Colors
{
static COLORREF FromHex(int hex)
{
auto r = (hex & 0xFF0000) >> 16;
auto g = (hex & 0x00FF00) >> 8;
auto b = (hex & 0x0000FF) >> 0;
return RGB(r, g, b);
}
static const COLORREF lightRed = FromHex(0xFF0000);
static const COLORREF lightYellow = FromHex(0xFFFF00);
static const COLORREF lightGreen = FromHex(0x00FF00);
static const COLORREF darkRed = FromHex(0x800000);
static const COLORREF darkYellow = FromHex(0x808000);
static const COLORREF darkGreen = FromHex(0x008000);
static const COLORREF Gold = FromHex(0xFDD017);
static const COLORREF Blue = FromHex(0x0000FF);
static const COLORREF SteelBlue2 = FromHex(0x5CACEE);
static const COLORREF Black = FromHex(0x000000);
static const COLORREF White = FromHex(0xFFFFFF);
}
HINSTANCE hInst;
const int MinDialogWidth = 400;
const int MinDialogHeight = 600;
const int Timer1ID = 1;
const int RED_SCHEDULE[] = {1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0};
const int YELLOW_SCHEDULE[] = {0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,1,1};
const int GREEN_SCHEDULE[] = {0,0,0,0,0,0,0,0,1,1,1,0,1,0,1,0,0};
const int TIME_SCHEDULE[] = {8,7,6,5,4,3,2,1,9,8,7,6,5,4,3,2,1};
int timerState = 0;
PAINTSTRUCT _paintstruct;
RECT rect;
namespace Pens {
auto line = CreatePen(PS_DASHDOTDOT, 1, Colors::SteelBlue2);
auto dark = CreatePen(PS_SOLID, 2, Colors::Black);
auto light = CreatePen(PS_SOLID, 4, Colors::Gold);
}
namespace Brushes {
auto lightRed = CreateSolidBrush(Colors::lightRed);
auto lightGreen = CreateSolidBrush(Colors::lightGreen);
auto lightYellow = CreateSolidBrush(Colors::lightYellow);
auto darkRed = CreateSolidBrush(Colors::darkRed);
auto darkGreen = CreateSolidBrush(Colors::darkGreen);
auto darkYellow = CreateSolidBrush(Colors::darkYellow);
}
RECT CreateRect(int left, int top, int rigth, int bottom)
{
auto rect = RECT();
rect.left = left;
rect.top = top;
rect.right = rigth;
rect.bottom = bottom;
return rect;
}
RECT CalculateShapeRect(int xCenter, int yCenter, double width, double heigth)
{
auto x = xCenter - (int)width / 2;
auto y = yCenter - (int)heigth / 2;
return CreateRect(x, y, x + (int)width, y + (int)heigth);
}
HFONT CreateFont(const char* name, int size, int weight)
{
auto lf = LOGFONT{};
strcpy(lf.lfFaceName, name);
lf.lfHeight = size;
lf.lfWeight = weight;
lf.lfCharSet = DEFAULT_CHARSET;
return CreateFontIndirect(&lf);
}
void OnPaint(HWND _hDialog)
{
auto hDeviceContext = BeginPaint(_hDialog, &_paintstruct);
GetClientRect(_hDialog, &rect);
auto width = rect.right - rect.left;
auto height = rect.bottom - rect.top;
auto unit = min(width, height) / 3;
/* BEGIN LINES */
auto hOldPen = SelectObject(hDeviceContext, Pens::line);
//Vertical line
MoveToEx(hDeviceContext, width / 2, 0, nullptr);
LineTo(hDeviceContext, width / 2, height);
//5 Horizontal Lines
MoveToEx(hDeviceContext, 0, height / 6, nullptr);
LineTo(hDeviceContext, width, height / 6);
MoveToEx(hDeviceContext, 0, 2*height / 6, nullptr);
LineTo(hDeviceContext, width, 2*height / 6);
MoveToEx(hDeviceContext, 0, 3*height / 6, nullptr);
LineTo(hDeviceContext, width, 3*height / 6);
MoveToEx(hDeviceContext, 0, 4*height / 6, nullptr);
LineTo(hDeviceContext, width, 4*height / 6);
MoveToEx(hDeviceContext, 0, 5*height / 6, nullptr);
LineTo(hDeviceContext, width, 5*height / 6);
/* END LINES */
/* BEGIN CIRCLES */
SelectObject(hDeviceContext, (RED_SCHEDULE[timerState]) ? Brushes::lightRed : Brushes::darkRed);
SelectObject(hDeviceContext, (RED_SCHEDULE[timerState]) ? Pens::light : Pens::dark );
Ellipse(hDeviceContext, PLAIN_RECT(CalculateShapeRect(width / 2, height / 6, unit, unit)));
SelectObject(hDeviceContext, (YELLOW_SCHEDULE[timerState]) ? Brushes::lightYellow : Brushes::darkYellow);
SelectObject(hDeviceContext, (YELLOW_SCHEDULE[timerState]) ? Pens::light : Pens::dark );
Ellipse(hDeviceContext, PLAIN_RECT(CalculateShapeRect(width / 2, 3*height / 6, unit, unit)));
SelectObject(hDeviceContext, (GREEN_SCHEDULE[timerState]) ? Brushes::lightGreen : Brushes::darkGreen);
SelectObject(hDeviceContext, (GREEN_SCHEDULE[timerState]) ? Pens::light : Pens::dark );
Ellipse(hDeviceContext, PLAIN_RECT(CalculateShapeRect(width / 2, 5*height / 6, unit, unit)));
/* END CIRCLES */
/* BEGIN TIME */
SelectObject(hDeviceContext, CreateFont("Consolas", 0.7*unit, FW_BOLD));
SetTextColor(hDeviceContext, Colors::Blue);
SetBkMode(hDeviceContext, TRANSPARENT);
auto textRectangle = CalculateShapeRect(width / 2, 3*height / 6, unit, unit);
char text[10];
sprintf(text, "%d", TIME_SCHEDULE[timerState]);
DrawText(hDeviceContext, text, -1, &textRectangle, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
/* END TIME */
ValidateRect(_hDialog, nullptr);
EndPaint(_hDialog, &_paintstruct);
}
BOOL CALLBACK DlgMain(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg)
{
case WM_INITDIALOG:
{
SetWindowPos(hwndDlg, nullptr, NULL, NULL, MinDialogWidth, MinDialogHeight, SWP_NOMOVE | SWP_NOZORDER | SWP_FRAMECHANGED);
SetTimer(hwndDlg, Timer1ID, 1000, nullptr);
}
return TRUE;
case WM_CLOSE:
{
EndDialog(hwndDlg, 0);
}
return TRUE;
case WM_TIMER:
{
auto timerID = (int)wParam;
if (timerID == Timer1ID)
{
++timerState;
if (timerState > 16)
timerState = 0;
InvalidateRect(hwndDlg, nullptr, TRUE);
}
return TRUE;
}
case WM_PAINT:
OnPaint(hwndDlg);
return TRUE;
case WM_GETMINMAXINFO:
{
auto sizeInfo = (LPMINMAXINFO)lParam;
sizeInfo->ptMinTrackSize.x = MinDialogWidth;
sizeInfo->ptMinTrackSize.y = MinDialogHeight;
return TRUE;
}
case WM_SIZE:
InvalidateRect(hwndDlg, nullptr, TRUE);
return TRUE;
}
return FALSE;
}
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
hInst=hInstance;
InitCommonControls();
return DialogBox(hInst, MAKEINTRESOURCE(DLG_MAIN), NULL, (DLGPROC)DlgMain);
}