C++ 设置控制台(命令行)窗口 光标位置,及前背景颜色分享!

核心代码

  #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.  */  //更改当前输出的颜色(前景色/背景色)  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);  }  //移动输入光标位置  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 testn" );      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("nn");   //MoveCursorTo( 1, 9 );   //ColorPrintf(0x0083,"This is a testn");   return 0;  }

终端/控制台设置颜色字体、光标定位和清屏

printf(“33[47;31mhello world33[5m”);

47是字背景颜色, 31是字体的颜色, hello world是字符串. 后面的33[5m是控制码.

颜色代码:

QUOTE:

字背景颜色范围: 40–49 字颜色: 30–39

40: 黑 30: 黑

41: 红 31: 红

42: 绿 32: 绿

43: 黄 33: 黄

44: 蓝 34: 蓝

45: 紫 35: 紫

46: 深绿 36: 深绿

47: 白色 37: 白色

ANSI控制码:

QUOTE:

33[0m 关闭所有属性

33[1m 设置高亮度

3[4m 下划线

33[5m 闪烁

33[7m 反显

33[8m 消隐

33[30m — 33[37m 设置前景色

33[40m — 33[47m 设置背景色

33[nA 光标上移n行

3[nB 光标下移n行

33[nC 光标右移n行

33[nD 光标左移n行

33[y;xH设置光标位置

33[2J 清屏

33[K 清除从光标到行尾的内容

33[s 保存光标位置

33[u 恢复光标位置

33[?25l 隐藏光标

33[?25h 显示光标

这样, 在某些时候就可以实现动态的输出.

—-想了解C++ 设置控制台(命令行)窗口 光标位置,及前背景颜色分享!全部内容且更多的C语言教程关注<计算机技术网(www.ctvol.com)!!>

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/c-cdevelopment/482927.html

(0)
上一篇 2020年11月9日
下一篇 2020年11月9日

精彩推荐