Mac OS效果


Windows 效果

想写这个东西其实是因为最近要写个命令行的工具,但是有个问题是什么呢?就是传统的那个黑漆漆的窗口看起来很蛋疼。并且完全看不到重点,于是就想起
来这么一个东西。相对来说针对*nix的系统方法会比较通用一些,而windows下这个东西需要用到专门的Windows相关的api来实现。

下面先说通用的方法:

1.*nix (Linux/Unix/Mac OS)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
//
// main.cpp
// ColoredHelloWorld
//
// Created by obaby on 14-2-27.
// Copyright (c) 2014年 mars. All rights reserved.
//
 
#include <iostream>
 
//the following are UBUNTU/LINUX ONLY terminal color codes.
#define RESET "\033[0m"
#define BLACK "\033[30m" /* Black */
#define RED "\033[31m" /* Red */
#define GREEN "\033[32m" /* Green */
#define YELLOW "\033[33m" /* Yellow */
#define BLUE "\033[34m" /* Blue */
#define MAGENTA "\033[35m" /* Magenta */
#define CYAN "\033[36m" /* Cyan */
#define WHITE "\033[37m" /* White */
#define BOLDBLACK "\033[1m\033[30m" /* Bold Black */
#define BOLDRED "\033[1m\033[31m" /* Bold Red */
#define BOLDGREEN "\033[1m\033[32m" /* Bold Green */
#define BOLDYELLOW "\033[1m\033[33m" /* Bold Yellow */
#define BOLDBLUE "\033[1m\033[34m" /* Bold Blue */
#define BOLDMAGENTA "\033[1m\033[35m" /* Bold Magenta */
#define BOLDCYAN "\033[1m\033[36m" /* Bold Cyan */
#define BOLDWHITE "\033[1m\033[37m" /* Bold White */
 
int main(int argc, const char * argv[])
{
 
// insert code here...
std::cout< <RED <<"Hello, World! in RED\n";
std::cout<<GREEN <<"Hello, World! in GREEN\n";
std::cout<<YELLOW <<"Hello, World! in YELLOW\n";
std::cout<<BLUE <<"Hello, World! in BLUE\n";
std::cout<<MAGENTA <<"Hello, World! in MAGENTA\n";
std::cout<<CYAN <<"Hello, World! in CYAN\n";
std::cout<<WHITE <<"Hello, World! in WHITE\n";
std::cout<<BOLDRED <<"Hello, World! in BOLDRED\n";
std::cout<<BOLDCYAN <<"Hello, World! in BOLDCYAN\n";
return ;
}

2.Windows下面要用到一个api叫做:SetConsoleTextAttribute方法也比较简单。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// ColordCout.cpp : Defines the entry point for the console application.
//
 
#include "stdafx.h"
#include <iostream>
#include <windows .h>
 
using namespace std;
 
void SetColor(unsigned short forecolor = ,unsigned short backgroudcolor =)
{
HANDLE hCon =GetStdHandle(STD_OUTPUT_HANDLE); //获取缓冲区句柄
SetConsoleTextAttribute(hCon,forecolor|backgroudcolor); //设置文本及背景色
}
 
int _tmain(int argc, _TCHAR* argv[])
{
SetColor(,);
std::cout < <"Colored hello world for windows!\n";
SetColor(,);
std::cout <<"Colored hello world for windows!\n";
SetColor(,);
std::cout <<"Colored hello world for windows!\n";
return ;
}

