-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
346 lines (290 loc) · 8.74 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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#include <windows.h>
#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <list>
#include <map>
#include <string>
#include <vector>
struct Line
{
Line(int column)
{
Column = column;
Length = 4 + rand() % 16;
// 画面外に上部に初期位置をセット
Row = -1 * (Length + 4 + rand() % 16);
}
~Line() {};
int Column;
int Row;
int Length;
};
struct Size
{
int Width;
int Height;
};
Size g_screenSize;
Size g_fontSize;
int g_columnNum;
int g_rowNum;
HWND g_workerW;
HDC g_buffer;
HBITMAP g_bitmap;
std::string g_path;
std::list<Line> g_lines;
std::vector<std::string> g_strings;
std::vector<std::string> g_strings2;
enum Mode
{
Alphabet,
Binary,
};
Mode g_mode = Mode::Alphabet;
int g_speed = 7;
COLORREF g_fgColor = RGB(19, 161, 14);
COLORREF g_bgColor = RGB(0, 0, 0);
bool Initialize();
void CalcParam();
void Update();
void Draw();
bool Finalize();
int main(int argc, char* argv[])
{
if (!Initialize()) return EXIT_FAILURE;
// オプションを設定
for (int i = 0; i < argc; i++)
{
std::string str = argv[i];
// 文字を0,1に限定
if (str == "/bin") g_mode = Mode::Binary;
// 落下速度を1~9で指定
if (str == "/spd" && i + 1 < argc)
{
int spd = std::stoi(argv[i + 1]);
g_speed = std::clamp(spd, 1, 9);
}
// 前景色を16進数で指定
if (str == "/fgc" && i + 1 < argc)
{
int fgc = std::stoi(argv[i + 1], nullptr, 16);
int r = (fgc >> 16) & 0xff;
int g = (fgc >> 8) & 0xff;
int b = fgc & 0xff;
g_fgColor = RGB(r, g, b);
}
// 後景色を16進数で指定
if (str == "/bgc" && i + 1 < argc)
{
int bgc = std::stoi(argv[i + 1], nullptr, 16);
int r = (bgc >> 16) & 0xff;
int g = (bgc >> 8) & 0xff;
int b = bgc & 0xff;
g_bgColor = RGB(r, g, b);
}
}
CalcParam();
int count = 0;
while (true)
{
count = (count + 1) % (10/*max*/ - g_speed);
if (count == 0)
{
CalcParam();
Update();
Draw();
}
Sleep(10/*ms*/);
}
return EXIT_SUCCESS;
}
bool Initialize()
{
// 壁紙の描画用ウィンドウハンドルを取得
{
// デスクトップ画面を管理するウィンドウを取得
const HWND Hwnd = GetShellWindow();
// メッセージを送ってWorkerWを生成させる
SendMessageTimeoutA(Hwnd, 0x052C, 0, 0, SMTO_NORMAL, 1000, nullptr);
// WorkerWは複数あるが壁紙の描画用はデスクトップ管理ウィンドウの次のWorkerW
g_workerW = GetWindow(Hwnd, GW_HWNDPREV);
if (g_workerW == nullptr)
{
std::cerr << "Error : WorkerW" << std::endl;
return false;
}
}
// デスクトップの壁紙を取得
{
char path[MAX_PATH]{};
if (!SystemParametersInfoA(SPI_GETDESKWALLPAPER, MAX_PATH, (PVOID)path, 0))
{
std::cerr << "Error : Get Wallpaper Path" << std::endl;
return false;
}
g_path = path;
}
// イベントを登録
{
if (!SetConsoleCtrlHandler((PHANDLER_ROUTINE)[](DWORD event) -> BOOL
{
switch (event)
{
case CTRL_C_EVENT:
case CTRL_CLOSE_EVENT:
case CTRL_SHUTDOWN_EVENT:
Finalize();
return FALSE;
default:
return FALSE;
}
}, TRUE))
{
std::cerr << "Error : Ctrl Handle" << std::endl;
return false;
}
}
return true;
}
void CalcParam()
{
// パラメータの算出
{
static RECT oldScreenSize{ 0, 0, 0, 0 }, nowScreenSize;
GetClientRect(g_workerW, &nowScreenSize);
const int OldWidth = oldScreenSize.right - oldScreenSize.left;
const int OldHeight = oldScreenSize.bottom - oldScreenSize.top;
const int NowWidth = nowScreenSize.right - nowScreenSize.left;
const int NowHeight = nowScreenSize.bottom - nowScreenSize.top;
if (OldWidth == NowWidth && OldHeight == NowHeight) return;
g_fontSize = { 10, 20 };
g_screenSize.Width = NowWidth;
g_screenSize.Height = NowHeight;
g_columnNum = g_screenSize.Width / (g_fontSize.Width * 2);
g_rowNum = g_screenSize.Height / g_fontSize.Height;
oldScreenSize = nowScreenSize;
}
// ダブルバッファの準備
{
const HDC Hdc = GetDC(g_workerW);
if (g_buffer != nullptr)
{
DeleteDC(g_buffer);
g_buffer = nullptr;
}
if (g_bitmap != nullptr)
{
DeleteObject(g_bitmap);
g_bitmap = nullptr;
}
g_bitmap = CreateCompatibleBitmap(Hdc, g_screenSize.Width, g_screenSize.Height);
g_buffer = CreateCompatibleDC(0);
SelectObject(g_buffer, g_bitmap);
ReleaseDC(g_workerW, Hdc);
}
// 文字列バッファの準備
{
srand((unsigned)time(nullptr));
g_lines.clear();
for (int column = 0; column < g_columnNum; column++) g_lines.push_back(Line(column * 2));
g_strings.clear();
g_strings2.clear();
for (int row = 0; row < g_rowNum; row++)
{
g_strings.push_back(std::string(g_columnNum * 2, ' '));
g_strings2.push_back(std::string(g_columnNum * 2, ' '));
}
}
}
void Update()
{
for (int row = 0; row < g_rowNum; row++)
{
for (int column = 0; column < g_columnNum * 2; column++)
{
if (g_strings2[row][column] == ' ') continue;
g_strings[row][column] = g_strings2[row][column];
g_strings2[row][column] = ' ';
}
}
for (auto iterator = g_lines.begin(); iterator != g_lines.end();)
{
const int Length = iterator->Length;
const int TopRow = ++iterator->Row;
const int BottomRow = TopRow + Length - 1;
const int Column = iterator->Column;
if (BottomRow < 0)
{
iterator++;
continue;
}
if (TopRow == 0) g_lines.push_back(Line(Column));
if (g_rowNum - 1 < TopRow)
{
iterator = g_lines.erase(iterator);
continue;
}
if (BottomRow < g_rowNum)
{
if (g_mode == Mode::Alphabet) g_strings2[BottomRow][Column] = 33 + rand() % 94;
else g_strings2[BottomRow][Column] = 48 + rand() % 2;
}
if (0 <= TopRow - 1) g_strings[TopRow - 1][Column] = ' ';
iterator++;
}
}
void Draw()
{
// 背景の塗りつぶし
{
const HBRUSH Brush = CreateSolidBrush(g_bgColor);
const HBRUSH OldBrush = (HBRUSH)SelectObject(g_buffer, Brush);
PatBlt(g_buffer, 0, 0, g_screenSize.Width, g_screenSize.Height, PATCOPY);
SelectObject(g_buffer, OldBrush);
DeleteObject(Brush);
}
// 文字列の描画
{
const HFONT Font = CreateFontA(g_fontSize.Height, g_fontSize.Width,
0, 0, FW_DONTCARE, FALSE, FALSE, FALSE,
ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
DRAFT_QUALITY, DEFAULT_PITCH, "Cascadia Mono SemiBold");
const HFONT OldFont = (HFONT)SelectObject(g_buffer, Font);
SetBkMode(g_buffer, TRANSPARENT);
SetTextColor(g_buffer, g_fgColor);
for (int row = 0; row < g_rowNum; row++)
{
TextOutA(g_buffer, 5, row * g_fontSize.Height, g_strings[row].c_str(), g_strings[row].length());
}
SetTextColor(g_buffer, RGB(192, 192, 192));
for (int row = 0; row < g_rowNum; row++)
{
TextOutA(g_buffer, 5, row * g_fontSize.Height, g_strings2[row].c_str(), g_strings2[row].length());
}
SelectObject(g_buffer, OldFont);
DeleteObject(Font);
}
// ダブルバッファを入れ替え
{
const HDC Hdc = GetDC(g_workerW);
BitBlt(Hdc, 0, 0, g_screenSize.Width, g_screenSize.Height, g_buffer, 0, 0, SRCCOPY);
ReleaseDC(g_workerW, Hdc);
}
}
bool Finalize()
{
if (g_buffer != nullptr) DeleteDC(g_buffer);
if (g_bitmap != nullptr) DeleteObject(g_bitmap);
// デスクトップの壁紙を設定
{
if (!SystemParametersInfoA(SPI_SETDESKWALLPAPER, NULL,
(PVOID)g_path.c_str(), SPIF_UPDATEINIFILE | SPIF_SENDCHANGE))
{
std::cerr << "Error : Set Wallpaper Path" << std::endl;
return false;
}
}
return true;
}