【问题标题】:how to change colour of a specific output on console in c++如何在 C++ 中更改控制台上特定输出的颜色
【发布时间】:2015-08-19 04:09:10
【问题描述】:

//下面的代码一旦改变颜色,就会一直保持下去,//如果我想写一个不同颜色的单词,比如 cout

#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;**

【问题讨论】:

  • Windows? Linux?操作系统?独立于平台?
  • 我不太了解,我用的是visual studio 2012
  • 我使用诅咒。如果为它构建一个包装类(它是 c 代码),使用起来相当简单。
  • 我想要 c++ 代码,我从网站上得到了所有这些代码,实际上是从这里发布的一个问题中得到的,但是这些代码一旦改变了它们保持不变的颜色,我不知道如何重置,我想要一个单词为不同的颜色,其余为相同的原始颜色,默认为控制台输出颜色

标签: c++ windows


【解决方案1】:

如果你想要red 文本到black 背景,你只需输入SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),4); cout&lt;&lt;"4th June";

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

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

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

#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



  */

【讨论】:

    猜你喜欢
    • 2012-12-27
    • 1970-01-01
    • 2016-01-23
    • 1970-01-01
    • 2013-06-27
    • 2014-01-10
    • 1970-01-01
    • 2020-10-06
    • 1970-01-01
    相关资源
    最近更新 更多