Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change text color in delphi (Console application)

I know the question I've asked seems similar to others however it doesn't seem to apply.

I am using delphi 10.3

I want to write two texts consecutively in the console application however I want them separate colors

writeln('yes just give me a minute, i need to talk to the manager'); {i want this in the default color}
writeln('Oi Dave we got another thick one shall i just pass him through as self employed'); {i want this to be in red}
writeln('Dont worry u dont have to complete this one') {and this one back to the default color}
like image 799
Ben Hanley Avatar asked Sep 17 '19 19:09

Ben Hanley


2 Answers

You can use SetConsoleTextAttribute as already commented to the question. Example:

program Project1;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils, winapi.windows;

var
  ConOut: THandle;
  BufInfo: TConsoleScreenBufferInfo;
begin
    writeln('yes just give me a minute, i need to talk to the manager');

    // get console screen buffer handle
    ConOut := TTextRec(Output).Handle;  // or GetStdHandle(STD_OUTPUT_HANDLE)

    // save current text attributes
    GetConsoleScreenBufferInfo(ConOut, BufInfo);

    // set foreground color to red
    SetConsoleTextAttribute(TTextRec(Output).Handle, FOREGROUND_INTENSITY or FOREGROUND_RED);

    writeln('Oi Dave we got another thick one shall i just pass him through as self employed');

    // reset to defaults
    SetConsoleTextAttribute(ConOut, BufInfo.wAttributes);

    writeln('Dont worry u dont have to complete this one');
    readln;
end.


Minimum required reading: SetConsoleTextAttribute and character attributes.

Don't forget to add error handling.

like image 74
Sertac Akyuz Avatar answered Nov 11 '22 22:11

Sertac Akyuz


I use too the SetConsoleTextAttribute function defined in WinAPI.Windows.

With this simple procedure (extensible):

/// <summary> Cambiar el color de la salida de consola </summary>
/// <summary> Change the color of console output</summary>
procedure SetColorConsole(AColor:TColor);
begin
  SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_RED or FOREGROUND_GREEN or FOREGROUND_BLUE);
  case AColor of
    clWhite:  SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_RED or FOREGROUND_GREEN or FOREGROUND_BLUE);
    clRed:    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_RED or FOREGROUND_INTENSITY);
    clGreen:  SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_GREEN or FOREGROUND_INTENSITY);
    clBlue:   SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_BLUE or FOREGROUND_INTENSITY);
    clMaroon: SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_GREEN or FOREGROUND_RED or FOREGROUND_INTENSITY);
    clPurple: SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_RED or FOREGROUND_BLUE or FOREGROUND_INTENSITY);
    clAqua: SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_GREEN or FOREGROUND_BLUE or FOREGROUND_INTENSITY);
  end;
end;

You can use something like this:

  WriteLn(' ');
  SetColorConsole(clWhite);
  WriteLn('clWhite');
  SetColorConsole(clRed);
  WriteLn('clRed');
  SetColorConsole(clGreen);
  WriteLn('clGreen');
  SetColorConsole(clBlue);
  WriteLn('clBlue');
  SetColorConsole(clMaroon);
  WriteLn('clYellow');
  SetColorConsole(clPurple);
  WriteLn('clPurple');
  SetColorConsole(clAqua);
  WriteLn('clAqua');

And the result is like this.

enter image description here

like image 29
Germán Estévez -Neftalí- Avatar answered Nov 11 '22 22:11

Germán Estévez -Neftalí-