gpt4 book ai didi

c++ - 如何更改 code::blocks 中的文本颜色和控制台颜色?

转载 作者:可可西里 更新时间:2023-11-01 17:15:48 24 4
gpt4 key购买 nike

我正在用 C 编写程序。我想在控制台中更改文本颜色和背景颜色。我的示例程序是 -

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <dos.h>
#include <dir.h>

int main(int argc,char *argv[])
{
textcolor(25);
printf("\n \n \t This is dummy program for text color ");
getch();

return 0;
}

当我编译这个程序时,code::blocks 给我一个错误 - textcolor not defined。为什么会这样?我在 GNU GCC 编译器和 Windows Vista 中工作。如果它不起作用,文本颜色的拷贝是什么。就像那样我想改变控制台的背景颜色。编译器给我同样的错误只是函数的名称不同。如何更改控制台和文本的颜色。请帮忙。

即使答案是用 C++ 我也可以。

最佳答案

textcolor 等函数可在 turbo CDev C 等旧编译器中使用。在今天的编译器中,这些函数将不起作用。我将提供两个函数 SetColorChangeConsoleToColors。您将这些函数代码复制粘贴到您的程序中,然后执行以下步骤。我提供的代码在某些编译器中将无法运行。

SetColor 的代码是 -

 void SetColor(int ForgC)
{
WORD wColor;

HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbi;

//We use csbi for the wAttributes word.
if(GetConsoleScreenBufferInfo(hStdOut, &csbi))
{
//Mask out all but the background attribute, and add in the forgournd color
wColor = (csbi.wAttributes & 0xF0) + (ForgC & 0x0F);
SetConsoleTextAttribute(hStdOut, wColor);
}
return;
}

要使用此函数,您需要从您的程序中调用它。例如,我正在使用您的示例程序 -

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <dos.h>
#include <dir.h>

int main(void)
{
SetColor(4);
printf("\n \n \t This text is written in Red Color \n ");
getch();
return 0;
}

void SetColor(int ForgC)
{
WORD wColor;

HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbi;

//We use csbi for the wAttributes word.
if(GetConsoleScreenBufferInfo(hStdOut, &csbi))
{
//Mask out all but the background attribute, and add in the forgournd color
wColor = (csbi.wAttributes & 0xF0) + (ForgC & 0x0F);
SetConsoleTextAttribute(hStdOut, wColor);
}
return;
}

当您运行该程序时,您将获得红色的文本颜色。现在我要给你每种颜色的代码 -

Name         | Value
|
Black | 0
Blue | 1
Green | 2
Cyan | 3
Red | 4
Magenta | 5
Brown | 6
Light Gray | 7
Dark Gray | 8
Light Blue | 9
Light Green | 10
Light Cyan | 11
Light Red | 12
Light Magenta| 13
Yellow | 14
White | 15

现在我要给出ChangeConsoleToColors的代码。代码是——

void ClearConsoleToColors(int ForgC, int BackC)
{
WORD wColor = ((BackC & 0x0F) << 4) + (ForgC & 0x0F);
//Get the handle to the current output buffer...
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
//This is used to reset the carat/cursor to the top left.
COORD coord = {0, 0};
//A return value... indicating how many chars were written
// not used but we need to capture this since it will be
// written anyway (passing NULL causes an access violation).
DWORD count;

//This is a structure containing all of the console info
// it is used here to find the size of the console.
CONSOLE_SCREEN_BUFFER_INFO csbi;
//Here we will set the current color
SetConsoleTextAttribute(hStdOut, wColor);
if(GetConsoleScreenBufferInfo(hStdOut, &csbi))
{
//This fills the buffer with a given character (in this case 32=space).
FillConsoleOutputCharacter(hStdOut, (TCHAR) 32, csbi.dwSize.X * csbi.dwSize.Y, coord, &count);

FillConsoleOutputAttribute(hStdOut, csbi.wAttributes, csbi.dwSize.X * csbi.dwSize.Y, coord, &count );
//This will set our cursor position for the next print statement.
SetConsoleCursorPosition(hStdOut, coord);
}
return;
}

在此函数中,您传递两个数字。如果你想要正常的颜色,只需将第一个数字作为零,将第二个数字作为颜色。我的例子是 -

#include <windows.h>          //header file for windows
#include <stdio.h>

void ClearConsoleToColors(int ForgC, int BackC);

int main()
{
ClearConsoleToColors(0,15);
Sleep(1000);
return 0;
}
void ClearConsoleToColors(int ForgC, int BackC)
{
WORD wColor = ((BackC & 0x0F) << 4) + (ForgC & 0x0F);
//Get the handle to the current output buffer...
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
//This is used to reset the carat/cursor to the top left.
COORD coord = {0, 0};
//A return value... indicating how many chars were written
// not used but we need to capture this since it will be
// written anyway (passing NULL causes an access violation).
DWORD count;

//This is a structure containing all of the console info
// it is used here to find the size of the console.
CONSOLE_SCREEN_BUFFER_INFO csbi;
//Here we will set the current color
SetConsoleTextAttribute(hStdOut, wColor);
if(GetConsoleScreenBufferInfo(hStdOut, &csbi))
{
//This fills the buffer with a given character (in this case 32=space).
FillConsoleOutputCharacter(hStdOut, (TCHAR) 32, csbi.dwSize.X * csbi.dwSize.Y, coord, &count);

FillConsoleOutputAttribute(hStdOut, csbi.wAttributes, csbi.dwSize.X * csbi.dwSize.Y, coord, &count );
//This will set our cursor position for the next print statement.
SetConsoleCursorPosition(hStdOut, coord);
}
return;
}

在这种情况下,我将第一个数字设置为零,将第二个数字设置为 15,因此控制台颜色将为白色,因为白色代码为 15。这在 code::blocks 中对我有用。希望它也对你有用。

关于c++ - 如何更改 code::blocks 中的文本颜色和控制台颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29574849/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com