【问题标题】:Change text color in delphi (Console application)在 delphi 中更改文本颜色(控制台应用程序)
【发布时间】:2020-01-18 16:37:27
【问题描述】:

我知道我提出的问题与其他问题相似,但似乎并不适用。

我正在使用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}

【问题讨论】:

标签: delphi delphi-10.3-rio


【解决方案1】:

您可以像 commented 一样使用 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.


最低要求阅读:SetConsoleTextAttributecharacter attributes

别忘了添加错误处理。

【讨论】:

  • 谢谢你,它就像一种享受,我必须改变它以获得其他颜色
【解决方案2】:

我也使用了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');

结果是这样的。

【讨论】:

    【解决方案3】:

    有一个非常简单的解决方案,使用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');
    

    【讨论】:

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