Using the API to modify the color of the console output (foreground and background colors)

Source: Internet
Author: User

1. API prototype Setconsoletextattribute
BOOL WINAPI setconsoletextattribute (  _in_  HANDLE hconsoleoutput,  //  console output stream handle   _in_  WORD wattributes        //  set properties );

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:

Properties 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. Code Test
#include <windows.h>#include<stdio.h>#include<stdlib.h>intMain () {HANDLE hOut; //gets the handle to the output streamHOut =GetStdHandle (Std_output_handle); printf ("normal color see \ n"); Setconsoletextattribute (HOut, Foreground_green|//Foreground Color _ greenforeground_intensity);//Foreground Color Reinforcementprintf"The light green is set. \ n"); printf ("and it's been light green since it's done ."); Setconsoletextattribute (HOut, Foreground_blue|//Foreground Color _ blueforeground_intensity |//Foreground Color ReinforcementCommon_lvb_underscore);//Add underlineprintf"text blue, add an underscore. \ n"); Setconsoletextattribute (HOut, foreground_red|//Foreground Color _ redforeground_intensity |//Foreground Color ReinforcementBackground_blue);//background Color _ blueprintf"set text red, background blue \ n"); Setconsoletextattribute (HOut, foreground_red|//Foreground Color _ redforeground_intensity |//Foreground Color Reinforcementcommon_lvb_grid_lvertical);//Grid _ left _ verticalprintf"add left grid \ n"); Setconsoletextattribute (HOut, foreground_red|//Foreground Color _ redforeground_intensity |//Foreground Color Reinforcementcommon_lvb_grid_rvertical);//Grid _ Right _ verticalprintf"add right grid \ n"); Setconsoletextattribute (HOut, foreground_red|//Foreground Color _ redForeground_green |//Foreground Color _ greenForeground_blue);//Foreground Color _ blueprintf"change back to white \ n"); System ("Pause"); return 0; }

For example, purple can be used with blue + red:

#include <windows.h>#include<stdio.h>//For printf#include <stdlib.h>//For system   intMain () {HANDLE hOut; //gets the handle to the output streamHOut =GetStdHandle (Std_output_handle); Setconsoletextattribute (HOut, foreground_red|//Foreground Color _ redForeground_blue |//Foreground Color _ blueforeground_intensity);//Strengthenprintf"red + blue = purple ^_^ \ n"); System ("Pause"); return 0; }  
View Code

Reprinted from: Lellansin ' s Ice sen

Using the API to modify the color of the console output (foreground and background colors)

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.