cmake编译输出时有颜色,这是怎么做到的呢

技术菌的blog 2016-07-10 07:44:05

这是在xshell下的输出,可能与xshell本身的配置也有关。


好想让个人程序的控制台输出也有颜色,可以用哪种库或配置呢?
...全文
1141 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2016-07-11
  • 打赏
  • 举报
回复
仅供参考:
#include <windows.h>
#include <stdio.h>

void ConPrint(char *CharBuffer, int len);
void ConPrintAt(int x, int y, char *CharBuffer, int len);
void gotoXY(int x, int y);
void ClearConsole(void);
void ClearConsoleToColors(int ForgC, int BackC);
void SetColorAndBackground(int ForgC, int BackC);
void SetColor(int ForgC);
void HideTheCursor(void);
void ShowTheCursor(void);

int main(int argc, char* argv[])
{
   HideTheCursor();
   ClearConsoleToColors(15, 1);
   ClearConsole();
   gotoXY(1, 1);
   SetColor(14);
   printf("This is a test...\n");
   Sleep(5000);
   ShowTheCursor();
   SetColorAndBackground(15, 12);
   ConPrint("This is also a test...\n", 23);
   SetColorAndBackground(1, 7);
   ConPrintAt(22, 15, "This is also a test...\n", 23);
   gotoXY(0, 24);
   SetColorAndBackground(7, 1);
   return 0;
}

//This will clear the console while setting the forground and
//background colors.
void ClearConsoleToColors(int ForgC, int BackC)
{
   WORD wColor = ((BackC & 0x0F) << 4) + (ForgC & 0x0F);
   //Get the handle to the current output buffer...
   HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
   //This is used to reset the carat/cursor to the top left.
   COORD coord = {0, 0};
   //A return value... indicating how many chars were written
   //not used but we need to capture this since it will be
   //written anyway (passing NULL causes an access violation).
   DWORD count;

   //This is a structure containing all of the console info
   // it is used here to find the size of the console.
   CONSOLE_SCREEN_BUFFER_INFO csbi;
   //Here we will set the current color
   SetConsoleTextAttribute(hStdOut, wColor);
   if(GetConsoleScreenBufferInfo(hStdOut, &csbi))
   {
      //This fills the buffer with a given character (in this case 32=space).
      FillConsoleOutputCharacter(hStdOut, (TCHAR) 32, csbi.dwSize.X * csbi.dwSize.Y, coord, &count);

      FillConsoleOutputAttribute(hStdOut, csbi.wAttributes, csbi.dwSize.X * csbi.dwSize.Y, coord, &count);
      //This will set our cursor position for the next print statement.
      SetConsoleCursorPosition(hStdOut, coord);
   }
}

//This will clear the console.
void ClearConsole()
{
   //Get the handle to the current output buffer...
   HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
   //This is used to reset the carat/cursor to the top left.
   COORD coord = {0, 0};
   //A return value... indicating how many chars were written
   //   not used but we need to capture this since it will be
   //   written anyway (passing NULL causes an access violation).
   DWORD count;
   //This is a structure containing all of the console info
   // it is used here to find the size of the console.
   CONSOLE_SCREEN_BUFFER_INFO csbi;
   //Here we will set the current color
   if(GetConsoleScreenBufferInfo(hStdOut, &csbi))
   {
      //This fills the buffer with a given character (in this case 32=space).
      FillConsoleOutputCharacter(hStdOut, (TCHAR) 32, csbi.dwSize.X * csbi.dwSize.Y, coord, &count);
      FillConsoleOutputAttribute(hStdOut, csbi.wAttributes, csbi.dwSize.X * csbi.dwSize.Y, coord, &count);
      //This will set our cursor position for the next print statement.
      SetConsoleCursorPosition(hStdOut, coord);
   }
}

//This will set the position of the cursor
void gotoXY(int x, int y)
{
   //Initialize the coordinates
   COORD coord = {x, y};
   //Set the position
   SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}

