如何在每个行数组C ++中进行颜色更改

时间:2015-11-17 12:08:01

标签: c++ arrays winapi

我有一个问题要问这个网站的每个人。我想在每一行中使我的数组语句颜色发生变化。我的意思是,我的数组语句的每一行都有不同的颜色。这是我的程序

#include<conio.h>
#include<windows.h>
#include<iostream>

using namespace std;

int main()
{
    system("color 0B");
    ShowWindow(GetConsoleWindow(), SW_MAXIMIZE);
    cout<<"\n Printing array index in char program";
    char data[27]={' ','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
    int x;
    for(x=0;x<28;x++)
    {
        cout<<"\n\n Input array index : ";
        cin>>x;
        cout<<"Character you are looking for is "<<data[x];
    }
    getch();
}

我希望cout<<"\n\n Input array index : ";语句和cout<<"Character you are looking for is "<<data[x];语句中的颜色发生变化。请帮帮我:)。

2 个答案:

答案 0 :(得分:2)

您可以使用console functions之类的Windows SetConsoleTextAttribute

SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN);
std::cout << "This text should be green" << std::endl;

免责声明:我真的不知道(并且无法测试)它是否适用于C ++标准输出流,或者您是否必须使用WriteConsole

答案 1 :(得分:1)

如果您使用DEALLOCATE PREPARE stmt; ,则可以使用EXECUTE stmt; 进行着色。

conio

如果您没有使用textcolor (int),请尝试使用

#include <windows.h>
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
    ShowWindow(GetConsoleWindow(), SW_MAXIMIZE);
    cout<<"\n Printing array index in char program";
    char data[27]={' ','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
    int x;
    for(x=0;x<27;x++)
    {
        textcolor(x+1);
        cout<<"\n\n Input array index : ";
        cin>>x;
        cout<<"Character you are looking for is "<<data[x];
    }
    getch();
}

小建议不使用conio#include <iostream> #include <windows.h> using namespace std; int main() { HANDLE hConsole; hConsole = GetStdHandle(STD_OUTPUT_HANDLE); ShowWindow(GetConsoleWindow(), SW_MAXIMIZE); cout<<"\n Printing array index in char program"; char data[27]={' ','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'}; int x; for(x=0;x<27;x++) { SetConsoleTextAttribute(hConsole, x+1); cout<<"\n\n Input array index : "; cin>>x; cout<<"Character you are looking for is "<<data[x]; } } 确实不是标准C标题。