How to set console font color

Source: Internet
Author: User

You can use the Setconsoletextattribute function to set the console font color, underline format, etc.

1. Setconsoletextattribute function prototype
1 BOOL WINAPI setconsoletextattribute (2     _in_  HANDLE hconsoleoutput,  //  Console output stream handle 3     _in_  WORD wattributes        //  Set Property 4     );
    • hconsoleoutput [Input parameters]

The handle to the output stream of the console screen (handle to console-screens buffer). The handle of this file stream must have write (generic_read) permission. For more information, see console Buffer Security and Access Rights on MSDN

    • wattributes [Input parameters]

The specific common settings are as follows:
attribute meaning

Foreground_blue foreground color contains blue
Foreground_green foreground color contains green
foreground_red foreground color contains red
Foreground_intensity Front View Enhancement
Background_blue background color contains blue
Background_green background color contains green
background_red background color contains red
background_intensity Background Color Enhancement
Common_lvb_grid_horizontal Top Horizontal Grid
Common_lvb_grid_lvertical Left vertical grid
Common_lvb_grid_rvertical Right Vertical grid
Common_lvb_underscore Underline

2. Example
1#include <Windows.h>2#include <iostream>3 voidMain ()4 {5 HANDLE HOut;6HOut =GetStdHandle (std_output_handle);7 8Std::cout <<"Normal color Look"<<Std::endl;9 Ten Setconsoletextattribute (HOut, OneForeground_green |//Foreground Color _ green Aforeground_intensity);//Foreground Color Reinforcement -Std::cout <<"Light green is set, and it's always light green after it's done."<<Std::endl; -  the Setconsoletextattribute (HOut, -Foreground_blue |//Foreground Color _ blue -foreground_intensity |//Foreground Color Reinforcement -Common_lvb_underscore);//Add underline +Std::cout <<"text blue, plus an underscore"<<Std::endl; -  + Setconsoletextattribute (HOut, Aforeground_red |//Foreground Color _ red atforeground_intensity |//Foreground Color Reinforcement -Background_blue);//background Color _ blue -Std::cout <<"set text red, background blue"<<Std::endl; -  -  - Setconsoletextattribute (HOut, inforeground_red |//Foreground Color _ red -foreground_intensity |//Foreground Color Reinforcement tocommon_lvb_grid_lvertical);//Grid _ left _ vertical +Std::cout <<"Add left Grid"<<Std::endl;  -  the Setconsoletextattribute (HOut, *foreground_red |//Foreground Color _ red $foreground_intensity |//Foreground Color ReinforcementPanax Notoginsengcommon_lvb_grid_rvertical);//Grid _ Right _ vertical -Std::cout <<"Add right Grid"<<Std::endl;  the  + Setconsoletextattribute (HOut, Aforeground_red |//Foreground Color _ red theForeground_green |//Foreground Color _ green +Foreground_blue);//Foreground Color _ blue -Std::cout <<"change back to white"<< Std::endl;

Original address: Http://www.lellansin.com/c%E8%AF%AD%E8%A8%80-%E8%AE%BE%E7%BD%AE%E6%8E%A7%E5%88%B6%E5%8F%B0%E5%AD%97%E4%BD %93%e9%a2%9c%e8%89%b2-setconsoletextattribute.html

How to set console font color

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.