//This will set the forground color for printing in a console window.
void SetColor(int ForgC)
{
   WORD wColor;
   //We will need this handle to get the current background attribute
   HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
   CONSOLE_SCREEN_BUFFER_INFO csbi;

   //We use csbi for the wAttributes word.
   if(GetConsoleScreenBufferInfo(hStdOut, &csbi))
   {
      //Mask out all but the background attribute, and add in the forgournd color
      wColor = (csbi.wAttributes & 0xF0) + (ForgC & 0x0F);
      SetConsoleTextAttribute(hStdOut, wColor);
   }
}

//This will set the forground and background color for printing in a console window.
void SetColorAndBackground(int ForgC, int BackC)
{
   WORD wColor = ((BackC & 0x0F) << 4) + (ForgC & 0x0F);;
   SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), wColor);
}

//Direct console output
void ConPrint(char *CharBuffer, int len)
{
   DWORD count;
   WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), CharBuffer, len, &count, NULL);
}

//Direct Console output at a particular coordinate.
void ConPrintAt(int x, int y, char *CharBuffer, int len)
{
   DWORD count;
   COORD coord = {x, y};
   HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
   SetConsoleCursorPosition(hStdOut, coord);
   WriteConsole(hStdOut, CharBuffer, len, &count, NULL);
}

//Hides the console cursor
void HideTheCursor()
{
   CONSOLE_CURSOR_INFO cciCursor;
   HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);

   if(GetConsoleCursorInfo(hStdOut, &cciCursor))
   {
      cciCursor.bVisible = FALSE;
	  SetConsoleCursorInfo(hStdOut, &cciCursor);
   }
}

//Shows the console cursor
void ShowTheCursor()
{
   CONSOLE_CURSOR_INFO cciCursor;
   HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);

   if(GetConsoleCursorInfo(hStdOut, &cciCursor))
   {
      cciCursor.bVisible = TRUE;
	  SetConsoleCursorInfo(hStdOut, &cciCursor);
   }
}

ID870177103 2016-07-10
  • 打赏
  • 举报
