使用 c++ 在 cmd 上绘制

Draw on cmd using c++

本文关键字:绘制 cmd c++ 使用      更新时间:2023-10-16

我正在尝试在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_REDBACKGROUND_GREENBACKGROUND_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);
}