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)