Test.cpp: Defines the entry point of the console application. #include "stdafx.h" #include <windows.h> #include <conio.h>void setcolor (unsigned short ForeColor, unsigned short backgroundcolor) {HANDLE hcon=getstdhandle (std_output_handle); WORD WR;//Here you can grasp switch (backgroundcolor) {case ' 1 ': WR = foreground_intensity | Foreground_red;break;case ' 2 ': WR = foreground_intensity | FOREGROUND_GREEN;BREAK;DEFAULT:WR = Foreground_intensity | Foreground_blue;break;} /*setconsoletextattribute (Hcon, (forecolor%16) | ( backgroundcolor%16*16)); */setconsoletextattribute (HCON,WR);} int _tmain (int argc, _tchar* argv[]) {int Color;char i = ' 1 ';/* HANDLE HANDLE = GetStdHandle (Std_output_handle); Setconsoletextattribute (Handle, Foreground_intensity | foreground_red); printf ("Turn the color of this sentence red \ n"); Setconsoletextattribute (Handle, Foreground_intensity | Foreground_green); printf ("Turn this phrase into yellow \ n"); Setconsoletextattribute (Handle, Foreground_intensity | Foreground_blue); printf ("Turn this phrase into yellow \ n"); system ("pause"); */for (color = 0; color < 8; color++)/* Set color 0 to 15*/{printf ("no.%d\r\n", color); /*\r\n represents a newline */system ("pause"); SetColor (0,i);p rintf ("Come on, China!"); *setcolor (7,0);///revert to white on black background. */i++;} return 0;}
How to use C to change part of the font color in VS