更改 delphi 中的文本颜色(控制台应用程序)
Change text color in delphi (Console application)
我知道我问的问题似乎与其他问题相似,但似乎并不适用。
我正在使用delphi 10.3
我想在控制台应用程序中连续写入两个文本,但我希望它们使用不同的颜色
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}
您可以使用 SetConsoleTextAttribute
作为已经 的问题。示例:
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.
最低阅读要求:SetConsoleTextAttribute
and character attributes.
不要忘记添加错误处理。
有一个非常简单的解决方案,使用 DelphiConsole
var
CurrentColor : TConsoleColor;
CurrentColor := Console.ForegroundColor;
Console.WriteLine('yes just give me a minute, i need to talk to the manager');
Console.ForegroundColor := TConsoleColor.Red;
Console.WriteLine('Oi Dave we got another thick one shall i just pass him through as self employed');
Console.ForegroundColor := DefaultColor;
Console.WriteLine('Dont worry u dont have to complete this one');
我也使用 WinAPI.Windows
中定义的 SetConsoleTextAttribute
函数。
有了这个简单的程序(可扩展):
/// <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;
你可以这样使用:
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');
结果是这样的
我知道我问的问题似乎与其他问题相似,但似乎并不适用。
我正在使用delphi 10.3
我想在控制台应用程序中连续写入两个文本,但我希望它们使用不同的颜色
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}
您可以使用 SetConsoleTextAttribute
作为已经
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.
最低阅读要求:SetConsoleTextAttribute
and character attributes.
不要忘记添加错误处理。
有一个非常简单的解决方案,使用 DelphiConsole
var
CurrentColor : TConsoleColor;
CurrentColor := Console.ForegroundColor;
Console.WriteLine('yes just give me a minute, i need to talk to the manager');
Console.ForegroundColor := TConsoleColor.Red;
Console.WriteLine('Oi Dave we got another thick one shall i just pass him through as self employed');
Console.ForegroundColor := DefaultColor;
Console.WriteLine('Dont worry u dont have to complete this one');
我也使用 WinAPI.Windows
中定义的 SetConsoleTextAttribute
函数。
有了这个简单的程序(可扩展):
/// <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;
你可以这样使用:
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');
结果是这样的