I'm trying to draw a chess board on the cmd screen - 64 squares, and a letter in each one, that symbolize the solider that in this place. After very long searching, I found a code that change the words color, but my question is how can I draw squares on the cmd? BLACK - WHITE and so on. This is the code I've found :
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 text\n";
SetConsoleTextAttribute
(hConsole, FOREGROUND_BLUE | FOREGROUND_INTENSITY);
cout << "Bright blue text\n";
SetConsoleTextAttribute
(hConsole, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
cout << "Back to normal white text\n";
// Wait for user pressing key before exiting
// Gives them a chance to see the output
cout << "\n\nPress any key to exit program.....";
cin.get();
return 0;
}
I'll be glad for some help.
since white is red+green+blue: background white is
BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE
in code:
SetConsoleTextAttribute
(hConsole,BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE )
cout << "black on white\n";
Someone did this on Codeproject (Draw rectangle on screen):
#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;
}
Hope it helps
You have found the code, but you didn't really bother to understand it or to look at the documentation for SetConsoleTextAttribute
.
If you had, you'd have discovered the documentation about character attributes and noticed things like BACKGROUND_RED
, BACKGROUND_GREEN
and BACKGROUND_BLUE
which, predictably, can set the background color in the console.
Now, with the ability to change the color of the background, ask yourself "what happens if I set the background color to FOREGROUND_RED
and then print a space on the console?"
//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);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.