c – 如何在code :: blocks中更改文本颜色和控制台颜色?

前端之家收集整理的这篇文章主要介绍了c – 如何在code :: blocks中更改文本颜色和控制台颜色?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在用C编写程序.我想在控制台中更改文本颜色和背景颜色.我的示例程序是 –
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <windows.h>
  4. #include <dos.h>
  5. #include <dir.h>
  6.  
  7. int main(int argc,char *argv[])
  8. {
  9. textcolor(25);
  10. printf("\n \n \t This is dummy program for text color ");
  11. getch();
  12.  
  13. return 0;
  14. }

当我编译这个程序代码:: blocks给我一个错误 – textcolor未定义.为什么会这样?我在GNU GCC编译器和Windows Vista中工作.如果它不起作用什么是textcolor的副本.就像我想要改变控制台的背景颜色.只要函数名称不同,编译器就会给出同样的错误.如何更改控制台和文本的颜色.请帮忙.

即使答案是在C中,我也没关系.

解决方法

像textcolor这样的函数在turbo C和Dev C等旧编译器中运行.
在今天的编译器中,这些功能不起作用.我将提供两个函数SetColor和ChangeConsoleToColors.您将这些函数代码复制粘贴到程序中并执行以下步骤.我给出的代码在某些编译器中不起作用.

SetColor的代码是 –

  1. void SetColor(int ForgC)
  2. {
  3. WORD wColor;
  4.  
  5. HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
  6. CONSOLE_SCREEN_BUFFER_INFO csbi;
  7.  
  8. //We use csbi for the wAttributes word.
  9. if(GetConsoleScreenBufferInfo(hStdOut,&csbi))
  10. {
  11. //Mask out all but the background attribute,and add in the forgournd color
  12. wColor = (csbi.wAttributes & 0xF0) + (ForgC & 0x0F);
  13. SetConsoleTextAttribute(hStdOut,wColor);
  14. }
  15. return;
  16. }

要使用此功能,您需要从程序中调用它.例如我正在拿你的示例程序 –

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <windows.h>
  4. #include <dos.h>
  5. #include <dir.h>
  6.  
  7. int main(void)
  8. {
  9. SetColor(4);
  10. printf("\n \n \t This text is written in Red Color \n ");
  11. getch();
  12. return 0;
  13. }
  14.  
  15. void SetColor(int ForgC)
  16. {
  17. WORD wColor;
  18.  
  19. HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
  20. CONSOLE_SCREEN_BUFFER_INFO csbi;
  21.  
  22. //We use csbi for the wAttributes word.
  23. if(GetConsoleScreenBufferInfo(hStdOut,&csbi))
  24. {
  25. //Mask out all but the background attribute,and add in the forgournd color
  26. wColor = (csbi.wAttributes & 0xF0) + (ForgC & 0x0F);
  27. SetConsoleTextAttribute(hStdOut,wColor);
  28. }
  29. return;
  30. }

运行程序时,您将获得RED的文本颜色.现在我要给你每种颜色的代码

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

现在我要给出ChangeConsoleToColors的代码.代码是 –

  1. void ClearConsoleToColors(int ForgC,int BackC)
  2. {
  3. WORD wColor = ((BackC & 0x0F) << 4) + (ForgC & 0x0F);
  4. //Get the handle to the current output buffer...
  5. HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
  6. //This is used to reset the carat/cursor to the top left.
  7. COORD coord = {0,0};
  8. //A return value... indicating how many chars were written
  9. // not used but we need to capture this since it will be
  10. // written anyway (passing NULL causes an access violation).
  11. DWORD count;
  12.  
  13. //This is a structure containing all of the console info
  14. // it is used here to find the size of the console.
  15. CONSOLE_SCREEN_BUFFER_INFO csbi;
  16. //Here we will set the current color
  17. SetConsoleTextAttribute(hStdOut,wColor);
  18. if(GetConsoleScreenBufferInfo(hStdOut,&csbi))
  19. {
  20. //This fills the buffer with a given character (in this case 32=space).
  21. FillConsoleOutputCharacter(hStdOut,(TCHAR) 32,csbi.dwSize.X * csbi.dwSize.Y,coord,&count);
  22.  
  23. FillConsoleOutputAttribute(hStdOut,csbi.wAttributes,&count );
  24. //This will set our cursor position for the next print statement.
  25. SetConsoleCursorPosition(hStdOut,coord);
  26. }
  27. return;
  28. }

在此功能中,您传递两个数字.如果你想要正常颜色,只需将第一个数字设为零,将第二个数字设为颜色即可.我的例子是 –

  1. #include <windows.h> //header file for windows
  2. #include <stdio.h>
  3.  
  4. void ClearConsoleToColors(int ForgC,int BackC);
  5.  
  6. int main()
  7. {
  8. ClearConsoleToColors(0,15);
  9. Sleep(1000);
  10. return 0;
  11. }
  12. void ClearConsoleToColors(int ForgC,int BackC)
  13. {
  14. WORD wColor = ((BackC & 0x0F) << 4) + (ForgC & 0x0F);
  15. //Get the handle to the current output buffer...
  16. HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
  17. //This is used to reset the carat/cursor to the top left.
  18. COORD coord = {0,0};
  19. //A return value... indicating how many chars were written
  20. // not used but we need to capture this since it will be
  21. // written anyway (passing NULL causes an access violation).
  22. DWORD count;
  23.  
  24. //This is a structure containing all of the console info
  25. // it is used here to find the size of the console.
  26. CONSOLE_SCREEN_BUFFER_INFO csbi;
  27. //Here we will set the current color
  28. SetConsoleTextAttribute(hStdOut,coord);
  29. }
  30. return;
  31. }

在这种情况下,我将第一个数字设置为零,第二个数字设置为15,因此控制台颜色将为白色,因为白色的代码为15.这对我来说是代码:: blocks.希望它也适合你.

猜你在找的C&C++相关文章