如何在 C++ 中更改控制台上特定输出的颜色 [英] how to change colour of a specific output on console in c++

查看:57
本文介绍了如何在 C++ 中更改控制台上特定输出的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

//下面的代码一旦改变颜色,它们就会一直保持下去,//如果我想写一个不同颜色的单词,比如cout<<<4June"//应该是红色的jxt文本的颜色不应改变背景//color 并且如果我显示类似 cout<<"colour not changed";那么//颜色应该是原来的颜色,如何获得那个c++????

//following codes once change the colour, then they keep them along all the way, //if i want to write juxt one word with different colour like cout<<"4June" //should be with red jxt the colour of text should change not the background //colour and the if i display something like cout<<"colour not changed"; then //colour should be the original one, how to attain that c++????

#include <iostream>
#include <windows.h>
#include <winnt.h>
#include <stdio.h>
#include "stdafx.h"
using namespace std;

int main(int argc, char* argv[])
{
   HANDLE consolehwnd = GetStdHandle(STD_OUTPUT_HANDLE);
   cout << "this text is not colorized\n";
   SetConsoleTextAttribute(consolehwnd, FOREGROUND_RED);
   cout << "this text shows as red\n";
   SetConsoleTextAttribute(consolehwnd, FOREGROUND_BLUE);
   cout << "this text shows as blue\n";
}


OR

SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),1)

OR

system("color 3");
cout<<"colour changed"<<endl;**

推荐答案

如果你想要 red 文本到 black 背景,你只需输入 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),4);cout<<"6 月 4 日";

if you want red text to a black background you would just type SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),4); cout<<"4th June";

要重置为正常颜色,请将其设置为颜色 7.SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),7);

To reset back to normal color, set it to color 7. SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),7);

这是所有控制台颜色的表格.只需输入上图中的颜色代码编号即可进行 256 种组合,以获得所需的颜色.

Here is a table of all the Console colors. you can make 256 combinations by just entering the color code number from the above image to get the desired color.

下面是一些控制台颜色管理代码.

Below is some console color management code.

#include <windows.h> 
#include <iostream>
using namespace std;




void gotoxy(int x, int y);
void setcolor(WORD color);
void setForeGroundAndBackGroundColor(int ForeGroundColor,int BackGroundColor);
void clrscr(); 
void printAllColors();



int main()
{
  // set red text on black background    
  gotoxy(30,10);
  SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),4); cout<<"4th June";  

  // set white text on black background
  gotoxy(1,23);
  SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),7);



  return 0;
}


void setcolor(WORD color)
{
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),color);
    return;
}



void setForeGroundAndBackGroundColor(int ForeGroundColor,int BackGroundColor)
{
   int color=16*BackGroundColor+ForeGroundColor;
   setcolor(color);
}




void gotoxy(int x, int y)
{
    COORD coord;
    coord.X = x; coord.Y = y;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
    return;
}




void clrscr()
{
    COORD coordScreen = { 0, 0 };
    DWORD cCharsWritten;
    CONSOLE_SCREEN_BUFFER_INFO csbi;
    DWORD dwConSize;
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

    GetConsoleScreenBufferInfo(hConsole, &csbi);
    dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
    FillConsoleOutputCharacter(hConsole, TEXT(' '), dwConSize, coordScreen, &cCharsWritten);
    GetConsoleScreenBufferInfo(hConsole, &csbi);
    FillConsoleOutputAttribute(hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten);
    SetConsoleCursorPosition(hConsole, coordScreen);
    return;
}


void printAllColors()
{
   int ix=0;
   int iy=1;
   int col=0;
   setcolor(7);
   clrscr();

   // Demo setForeGroundAndBackGroundColor
   for (int i =0;i<16;i++)
   {
      for(int j=0;j<16;j++)
      {
       setForeGroundAndBackGroundColor(i,j);
       gotoxy(i*5  , iy+j); cout<<""<<i + (16 *j)<<"";
       col++;
      }
   }


   setcolor(31);
   cout<<"\n";

  gotoxy(1,23);
}


/*

Color      Background    Foreground
---------------------------------------------
Black            0           0
Blue             1           1
Green            2           2
Cyan             3           3
Red              4           4
Magenta          5           5
Brown            6           6
White            7           7
Gray             -           8
Intense Blue     -           9
Intense Green    -           10
Intense Cyan     -           11
Intense Red      -           12
Intense Magenta  -           13
Yellow           -           14
Intense White    -           15



  */

这篇关于如何在 C++ 中更改控制台上特定输出的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