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}
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With