【问题标题】:How to change text color and console color in code::blocks?如何更改 code::blocks 中的文本颜色和控制台颜色?
【发布时间】:2015-06-16 23:08:58
【问题描述】:

我正在用 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 没有定义。为什么会这样?我在 GNU GCC 编译器和 Windows Vista 中工作。如果它不起作用,那么 textcolor 的副本是什么。像这样我想改变控制台的背景颜色。编译器给了我同样的错误,只是函数的名称不同。如何更改控制台和文本的颜色。请帮忙。

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

【问题讨论】:

  • 什么是textcolor
  • 这是一个用来改变文本颜色的函数。
  • 喜欢在行尾放置换行符...printf(\tThis is example.\n\n");

标签: c++ c gcc colors codeblocks


【解决方案1】:

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 中对我有用。希望它也对你有用。

【讨论】:

    【解决方案2】:

    你也可以使用rlutil:

    • 跨平台,
    • 仅标题 (rlutil.h),
    • 适用于 C 和 C++,
    • 实现setColor()cls()getch()gotoxy()
    • 许可证:WTFPL

    你的代码会变成这样:

    #include <stdio.h>
    
    #include "rlutil.h"
    
    int main(int argc, char* argv[])
    {
        setColor(BLUE);
        printf("\n \n \t This is dummy program for text color ");
        getch();
    
        return 0;
    }
    

    查看 example.ctest.cpp 以获取 C 和 C++ 示例。

    【讨论】:

    • 这是一个相当不错的小图书馆。我只用了 5 分钟就将其调整为在 Visual Studio unicode 模式下工作并且效果很好。
    • 如何将这些颜色值应用于文件打印变量??请告诉我。
    【解决方案3】:

    我知道,我很晚了,但是,也许我的回答可以帮助别人。 基本上它非常简单。 这是我的代码。

    #include<iostream>
    #include<windows.h>
    using namespace std;
    int main()
    {
        HANDLE colors=GetStdHandle(STD_OUTPUT_HANDLE);
    
        string text;
        int k;
        cout<<" Enter your Text : ";
        getline(cin,text);
        for(int i=0;i<text.length();i++)
        {
            k>9 ? k=0 : k++;
    
            if(k==0)
            {
                SetConsoleTextAttribute(colors,1);
            }else
            {
                SetConsoleTextAttribute(colors,k);
            }
            cout<<text.at(i);
        }
    }
    

    输出

    This Image will show you how it works

    如果您想要完整的教程,请在此处观看我的视频:How to change Text color in C++

    【讨论】:

      【解决方案4】:

      这是一个在线函数,我用它创建了一个头文件,我用Setcolor();代替,希望对你有帮助!您可以通过选择 0-256 范围内的任何颜色来更改颜色。 :) 可悲的是,我相信 CodeBlocks 有更高版本的 window.h 库...

      #include <windows.h>            //This is the header file for windows.
      #include <stdio.h>              //C standard library header file
      
      void SetColor(int ForgC);
      
      int main()
      {
          printf("Test color");       //Here the text color is white
          SetColor(30);               //Function call to change the text color
          printf("Test color");       //Now the text color is green
          return 0;
      }
      
      void SetColor(int ForgC)
      {
           WORD wColor;
           //This handle is needed to get the current background attribute
      
           HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
           CONSOLE_SCREEN_BUFFER_INFO csbi;
           //csbi is used for wAttributes word
      
           if(GetConsoleScreenBufferInfo(hStdOut, &csbi))
           {
                //To mask out all but the background attribute, and to add the color
                wColor = (csbi.wAttributes & 0xF0) + (ForgC & 0x0F);
                SetConsoleTextAttribute(hStdOut, wColor);
           }
           return;
      }
      

      【讨论】:

        【解决方案5】:

        textcolor 函数在最新的编译器中不再支持。

        这是更改代码块中文本颜色的最简单方法。 你可以使用system函数。

        更改文本颜色:

        #include<stdio.h> 
        #include<stdlib.h> //as system function is in the standard library
        
        int main()        
                {
                  system("color 1"); //here 1 represents the text color
                  printf("This is dummy program for text color");
                  return 0;
                }
        

        如果您想同时更改文本颜色和控制台颜色,您只需在 system 函数中添加另一个颜色代码

        更改文本颜色和控制台颜色:

        system("color 41"); //here 4 represents the console color and 1 represents the text color
        

        注意:不要在这些颜色代码之间使用空格

        system("color 4 1");
        

        如果你这样做,代码块将显示所有颜色代码。您可以使用此技巧了解所有支持的颜色代码。

        【讨论】:

          【解决方案6】:

          您应该在之前定义函数 textcolor。因为 textcolor 不是 C 中的标准函数。

          void textcolor(unsigned short color) {
              HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);
              SetConsoleTextAttribute(hcon,color);
          }
          

          【讨论】:

          • GetStdHandleSetConsoleTextAttribute 也不是 C 中的标准函数。
          • 但是GetStdHandleSetConsoleTextAttribute 是Windows API。
          • 我先用了你的例子,并在我的程序中尝试过。颜色来了,但它周围有一个边框。没有预期。相反,您会看到我的答案。更好。
          【解决方案7】:

          简单的方法...

          system("Color F0");
          

          字母代表背景颜色,数字代表文字颜色。

          0 = 黑色

          1 = 蓝色

          2 = 绿色

          3 = 水色

          4 = 红色

          5 = 紫色

          6 = 黄色

          7 = 白色

          8 = 灰色

          9 = 浅蓝色

          A = 浅绿色

          B = 浅水色

          C = 浅红色

          D = 浅紫色

          E = 浅黄色

          F = 亮白色

          【讨论】:

            【解决方案8】:

            system("COLOR 0A");'

            其中 0A 是背景和字体颜色的组合 0

            【讨论】:

              猜你喜欢
              • 2016-03-04
              • 2022-01-16
              • 2020-10-06
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2012-06-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多