-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmd-matrix.c
More file actions
384 lines (319 loc) · 12.5 KB
/
cmd-matrix.c
File metadata and controls
384 lines (319 loc) · 12.5 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
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <process.h>
#define MAX_WINDOWS 100
#define MAX_COMMAND_LENGTH 1024
#define MAX_COMMANDS 100
#define WINDOW_TITLE_PREFIX "BATCH-CMD-"
typedef struct {
char title[64];
HWND hwnd;
int index;
int posX;
int posY;
int width;
int height;
int windowIndex;
char command[MAX_COMMAND_LENGTH];
} ThreadData;
HWND createdWindows[MAX_WINDOWS] = {NULL};
int windowCount = 0;
char commands[MAX_COMMANDS][MAX_COMMAND_LENGTH];
int commandCount = 0;
HWND CreateCMDWindow(const char* title, const char* command);
void CloseAllWindows();
unsigned __stdcall CreateWindowThread(void* data);
void InitializeWindowArrays();
int ParseDimensions(int argc, char** argv, int* rows, int* cols);
void CalculateWindowLayout(int rows, int cols, int* windowWidth, int* windowHeight,
int* charWidth, int* charHeight);
int CreateAndArrangeWindows(int rows, int cols);
void PrintUsage();
void GetScreenResolution(int* width, int* height);
HWND FindWindowByTitle(const char* title);
int ReadCommandsFromFile(const char* filename);
unsigned __stdcall CreateWindowThread(void* data) {
ThreadData* threadData = (ThreadData*)data;
threadData->hwnd = CreateCMDWindow(threadData->title, threadData->command);
if (threadData->hwnd != NULL) {
MoveWindow(threadData->hwnd, threadData->posX, threadData->posY,
threadData->width, threadData->height, TRUE);
}
return 0;
}
void GetScreenResolution(int* width, int* height) {
*width = GetSystemMetrics(SM_CXSCREEN);
*height = GetSystemMetrics(SM_CYSCREEN);
}
HWND FindWindowByTitle(const char* title) {
HWND hwnd = FindWindow(NULL, title);
if (hwnd != NULL) {
return hwnd;
}
hwnd = NULL;
char windowTitle[256];
while ((hwnd = FindWindowEx(NULL, hwnd, NULL, NULL)) != NULL) {
if (GetWindowText(hwnd, windowTitle, sizeof(windowTitle))) {
if (strstr(windowTitle, title) != NULL) {
return hwnd;
}
}
}
return NULL;
}
int ReadCommandsFromFile(const char* filename) {
FILE* file = fopen(filename, "r");
if (file == NULL) {
printf("Error: Cannot open file '%s'\n", filename);
return 0;
}
commandCount = 0;
char line[MAX_COMMAND_LENGTH];
while (fgets(line, sizeof(line), file) && commandCount < MAX_COMMANDS) {
// 移除换行符
line[strcspn(line, "\r\n")] = 0;
// 跳过空行和注释行(以#开头)
if (strlen(line) > 0 && line[0] != '#') {
strcpy(commands[commandCount], line);
commandCount++;
}
}
fclose(file);
printf("Successfully read %d commands from file '%s'\n", commandCount, filename);
return commandCount;
}
HWND CreateCMDWindow(const char* title, const char* command) {
char cmdCommand[2048];
PROCESS_INFORMATION pi;
STARTUPINFO si = {0};
si.cb = sizeof(STARTUPINFO);
// 使用文件中的命令替换默认命令
if (strlen(command) > 0) {
snprintf(cmdCommand, sizeof(cmdCommand),
"cmd /k \"title %s & echo ============================ & echo Executing command from file... & echo Command: %s & echo ============================ & %s\"",
title, command, command);
} else {
snprintf(cmdCommand, sizeof(cmdCommand),
"cmd /k \"title %s & echo No command specified for this window. & echo. & echo Type your commands here...\"",
title);
}
if (CreateProcess(NULL, cmdCommand, NULL, NULL, FALSE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi)) {
WaitForInputIdle(pi.hProcess, 5000);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
HWND hwnd = NULL;
for (int attempt = 0; attempt < 20; attempt++) {
Sleep(5);
hwnd = FindWindowByTitle(title);
if (hwnd != NULL) {
return hwnd;
}
}
}
return NULL;
}
void CloseAllWindows() {
for (int i = 0; i < windowCount; i++) {
if (createdWindows[i] != NULL && IsWindow(createdWindows[i])) {
PostMessage(createdWindows[i], WM_CLOSE, 0, 0);
}
}
}
void InitializeWindowArrays() {
windowCount = 0;
for (int i = 0; i < MAX_WINDOWS; i++) {
createdWindows[i] = NULL;
}
commandCount = 0;
for (int i = 0; i < MAX_COMMANDS; i++) {
commands[i][0] = '\0';
}
}
int ParseDimensions(int argc, char** argv, int* rows, int* cols) {
*rows = 3;
*cols = 3;
if (argc >= 4) {
*rows = atoi(argv[2]);
*cols = atoi(argv[3]);
if (*rows <= 0 || *cols <= 0) {
printf("Error: Invalid dimensions\n");
return 0;
}
}
return 1;
}
void CalculateWindowLayout(int rows, int cols, int* windowWidth, int* windowHeight,
int* charWidth, int* charHeight) {
int screenWidth, screenHeight;
GetScreenResolution(&screenWidth, &screenHeight);
int borderOffset = 16;
*windowWidth = screenWidth / cols;
if (rows == 1) {
*windowHeight = screenHeight - 100;
} else {
*windowHeight = screenHeight / rows;
}
*charWidth = (*windowWidth - borderOffset) / 8;
*charHeight = (*windowHeight - borderOffset) / 16;
if (*charWidth < 10) *charWidth = 10;
if (*charHeight < 5) *charHeight = 5;
}
int CreateAndArrangeWindows(int rows, int cols) {
int windowWidth, windowHeight, charWidth, charHeight;
CalculateWindowLayout(rows, cols, &windowWidth, &windowHeight, &charWidth, &charHeight);
ThreadData threadData[MAX_WINDOWS];
HANDLE threads[MAX_WINDOWS];
int totalWindowsToCreate = 0;
// 确定要创建的窗口数量(取命令数和网格数的较小值)
int maxWindows = rows * cols;
int actualWindows = (commandCount > 0) ? ((commandCount < maxWindows) ? commandCount : maxWindows) : maxWindows;
for (int row = 1; row <= rows; row++) {
for (int col = 1; col <= cols; col++) {
int windowIndex = (row - 1) * cols + (col - 1) + 1;
// 如果已经创建了足够的窗口,跳出循环
if (totalWindowsToCreate >= actualWindows) {
break;
}
int posX = (col - 1) * windowWidth;
int posY = (row - 1) * windowHeight;
char windowTitle[64];
snprintf(windowTitle, sizeof(windowTitle), "%s%d-WIN", WINDOW_TITLE_PREFIX, windowIndex);
strcpy(threadData[totalWindowsToCreate].title, windowTitle);
threadData[totalWindowsToCreate].hwnd = NULL;
threadData[totalWindowsToCreate].index = windowIndex - 1;
threadData[totalWindowsToCreate].posX = posX;
threadData[totalWindowsToCreate].posY = posY;
threadData[totalWindowsToCreate].width = windowWidth;
threadData[totalWindowsToCreate].height = windowHeight;
threadData[totalWindowsToCreate].windowIndex = windowIndex;
// 设置对应的命令
if (totalWindowsToCreate < commandCount) {
strcpy(threadData[totalWindowsToCreate].command, commands[totalWindowsToCreate]);
} else {
threadData[totalWindowsToCreate].command[0] = '\0';
}
totalWindowsToCreate++;
}
// 如果已经创建了足够的窗口,跳出外层循环
if (totalWindowsToCreate >= actualWindows) {
break;
}
}
for (int i = 0; i < totalWindowsToCreate; i++) {
threads[i] = (HANDLE)_beginthreadex(NULL, 0, CreateWindowThread, &threadData[i], 0, NULL);
}
WaitForMultipleObjects(totalWindowsToCreate, threads, TRUE, INFINITE);
for (int i = 0; i < totalWindowsToCreate; i++) {
if (threadData[i].hwnd != NULL) {
createdWindows[threadData[i].index] = threadData[i].hwnd;
windowCount++;
} else {
createdWindows[threadData[i].index] = NULL;
}
CloseHandle(threads[i]);
}
return totalWindowsToCreate;
}
void PrintUsage() {
printf("Usage: win_sizer.exe <window_title> <x> <y> <width_chars> <height_chars>\n");
printf(" win_sizer.exe --resolution (Get screen resolution)\n");
printf(" win_sizer.exe --test [rows] [cols] (Create and arrange CMD windows)\n");
printf(" win_sizer.exe --file <filename> [rows] [cols] (Create windows with commands from file)\n");
printf("Example: win_sizer.exe \"CMD\" 100 100 80 25\n");
printf(" win_sizer.exe --resolution\n");
printf(" win_sizer.exe --test\n");
printf(" win_sizer.exe --test 2 3\n");
printf(" win_sizer.exe --file commands.txt\n");
printf(" win_sizer.exe --file commands.txt 2 3\n");
printf("\nFile format (commands.txt):\n");
printf(" Each line contains a command to execute in corresponding window\n");
printf(" Lines starting with # are treated as comments\n");
printf(" Empty lines are ignored\n");
printf(" Example:\n");
printf(" echo Hello from Window 1\n");
printf(" dir C:\\\n");
printf(" ipconfig /all\n");
printf(" # This is a comment\n");
printf(" ping google.com\n");
}
int main(int argc, char** argv) {
if (argc >= 2 && strcmp(argv[1], "--resolution") == 0) {
int width, height;
GetScreenResolution(&width, &height);
printf("%d %d\n", width, height);
return 0;
}
if (argc >= 2 && strcmp(argv[1], "--test") == 0) {
InitializeWindowArrays();
int rows, cols;
if (!ParseDimensions(argc, argv, &rows, &cols)) {
return 1;
}
int createdCount = CreateAndArrangeWindows(rows, cols);
if (createdCount > 0) {
printf("Created %d windows successfully. Press Enter to close all windows...\n", createdCount);
getchar();
CloseAllWindows();
}
return 0;
}
if (argc >= 3 && strcmp(argv[1], "--file") == 0) {
InitializeWindowArrays();
const char* filename = argv[2];
if (!ReadCommandsFromFile(filename)) {
return 1;
}
int rows, cols;
if (argc >= 5) {
rows = atoi(argv[3]);
cols = atoi(argv[4]);
if (rows <= 0 || cols <= 0) {
printf("Error: Invalid dimensions\n");
return 1;
}
} else {
// 根据命令数量自动计算合适的行列数
if (commandCount <= 4) {
rows = 2; cols = 2;
} else if (commandCount <= 6) {
rows = 2; cols = 3;
} else if (commandCount <= 9) {
rows = 3; cols = 3;
} else {
rows = 3; cols = 4;
}
}
printf("Creating %dx%d grid with commands from file...\n", rows, cols);
int createdCount = CreateAndArrangeWindows(rows, cols);
if (createdCount > 0) {
printf("Created %d windows successfully. Press Enter to close all windows...\n", createdCount);
getchar();
CloseAllWindows();
}
return 0;
}
if (argc != 6) {
PrintUsage();
return 1;
}
const char* windowTitle = argv[1];
int x = atoi(argv[2]), y = atoi(argv[3]), w = atoi(argv[4]), h = atoi(argv[5]);
if (x < 0 || y < 0 || w < 1 || h < 1) {
printf("Error: Invalid parameters\n");
return 1;
}
HWND hWnd = FindWindowByTitle(windowTitle);
if (hWnd == NULL) {
printf("Error: Cannot find window with title containing '%s'\n", windowTitle);
return 1;
}
if (!MoveWindow(hWnd, x, y, w*8, h*16, TRUE)) {
printf("Error: MoveWindow failed\n");
return 1;
}
printf("Successfully moved window '%s' to position (%d, %d) with size %dx%d\n",
windowTitle, x, y, w*8, h*16);
return 0;
}