回复
SetConsoleTextAttribute或者system的color命令?
VTK用户指南 版本4.0 William J. Schroeder 1998-2000 第一部分 VTK 介绍 第1章 欢迎 机构-----------------------------------------------------------------------------------------------8 怎样使用VTK----------------------------------------------------------------------------------8 附加资源-----------------------------------------------------------------------------------------8 第2章 安装 2.1 概述-----------------------------------------------------------------------------------------------9 2.2 安装VTK到Windows9x/NT/ME/2000/XP------------------------------------------------9 二进制安装-------------------------------------------------------------------------------------9 源代码安装-------------------------------------------------------------------------------------9 2.3 安装VTK到Unix操作系统 源代码安装------------------------------------------------------------------------------------10 运行CMake------------------------------------------------------------------------------------11 编译源代码 建立VTK多平台 安装VTK 第3章 系统概述 3.1 系统设计---------------------------------------------------------------------------------------12 图形模型--------------------------------------------------------------------------------------13 可视化模型-----------------------------------------------------------------------------------15 3.2 创建一个应用---------------------------------------------------------------------------------19 用户方法、对象和命令--------------------------------------------------------------------19 Tcl----------------------------------------------------------------------------------------------19 C++---------------------------------------------------------------------------------------------20 Java Phthon Visual Basic/COM/ActiveX 3.3 在两种语言间转换 第二部分 通过例子学习VTK 第4章 基础 4.1 创建1个简单的模型-------------------------------------------------------------------------24 程序化源对象---------------------------------------------------------------------------------24 读取源对象------------------------------------------------------------------------------------26 4.2 使用VTK交互器-----------------------------------------------------------------------------27 vtk绘制窗口交互器 交互风格 4.3 滤波数据---------------------------------------------------------------------------------------29 4.4 控制相机---------------------------------------------------------------------------------------30 安装相机 简单操作方法 控制视角方向 透视与正交视 保存与恢复相机状态 4.5 控制光线---------------------------------------------------------------------------------------32 位置光 4.6 控制3D道具-----------------------------------------------------------------------------------32 指定vtk道具3D位置 演员 演员的详细级 装配 体 vtk装载3D道具 4.7 作用纹理---------------------------------------------------------------------------------------37 4.8 拾取---------------------------------------------------------------------------------------------38 vtk装配路线 例子 4.9 vtk坐标和坐标系---------------------------------------------------------------------------40 4.10 控制vtk演员2D----------------------------------------------------------------------------41 4.11 注释--------------------------------------------------------------------------------------------41 2D注释 3D注释和vtk跟踪 4.12 特殊绘图类-----------------------------------------------------------------------------------44 尺度棒 X-Y绘制 边界盒轴 标记数据 4.13 变换数据--------------------------------------------------------------------------------------48 高级变换 第5章 可视化技术 5.1 可视化VTK数据集vtkDataSet(和子类) -------------------------------------------------50 使用数据属性进行工作 颜色映射 轮廓化 浮雕化 流线图 流线表面 剪裁 融合数据 附加数据 用另外一个尺度给等值面赋颜色 抽取单元格子集 抽取单元格作为多边形数据 5.2 可视化多边形数据---------------------------------------------------------------------------67 手工产生多边形数据 产生表面当量 十比一抽取 平滑网格 粘贴数据 产生纹理坐标 5.3 可视化结构网格-----------------------------------------------------------------------------74 手工产生结构化网格 抽取计算平面 结构网格子样化 5.4 可视化直线网格-----------------------------------------------------------------------------76 手工产生VTK直线网格 抽取计算平面 5.5 可视化非结构网格--------------------------------------------------------------------------77 手工产生VTK非结构网格 抽取部分网格 非结构网格轮廓化 第6章 可视化图像和体数据 6.1 VTK结构化点的历史表示-----------------------------------------------------------------80 6.2 手工产生VTK图像数据-------------------------------------------------------------------80 6.3 抽取图像数据子样--------------------------------------------------------------------------81 6.4 基于尺度值的弯曲--------------------------------------------------------------------------83 6.5 图像显示--------------------------------------------------------------------------------------83 图像观察者 图像演员 6.6 图像源-----------------------------------------------------------------------------------------85 2D帆布图像源 3D椭圆体图像源 高斯图像源 网格图像源 噪声图像源 正弦曲线源 6.7 图像处理--------------------------------------------------------------------------------------88 梯度化 高斯平滑 直方图 图像逻辑 重新切片 6.8 体绘制-----------------------------------------------------------------------------------------92 一个简单的例子 为什么会有多种体绘制技术? 产生一个VTK体 使用片层化函数 使用颜色变换函数 在一个体属性中控制颜色和透明度 在一个体属性中控制阴影 产生一个体映射 裁剪一个体 粘贴一个体 对一个体应用3D纹理 控制标准编码 体素光线计算 2D纹理映射 VolumePro绘制硬件 速度和精确度交替使用 使用vtkLODProp3D改善性能 可行性/局限性技术 第7章 建立模型 7.1 隐模型----------------------------------------------------------------------------------------114 定义隐函数 对隐函数进行抽样 7.2 挤压-------------------------------------------------------------------------------------------117 7.3 构建表面-------------------------------------------------------------------------------------119 Delaunay三角形化 高斯油彩 无组织点产生表面 第三部分 VTK研发者指南 第8章 数据接口和其他 8.1 读入器----------------------------------------------------------------------------------------130 多边形数据读入器 图像和体素读入器 数据集读入器 结构化网格读入器 线性网格读入器 非结构化网格读入器 8.2 写入器----------------------------------------------------------------------------------------131 多边形数据读入器 图像和体素读入器 结构化网格读入器 线性网格读入器 非结构化网格读入器 8.3 输入者----------------------------------------------------------------------------------------132 8.4 输出者----------------------------------------------------------------------------------------132 8.5 创建硬拷贝----------------------------------------------------------------------------------132 保存图像 保存大(高分辨率)图像 8.6 产生动画(使用样条) -----------------------------------------------------------------------134 8.7 使用现场数据工作--------------------------------------------------------------------------136 第9章 贡献编码 9.1 编码补偿--------------------------------------------------------------------------------------141 为VTK贡献编码的条件 编码风格 如何贡献编码 9.2 标准方法: 创建和消除对象---------------------------------------------------------------142 9.3 拷贝对象和受保护的方法------------------------------------------------------------------143 9.4 写一个VTK类: 综述-----------------------------------------------------------------------144 找到一个相似类 识别一个超类 单个类Per.h 文件 必需的方法 文档编码 使用SetGet宏 向VTK中添加类 9.5 对象工厂--------------------------------------------------------------------------------------145 综述 如何写一个工厂 如何安装一个工厂 例子工厂 第10章 流水线执行管理 10.1 执行过程--------------------------------------------------------------------------------------151 概述和术语 更新信息通道 传播更新扩展通道 触发异步更新通道 更新数据通道 10.2 使用流---------------------------------------------------------------------------------------162 第11章 VTK数据对象接口 11.1 数据组---------------------------------------------------------------------------------------166 方法 11.2 数据集---------------------------------------------------------------------------------------169 11.3 VTK数据集接口---------------------------------------------------------------------------170 方法 例子 11.4 VTK图像数据接口-----------------------------------------------------------------------174 方法 例子 11.5 VTK点集接口-----------------------------------------------------------------------------176 方法 例子 11.6 VTK结构化网格接口---------------------------------------------------------------------178 方法 例子 11.7 VTK线性网格接口-----------------------------------------------------------------------178 方法 例子 11.8 VTK多边形数据接口---------------------------------------------------------------------179 方法 例子 11.9 VTK非结构化网格接口-----------------------------------------------------------------184 方法 例子 11.10 单元格接口(VTK单元格子类) ------------------------------------------------------185 11.11 其他接口----------------------------------------------------------------------------------187 点 单元格数组 单元格类型 单元格连接 11.12 现场和属性数据接口------------------------------------------------------------------193 现场数据方法 数据集属性方法 第12章 如何写一个过程方法 12.1 概述----------------------------------------------------------------------------------------196 永远不要修改输入数据 参考计数数据 使用Debug宏 回收/删除截入的内在 修改时间 过程事件和异常终止执行 12.2 如何写一个绘图过滤器---------------------------------------------------------------199 概述 简单过滤器 复杂过滤器和流水线执行 抽取绘图过滤器 程序过滤器 重载流水执行方法 12.3 如何写一个图像过滤器---------------------------------------------------------------210 实现一个图像过滤器 第13章 用窗口系统集成 13.1 绘制窗口交互风格--------------------------------------------------------------------------216 13.2 GUI交互的总指导线------------------------------------------------------------------------217 13.3 X Window, Xt, and Motif--------------------------------------------------------------------221 13.4 MS Windows/Microsoft Foundation Classes---------------------------------------------226 13.5 Tcl/Tk-------------------------------------------------------------------------------------------227 13.6 Java 第14章 编码资源 14.1 对象图表--------------------------------------------------------------------------------------230 基础 单元格 数据集 流水线 源 过滤器 映射器 图形 体绘制 成像 OpenGL绘制器 拾取 变换塔形结构 14.2 过滤器总结-----------------------------------------------------------------------------------237 可视化过滤器 映射者对象 演员对象 14.3 VTK文件格式--------------------------------------------------------------------------------244 二进制文件 数据集属性格式 例子 第15章 光盘 15.1 源代码 15.2 例子代码 15.3 Window 9x/NT/ME/2000/XP 预编译二进制 15.4 数据 15.5 文档 15.6 退化测试图像 15.7 Kitware 应用

24,854

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 工具平台和程序库
社区管理员
  • 工具平台和程序库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