zoukankan      html  css  js  c++  java
  • C++ 输出彩色的控制台

    输出彩色的控制台文字

    #include <iostream>
    #include <Windows.h>
    #include <stdio.h>
    #include <stdarg.h>
    
    using namespace std;
    
    void cprintf(const char* str, WORD color, ...);
    void testPrintf();
    
    int main() {
        cprintf("H", 10);
        cprintf("e", 9);
        cprintf("l", 12);
        cprintf("l", 11);
        cprintf("o", 13);
        cprintf(" ", 10);
        cprintf("W", 15);
        cprintf("o", 2);
        cprintf("r", 5);
        cprintf("l", 8);
        cprintf("d", 14);
        cprintf("!", 4);
    
        testPrintf();
    
        return 0;
    }
    
    void cprintf(const char* str, WORD color, ...) {
        WORD colorOld;
        HANDLE handle = ::GetStdHandle(STD_OUTPUT_HANDLE);
        CONSOLE_SCREEN_BUFFER_INFO csbi;
        GetConsoleScreenBufferInfo(handle, &csbi);
        colorOld = csbi.wAttributes;
        SetConsoleTextAttribute(handle, color);
        cout << str;
        SetConsoleTextAttribute(handle, colorOld);
    }
    
    void testPrintf() {
    
        WORD colorOld;
        HANDLE handle = ::GetStdHandle(STD_OUTPUT_HANDLE);
        CONSOLE_SCREEN_BUFFER_INFO csbi;
        GetConsoleScreenBufferInfo(handle, &csbi);
        colorOld = csbi.wAttributes;
    
        cout << endl;
        //https://docs.microsoft.com/zh-cn/windows/console/console-screen-buffers#character-attributes
        SetConsoleTextAttribute(handle, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY | BACKGROUND_BLUE);//以下组合实现:蓝色背景上的亮青色文本。
        cout << "123" << endl;
        SetConsoleTextAttribute(handle, BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED);//以下组合实现:白色背景上的黑色文本。
        cout << "456" << endl;
        SetConsoleTextAttribute(handle, colorOld);//恢复原来样式
    }

    转: https://www.cnblogs.com/finlay/archive/2013/06/09/3234729.html

  • 相关阅读:
    http://www.bugku.com:Bugku——SQL注入1(http://103.238.227.13:10087/)
    [笔记]一道C语言面试题:大整数乘法
    [笔记] Access Control Lists (ACL) 学习笔记汇总
    [笔记]如何将传统的回调函数转换为C#5.0支持的await格式
    6.链接与导航
    9章 下拉菜单
    11章圆角框 本章很重要 经常用到
    原来链接与导航
    7竖直排列的导航菜单
    8.水平导航菜单
  • 原文地址:https://www.cnblogs.com/fps2tao/p/15078195.html
Copyright © 2011-2022 走看看