C++ sets the cursor position and front background color of the console of command line window

  • 2020-06-15 09:59:43
  • OfStack

The core code


#include "stdafx.h"
 
#include <stdio.h>
#include <windows.h>
 
/*
#define FOREGROUND_BLUE   0x0001 // text color contains blue.
#define FOREGROUND_GREEN   0x0002 // text color contains green.
#define FOREGROUND_RED    0x0004 // text color contains red.
#define FOREGROUND_INTENSITY 0x0008 // text color is intensified.
 
#define BACKGROUND_BLUE   0x0010 // background color contains blue.
#define BACKGROUND_GREEN   0x0020 // background color contains green.
#define BACKGROUND_RED    0x0040 // background color contains red.
#define BACKGROUND_INTENSITY 0x0080 // background color is intensified.
*/
// Changes the color of the current output ( The foreground / The background color )
void ColorPrintf(WORD cl,char* str)
{
  static HANDLE h = GetStdHandle ( STD_OUTPUT_HANDLE );
  //WORD wOldColorAttrs;
  //CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
   
  //First save the current color information
  //GetConsoleScreenBufferInfo(h, &csbiInfo);
  //wOldColorAttrs = csbiInfo.wAttributes;
   
  //Set the new color information
  SetConsoleTextAttribute ( h, cl );
   
  printf ( str);
  //Restore the original colors
  //SetConsoleTextAttribute ( h, wOldColorAttrs);
  SetConsoleTextAttribute(h, FOREGROUND_INTENSITY | FOREGROUND_INTENSITY);
}
// Move the input cursor position 
void MoveCursorTo(int x,int y)
{
  static HANDLE m=GetStdHandle(STD_OUTPUT_HANDLE);
  COORD cp={x,y};
  SetConsoleCursorPosition(m,cp);
}
 
int main ( void )
{
 char st[10];
 ColorPrintf (FOREGROUND_BLUE | FOREGROUND_INTENSITY, "This is a color test\n" );
  
for (int j=0;j<255;j+=16)
{
  for (int i=0;i<16;i++)
  {
    sprintf(st,"%02x ",j+i);
    ColorPrintf(j+i,st);
  }
  printf("\n");
}
 
 //printf("\n\n");
 //MoveCursorTo( 1, 9 );
 //ColorPrintf(0x0083,"This is a test\n");
 return 0;
}

The terminal/console sets the color font, cursor positioning, and clear the screen

printf("\033[47;31mhello world\033[5m");

47 is the word background color, 31 is the font color, hello world is a string. \033[5m is the control code.

Color code:

QUOTE:

Word background color range: 40--49 word color: 30--39

40:30: black black

41: red 31: red

42: green 32: green

43: yellow 33: yellow

44: blue 34: blue

35:45: purple purple

46: Dark green 36: dark green

Forty-seven: white. Thirty-seven: white

ANSI control code:

QUOTE:

\033[0m] Closes all properties

\033[1m to set high brightness

\ '03 (4 m underline

033 \ [5 m flicker

\ [033 7 m reverse video

\ [033 8 m blanking

\033[30m -- \033[37m to set the foreground color

\033[40m - \033[47m] Set the background color

\033[nA] Move the cursor up n line

\03[nB move cursor down n line

\033[nC cursor moves n line to the right

\033[nD moves cursor left on n line

\033[y;xH sets the cursor position

033 \ [2 J screen clearly

\033[K clears everything from cursor to end of line

\033[s saves cursor position

\033[u] restore cursor position

\033[?25l hide cursor

\33[?25h display cursor

In this way, dynamic output can be implemented at some point.


Related articles: