Как в консольном приложении можно задать цвет текста?

Материал из DRKB

Как в консольном приложении можно задать цвет текста?[править | править код]

Цвет Текста задается командой SetTextColor(Color), параметр Color - целое число от 0 до 15.

Вывод текста в указанном месте экрана задается командой GotoXY(X, Y, Text).

  • X,Y-координаты экрана.
  • Text - переменная типа String.

Ответ 3:

Вот текст модуля, напоминающего про наш любимый ДОС (CRT-like):

unit UffCRT;
// written by Michael Uskoff, Apr 2001, St.Petersburg, RUSSIA

interface

procedure ClrScr;
procedure SetAttr(attr: Word);
function GetAttr: Word;
procedure GotoXY(aX, aY: Integer); // zero-based coords
function WhereX: Integer;
function WhereY: Integer;

implementation

uses Windows;

var
  UpperLeft: TCoord = (X: 0; Y: 0);
  hCon: Integer;

procedure GotoXY(aX, aY: Integer);
var
  aCoord: TCoord;
begin
  aCoord.x := aX;
  aCoord.y := aY;
  SetConsoleCursorPosition(hCon, aCoord);
end;

procedure SetAttr(attr: Word);
begin
  SetConsoleTextAttribute(hCon, attr);
end;

function WhereX: Integer;
var
  ScrBufInfo: TConsoleScreenBufferInfo;
begin
  GetConsoleScreenBufferInfo(hCon, ScrBufInfo);
  Result := ScrBufInfo.dwCursorPosition.x;
end;

function WhereY: Integer;
var
  ScrBufInfo: TConsoleScreenBufferInfo;
begin
  GetConsoleScreenBufferInfo(hCon, ScrBufInfo);
  Result := ScrBufInfo.dwCursorPosition.y;
end;

function GetAttr: Word;
var
  ScrBufInfo: TConsoleScreenBufferInfo;
begin
  GetConsoleScreenBufferInfo(hCon, ScrBufInfo);
  Result := ScrBufInfo.wAttributes;
end;

procedure ClrScr;
var
  fill: Integer;
  ScrBufInfo: TConsoleScreenBufferInfo;
begin
  GetConsoleScreenBufferInfo(hCon, ScrBufInfo);
  fill := ScrBufInfo.dwSize.x * ScrBufInfo.dwSize.y;
  FillConsoleOutputCharacter(hCon, ' ', fill, UpperLeft, fill);
  FillConsoleOutputAttribute(hCon, ScrBufInfo.wAttributes, fill, UpperLeft, fill);
  GotoXY(0, 0);
end;

initialization
  hCon := GetStdHandle(STD_OUTPUT_HANDLE);
end.


Теперь можно творить такое:

uses UffCRT;

begin
  ClrScr;
  SetAttr($1E);
  GotoXY(32, 12);
  Write('Hello, master !');
  ReadLn;
end.


Source: Взято с сайта http://blackman.wp-club.net/
ID: 02185