如何在每一行数组C++中进行颜色更改
How to make color changes in every line array C++
我有一个问题要问这个网站上的每个人。我想在每一行中更改数组语句的颜色。我的意思是,数组语句的每一行都有不同的颜色。这是我的程序
#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<<"nn Input array index : ";
cin>>x;
cout<<"Character you are looking for is "<<data[x];
}
getch();
}
我想在cout<<"nn Input array index : ";
语句和cout<<"Character you are looking for is "<<data[x];
语句中更改颜色。请帮帮我:)
您可以使用像SetConsoleTextAttribute
:这样的Windows控制台功能
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN);
std::cout << "This text should be green" << std::endl;
免责声明:我真的不知道(也无法测试)它是否能与C++标准输出流一起工作,或者你是否必须使用WriteConsole
如果使用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<<"nn 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<<"nn Input array index : ";
cin>>x;
cout<<"Character you are looking for is "<<data[x];
}
}
小建议不要使用conio
,conio.h
确实不是标准的C头。
相关文章:
- 将"打开的CV图像"中的"颜色"转换为整数格式
- 如何在内核C++中使用1920x1080x16M图形或类似的16M颜色?(VGA)
- 如何在24位SDL_Surface上设置像素的颜色
- 如何从SDL_Surface获取特定像素的颜色
- 列表视图更改选择颜色
- GtkTreeView 交替行颜色
- dx11 渲染到纹理仅显示透明颜色
- 使用 OpenGL 4.5 更改所选顶点的颜色
- 为什么我的 LEGACY OPENGL 颜色反转了?
- 双击更改 mfc 中列表控件中的行的颜色
- 更改 QT 中按钮的颜色
- 当我使用CHAR_INFO结构时,控制台会无缘无故地改变颜色
- 列表视图项的替代颜色
- GLSL 将 uint 转换为浮点颜色
- std::带有颜色和标题的 clog 包装器无法正确打印整数
- Vec3b:r/g/b 颜色的最大值为 254,而不是 255
- 如何将矢量对象更改为不同的颜色?
- PPM 颜色显示错误
- 如何更改 QGraphicsItem 对象的颜色?
- 如何防止每次 Arduino 启动时颜色数组都是相同的随机数组