使用 c++ 在 cmd 上绘制
Draw on cmd using c++
我正在尝试在cmd屏幕上画一个棋盘-64个方块,每个方块中都有一个字母,象征着这个地方的士兵。经过很长时间的搜索,我找到了一个更改单词颜色的代码,但我的问题是如何在cmd上绘制正方形?黑色 - 白色等。这是我找到的代码:
int main()
{
HANDLE hConsole;
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
// Use the three primary colors for mixing any other color.
// Use FOREGROUND_INTENSITY for brighter colors.
SetConsoleTextAttribute
(hConsole, FOREGROUND_RED | FOREGROUND_INTENSITY);
cout << "Bright red textn";
SetConsoleTextAttribute
(hConsole, FOREGROUND_BLUE | FOREGROUND_INTENSITY);
cout << "Bright blue textn";
SetConsoleTextAttribute
(hConsole, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
cout << "Back to normal white textn";
// Wait for user pressing key before exiting
// Gives them a chance to see the output
cout << "nnPress any key to exit program.....";
cin.get();
return 0;
}
我会很高兴得到一些帮助。
因为白色是红色+绿色+蓝色:背景白色是
BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE
在代码中:
SetConsoleTextAttribute
(hConsole,BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE )
cout << "black on whiten";
有人在Codeproject上做了这个(在屏幕上画矩形):
#include <iostream>
#include <Windows.h>
using std::cout;
/*
*This method sets the cursor position.
*Usage:
*setxy(1,1);
*/
BOOL setxy(short x, short y)
{
COORD c = {x,y};
return SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),c);
}
/*
*This method draws the rectangle.
*Usage:
*DrawRect(1,1,10,10);
*or
*DrawRect(1,1,10,10,20,5);
*/
void DrawRect(int x, int y, int width, int height, int curPosX=0, int curPosY=0)
{
setxy(x,y);cout << char(201);
for(int i = 1; i < width; i++)cout << char(205);
cout << char(187);
setxy(x,height + y);cout << char(200);
for(int i = 1; i < width; i++)cout << char(205);
cout << char(188);
for(int i = y + 1; i < height + y; i++)
{
setxy(x,i);cout << char(186);
setxy(x + width,i);cout << char(186);
}
setxy(curPosX,curPosY);
}
int main()
{
setxy(1,1);
DrawRect(1,1,10,10,20,5);
return 0;
}
希望对你有帮助
您已经找到了代码,但您并没有真正费心去理解它或查看SetConsoleTextAttribute
的文档。
如果有的话,您就会发现有关角色属性的文档,并注意到诸如BACKGROUND_RED
、BACKGROUND_GREEN
和BACKGROUND_BLUE
之类的内容,可以预见的是,它们可以在控制台中设置背景颜色。
现在,有了更改背景颜色的功能,问问自己"如果我将背景颜色设置为 FOREGROUND_RED
,然后在控制台上打印一个空格会发生什么?
//win api drawing
#include <windows.h>
#include <iostream>
#include <conio.h>
using namespace std;
void draw(HWND hwnd, HDC hdc);
void setConsoleSize();
void main()
{
SetConsoleTitle(L"Simple Rectangle Drawing");
HWND hwnd = GetConsoleWindow();
HDC hdc = GetDC(hwnd);
setConsoleSize(); // set console size
Sleep(100);
draw(hwnd, hdc); // drawing function
int iKey = 1;
while (iKey != 27) { // exit on ESC key
if (_kbhit()) {
iKey = _getch();
switch (iKey)
{
case 112: case 80: case 167: case 135:
draw(hwnd, hdc); // on key 'p' we redraw
break;
}
}
}
ReleaseDC(hwnd, hdc); //cleaning console descriptors
}
// here you place your Drawing
void draw(HWND hwnd, HDC hdc)
{
Rectangle(hdc, 100, 60, 180, 160); // example
}
void setConsoleSize()
{
const int colConsole = 80;
const int rowConsole = 30;
HANDLE hNdl = GetStdHandle(STD_OUTPUT_HANDLE);
SMALL_RECT windowSize ={0,0,colConsole-1,rowConsole-1};
SetConsoleWindowInfo(hNdl, TRUE, &windowSize);// bufferSize
SetConsoleScreenBufferSize(hNdl, bufferSize);
}
相关文章:
- 如何使用ndk-build.cmd构建Android.so文件
- QuadTree只在窗口的右上角绘制
- 绘制旋转的三角形
- flutter:即使shouldRepaint()返回true,自定义画家也不会重新绘制
- 如何在QT中的自定义视频小工具t上绘制矩形
- 如何在Visual Basic中使用矩形函数OpenGL绘制矩形
- 无法使用VAO和EBO(openGL)绘制多个对象
- 为什么我的点没有在 OpenGL 中绘制鼠标所在的位置?
- 绘制顺时针三角形,重新排序点
- 在顶点着色器中使用 OpenGl 的未声明标识符,我在顶点着色器中绘制三角形时遇到问题
- SFML 文本未绘制在窗口上
- 在由Sublime文本3编译后在cmd上显示Java程序输出
- 如何在快板的屏幕中显示子位图的绘制?
- 如何绘制自定义形状的元素?
- QPainter 将 QBrush 设置为 在 QT/C++ 中绘制文本
- 如何在使用Makefile构建后在cmd中运行该程序?
- 除非重新绘制大小,否则SDL_Texture在调整大小后呈现黑色
- 绘制文本问题
- 在 QT C++中绘制/操作多边形
- 使用 c++ 在 cmd 上绘制