How can I change the text color in the windows command prompt

后端 未结 14 2485
南旧
南旧 2020-12-14 22:32

I have a command line program, which outputs logging to the screen.

I want error lines to show up in red. Is there some special character codes I can output to switc

相关标签:
14条回答
  • 2020-12-14 23:08

    The standard C/C++ specification for outputting to the command line doesn't specify any capabilities for changing the color of the console window. That said, there are many functions in Win32 for doing such a thing.

    The easiest way to change the color of the Win32 console is through the system command in iostream.h. This function invokes a DOS command. To change colors, we will use it to invoke the color command. For example, system("Color F1"); will make the console darkblue on white.

    DOS Colors

    The colors available for use with the command are the sixteen DOS colors each represented with a hex digit. The first being the background and the second being the foreground.

    0 = Black    8 = Gray
    1 = Blue     9 = Light Blue
    2 = Green    A = Light Green
    3 = Aqua     B = Light Aqua
    4 = Red      C = Light Red
    5 = Purple   D = Light Purple
    6 = Yellow   E = Light Yellow
    7 = White    F = Bright White
    

    Just this little touch of color makes console programs more visually pleasing. However, the Color command will change the color of the entire console. To control individual cells, we need to use functions from windows.h.

    Do do that you need to use the SetConsoleAttribute function

    http://msdn.microsoft.com/en-us/library/ms686047.aspx

    0 讨论(0)
  • 2020-12-14 23:12

    As far as I know it is not possible with a command line, it is just one color...

    0 讨论(0)
  • 2020-12-14 23:13

    You could use an ANSI escape sequence, but that won't do what you want under modern versions of Windows. Wikipedia has a very informative article:

    http://en.wikipedia.org/wiki/ANSI_escape_code

    So the answer to your original question is almost certainly "no." However, you can change the foreground color without writing an escape sequence, for example by invoking a Win32 API function. I don't know how to do this sort of thing in Ruby off the top of my head, but somebody else seems to have managed:

    http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/241925

    I imagine you'd want to use 4 for dark red or 12 for bright red, and 7 to restore the default color.

    Hope this helps!

    0 讨论(0)
  • 2020-12-14 23:15

    You want ANSI escape codes.

    0 讨论(0)
  • 2020-12-14 23:16

    A lot of the old ANSI Color Codes work. The code for a red foreground is something like Escape-[31m. Escape is character 27, so that's "\033[31m" or "\x1B[31m", depending on your escaping scheme.

    [39m is the code to return to default color.

    It's also possible to specify multiple codes at once to set foreground and background color simultaneously.

    You may have to load ANSI.sys, see this page.

    0 讨论(0)
  • 2020-12-14 23:17

    You need to access the [Win32 Console API](http://msdn.microsoft.com/en-us/library/ms682073(VS.85%29.aspx). Unfortunately, I don't know how you'd do that from Ruby. In Perl, I'd use the Win32::Console module. The Windows console does not respond to ANSI escape codes.

    According to the article on colorizing Ruby output that artur02 mentioned, you need to install & load the win32console gem.

    0 讨论(0)
提交回复
热议问题