-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConsole.cpp
64 lines (59 loc) · 2.01 KB
/
Console.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
#include "include.h"
#include <windows.h>
#include <time.h>
static HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
void initWindow()
{
SetConsoleTitleA("Plant VS. Zombies! | version = 2.1 | @Roc"); //修改窗口名字
//设置窗口大小不可变
HWND hWnd = GetConsoleWindow();
RECT rc;
GetWindowRect(hWnd, &rc);
SetWindowLongPtr(hWnd, GWL_STYLE, GetWindowLong(hWnd, GWL_STYLE) & ~WS_THICKFRAME & ~WS_MAXIMIZEBOX & ~WS_MINIMIZEBOX);
SetWindowPos(hWnd, NULL, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, NULL);
//修改窗口大小
char cmd[] = "mode con cols=0000 lines=0000";
char buffer_w[64];
char buffer_h[64];
int ret = snprintf(buffer_w, sizeof buffer_w, "%04d", WINDOWS_WIDTH);
if (ret < 0)
exit(-1);
ret = snprintf(buffer_h, sizeof buffer_h, "%04d", WINDOWS_HIGH);
if (ret < 0)
exit(-1);
cmd[14] = buffer_w[0]; cmd[15] = buffer_w[1]; cmd[16] = buffer_w[2]; cmd[17] = buffer_w[3];
cmd[25] = buffer_h[0]; cmd[26] = buffer_h[1]; cmd[27] = buffer_h[2]; cmd[28] = buffer_h[3];
system(cmd);
//system("mode con cols=90 lines=39");
//隐藏光标
setCursorHide(0);
//恢复默认颜色
setColor();
//重置随机数种子
srand((unsigned)time(0));
}
void setCursorHide(bool visible)
{
//HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO CursorInfo;
GetConsoleCursorInfo(handle, &CursorInfo);//获取控制台光标信息
CursorInfo.bVisible = visible; //隐藏或显示控制台光标
SetConsoleCursorInfo(handle, &CursorInfo);//设置控制台光标状态
return;
}
void setCursorPos(int x, int y)
{
//HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(handle, coord);
}
void setColor(int font_color, int background_color)
{
//HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
if (font_color == WHITE &&
background_color == YELLOW || background_color == WHITE || background_color == BLUE || background_color == CYAN || background_color == DARK_WHITE || background_color == font_color)
font_color = BLACK;
SetConsoleTextAttribute(handle, background_color << 4 | font_color);
}