C++ Windows.h 控制台绘画程序 - 左键绘画、右键擦除、中键清屏、调色、调粗细、改画布大小
#include
void SetColor(int color) { HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleTextAttribute(hConsole, color); }
void DrawPixel(int x, int y, int color) { COORD coord; coord.X = x; coord.Y = y; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); SetColor(color); std::cout << "*"; }
void ClearScreen() { COORD topLeft = {0, 0}; HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE); CONSOLE_SCREEN_BUFFER_INFO screen; DWORD written;
GetConsoleScreenBufferInfo(console, &screen);
FillConsoleOutputCharacterA(
console, ' ', screen.dwSize.X * screen.dwSize.Y, topLeft, &written
);
FillConsoleOutputAttribute(
console, FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE,
screen.dwSize.X * screen.dwSize.Y, topLeft, &written
);
SetConsoleCursorPosition(console, topLeft);
}
void DrawCanvas(int width, int height) { // 清屏 ClearScreen();
// 输出画布边框
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
if (i == 0 || i == height - 1 || j == 0 || j == width - 1) {
DrawPixel(j, i, FOREGROUND_INTENSITY);
}
}
}
}
int main() { const int WIDTH = 80; const int HEIGHT = 25;
int color = FOREGROUND_GREEN;
int thickness = 1;
// 设置控制台窗口大小
HWND console = GetConsoleWindow();
RECT rect;
GetWindowRect(console, &rect);
MoveWindow(console, rect.left, rect.top, WIDTH * 8, HEIGHT * 16, TRUE);
// 设置控制台缓冲区大小
COORD bufferSize = {WIDTH, HEIGHT};
SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), bufferSize);
// 隐藏控制台光标
CONSOLE_CURSOR_INFO cursorInfo;
GetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursorInfo);
cursorInfo.bVisible = false;
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursorInfo);
// 绘制画布
DrawCanvas(WIDTH, HEIGHT);
bool isDrawing = false;
POINT lastPoint;
// 事件循环
while (true) {
if (GetAsyncKeyState(VK_ESCAPE)) {
break;
}
MSG msg;
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
if (GetAsyncKeyState(VK_LBUTTON) & 0x8000) {
// 左键绘画
isDrawing = true;
GetCursorPos(&lastPoint);
ScreenToClient(console, &lastPoint);
} else if (GetAsyncKeyState(VK_RBUTTON) & 0x8000) {
// 右键擦除
isDrawing = false;
} else if (GetAsyncKeyState(VK_MBUTTON) & 0x8000) {
// 中键清屏
DrawCanvas(WIDTH, HEIGHT);
}
POINT currentPoint;
if (GetAsyncKeyState(VK_LBUTTON) & 0x8000 && isDrawing) {
// 绘制
GetCursorPos(¤tPoint);
ScreenToClient(console, ¤tPoint);
int deltaX = currentPoint.x - lastPoint.x;
int deltaY = currentPoint.y - lastPoint.y;
int distance = std::max(abs(deltaX), abs(deltaY));
for (int i = 0; i <= distance; i++) {
float t = static_cast<float>(i) / distance;
int x = lastPoint.x + static_cast<int>(t * deltaX);
int y = lastPoint.y + static_cast<int>(t * deltaY);
DrawPixel(x, y, color);
}
lastPoint = currentPoint;
}
// 处理键盘输入
if (_kbhit()) {
int key = _getch();
switch (key) {
case '1':
color = FOREGROUND_GREEN;
break;
case '2':
color = FOREGROUND_RED;
break;
case '3':
color = FOREGROUND_BLUE;
break;
case '+':
thickness++;
break;
case '-':
if (thickness > 1) {
thickness--;
}
break;
case '0':
DrawCanvas(WIDTH, HEIGHT);
break;
}
}
}
return 0;
}
原文地址: https://www.cveoy.top/t/topic/qsO0 著作权归作者所有。请勿转载和采集!