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 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;
}

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

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

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

颜色代码:

QUOTE:

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

40: 黑 30: 黑

41: 红 31: 红

42: 绿 32: 绿

43: 黄 33: 黄

44: 蓝 34: 蓝

45: 紫 35: 紫

46: 深绿 36: 深绿

47: 白色 37: 白色

ANSI控制码:

QUOTE:

\033[0m 关闭所有属性

\033[1m 设置高亮度

\03[4m 下划线

\033[5m 闪烁

\033[7m 反显

\033[8m 消隐

\033[30m -- \033[37m 设置前景色

\033[40m -- \033[47m 设置背景色

\033[nA 光标上移n行

\03[nB 光标下移n行

\033[nC 光标右移n行

\033[nD 光标左移n行

\033[y;xH设置光标位置

\033[2J 清屏

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

\033[s 保存光标位置

\033[u 恢复光标位置

\033[?25l 隐藏光标

\33[?25h 显示光标

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

(0)

相关推荐

  • c++文件监控之FileSystemWatcher

    具体代码如下: #using <System.dll> #include <iostream> using namespace std; using namespace System; using namespace System::IO; using namespace System::Security::Permissions; public ref class Watcher { private: // Define the event handlers. static vo

  • 用C/C++代码检测ip能否ping通(配合awk和system可以做到批量检测)

    遇到一个小需求, 快速搞定. 来看看用C/C++代码检测ip能否ping通: #include <iostream> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> using namespace std; string getCmdResult(const string &strCmd) // 这个是获取命令执行的结果, 类似于

  • C++中rapidjson将嵌套map转为嵌套json的讲解

    rapidjson将嵌套map转为嵌套json------人生苦短,我用rapidjson 看代码: #include <iostream> #include <map> // 请自己下载开源的rapidjson #include "rapidjson/prettywriter.h" #include "rapidjson/rapidjson.h" #include "rapidjson/document.h" #incl

  • vc++ 监控指定路径下文件变化实现代码

    参考MSDN文档 https://docs.microsoft.com/zh-cn/windows/desktop/api/winbase/nf-winbase-readdirectorychangesw https://docs.microsoft.com/zh-cn/windows/desktop/api/winnt/ns-winnt-_file_notify_information 具体看代码 # include < iostream > # include < windows.h

  • C++中rapidjson组装map和数组array的代码示例

    rapidjson组装map和数组array的代码示例 直接上码: #include <iostream> #include <map> // 请自己下载开源的rapidjson #include "rapidjson/prettywriter.h" #include "rapidjson/rapidjson.h" #include "rapidjson/document.h" #include "rapidjs

  • C++中rapidjson将map转为json的方法

    rapidjson将map转为json------人生苦短,我用rapidjson 直接撸代码: #include <iostream> #include <map> // 请自己下载开源的rapidjson #include "rapidjson/prettywriter.h" #include "rapidjson/rapidjson.h" #include "rapidjson/document.h" #includ

  • rapidjson解析json代码实例以及常见的json core dump问题

    rapidjson解析json代码实例 直接看代码: #include <iostream> #include <stdio.h> #include<unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include<sstream> // 请自己下载开源的rapidjson #include "rapidjson/

  • C++中rapidjson组装继续简化的方法

    rapidjson组装继续简化------人生苦短,我用rapidjson 看最简单的: #include <iostream> #include <stdio.h> #include<unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include<sstream> // 请自己下载开源的rapidjson #includ

  • 利用rapidjson实现解析嵌套的json的方法示例

    利用rapidjson解析嵌套的json 看json串1:{"system":{"version":"v2.6.1", "name":"value"}} 废话少说, 直接撸代码: #include <iostream> #include <stdio.h> #include<unistd.h> #include <sys/types.h> #include &

  • C++ 获取进程CPU占用率

    核心代码 // 时间转换 static __int64 file_time_2_utc(const FILETIME* ftime) { LARGE_INTEGER li; li.LowPart = ftime->dwLowDateTime; li.HighPart = ftime->dwHighDateTime; return li.QuadPart; } // 获得CPU的核数 static int get_processor_number() { SYSTEM_INFO info; Ge

随机推荐