std::cout彩色输出的更多相关文章

  1. 使用std::cout不能输出显示

    在测试一行函数时,出现std::cout不能输出的情况,找不到原因. 1.         只好从main函数第一行,开始测试,直到发现一个函数 o_Initer.ekf_filter(filter, ...

  2. Visual Studio将std::cout输出到Output窗口

    在debug的时候,输出到Output需要使用OutputDebugString函数,但部分库的log是采用std::cout输出的,需要用控制台(黑窗)程序来查看输出.有没有一种使用GUI和Outp ...

  3. 02 - 用wxStreamToTextRedirector和wxTextCtrl输出std::cout

    遇到问题,单行显示, new line丢失 原因: wxTextCtrl默认是单行的 解决办法:使用wxTE_MULTILINE参数初始化wxTextCtrl wxTextCtrl *text = , ...

  4. cout 格式化输出

    一直习惯于C语言的printf函数来打印,突然有一天要用cout来打印,发现有点不适应. 原来cout也是有格式化输出的. 首先要引入头文件 #include<iostream> // 在 ...

  5. C++输入cout与输出cin

    输入和输出并不是C++语言中的正式组成成分.C和C++本身都没有为输入和输出提供专门的语句结构.输入输出不是由C++本身定义的,而是在编译系统提供的I/O库中定义的.C++的输出和输入是用" ...

  6. [转载] c++ cout 格式化输出浮点数、整数及格方法

    C语言里可以用printf(),%f来实现浮点数的格式化输出,用cout呢...? 下面的方法是在网上找到的,如果各位有别的办法谢谢留下... iomanip.h是I/O流控制头文件,就像C里面的格式 ...

  7. std::cout和printf

    禁止std::cout和printf混用,在多线程环境下可能导致coredump. 说明:printf和std::cout分别为标准c语言与c++中的函数,两者的缓冲区机制不同(printf无缓冲区, ...

  8. C++ cout格式化输出(转)

    C++ cout格式化输出(转) 这篇文章主要讲解如何在C++中使用cout进行高级的格式化输出操作,包括数字的各种计数法(精度)输出,左或右对齐,大小写等等.通过本文,您可以完全脱离scanf/pr ...

  9. std::cout << char + int

    #include<iostream> int main(){ char ch; std::cout << "Type, and I shall repeat.\n&q ...

随机推荐

  1. linux 命令(1)screen

    一.screen的安装和用法 Screen是一个可以在多个进程之间多路复用一个物理终端的窗口管理器,这意味着你能够使用一个单一的终端窗口运行多终端的应用. Screen中有会话的概念,用户可以在一个s ...

  2. CLR via C#笔记

    第一部分 CLR基础 CLR和JIT “运行时”如何解析类型引用 第二部分 设计类型 使用C#的is和as操作符来转型 运行时的相互联系 基元类型.引用类型和值类型 值类型的装箱和拆箱 哈希码 dyn ...

  3. Windows服务程序和安装程序制作

    转:http://www.cr173.com/html/15350_1.html 本文介绍了如何用C#创建.安装.启动.监控.卸载简单的Windows Service 的内容步骤和注意事项. 一.创建 ...

  4. eclipse升级,导入旧版eclipse的插件[转]

    启动 eclipse.菜单 File => import … => Install => From existing Installation, 点确定, 就会弹出对话框, 浏览选择 ...

  5. ylbtech-Unitity-CS:Delegates

    ylbtech-Unitity-CS:Delegates 1.A,效果图返回顶部 Invoking delegate a: Hello, A! Invoking delegate b: Goodbye ...

  6. Unable to save settings: Failed to save settings. Please restart IntelliJ IDEA 解决方案

    ubuntu 上 今天安idea 社区版,报这个错, 解决办法. 1 首先删除掉配置目录,例如安装的是idea最新的15.1版本,就删除 .ideaIC这个文件夹,重启解决了 2 而后,不大一会.又开 ...

  7. Package 'chkconfig' has no installation candidate

    Chkcofig不再适用于Ubuntu系统,可用类似的软件sysv-rc-conf进行替换: Chkconfig is no longer available in Ubuntu. Chkconfig ...

  8. POJ 2154 【POLYA】【欧拉】

    前记: TM终于决定以后干啥了.这几天睡的有点多.困饿交加之间喝了好多水.可能是灌脑了. 切记两件事: 1.安心当单身狗 2.顺心码代码 题意: 给你N种颜色的珠子,串一串长度问N的项链,要求旋转之后 ...

  9. LeetCode Lowest Common Ancestor of a Binary Serach Tree

    Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...

  10. iOS 进入后台的处理

    当App进入后台时(按下Home键), App会被系统暂停, 所有的程序逻辑都会停止, App还是驻留内存中, 除非被用户强制退出, 或者被系统kill掉(为了保证正在前台运行的App有足够的内存, ...