2023-02-16 21:18:26 +03:00
|
|
|
|
// Copyright (c) Ivan Bondarev, Stanislav Mikhalkovich (for details please see \doc\copyright.txt)
|
2015-06-27 19:33:50 +03:00
|
|
|
|
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
|
|
|
|
|
|
|
2015-05-14 22:35:07 +03:00
|
|
|
|
/// <summary>
|
2015-12-28 14:25:15 +03:00
|
|
|
|
/// Модуль для работы с консолью
|
2015-05-14 22:35:07 +03:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
unit CRT;
|
|
|
|
|
|
|
|
|
|
|
|
{$apptype console}
|
|
|
|
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
|
|
|
|
uses
|
|
|
|
|
|
System;
|
|
|
|
|
|
|
|
|
|
|
|
const
|
2024-02-04 19:17:30 +03:00
|
|
|
|
//{{{doc: Начало секции 0 }}}
|
|
|
|
|
|
// -----------------------------------------------------
|
|
|
|
|
|
//>> Цветовые константы модуля CRT # CRT constants
|
|
|
|
|
|
// -----------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
/// Черный цвет
|
|
|
|
|
|
Black = 0;
|
|
|
|
|
|
/// Синий цвет
|
2015-05-14 22:35:07 +03:00
|
|
|
|
Blue = 1;
|
2024-02-04 19:17:30 +03:00
|
|
|
|
/// Зеленый цвет
|
2015-05-14 22:35:07 +03:00
|
|
|
|
Green = 2;
|
2024-02-04 19:17:30 +03:00
|
|
|
|
/// Бирюзовый цвет
|
2015-05-14 22:35:07 +03:00
|
|
|
|
Cyan = 3;
|
2024-02-04 19:17:30 +03:00
|
|
|
|
/// Красный цвет
|
2015-05-14 22:35:07 +03:00
|
|
|
|
Red = 4;
|
2024-02-04 19:17:30 +03:00
|
|
|
|
/// Малиновый цвет
|
2015-05-14 22:35:07 +03:00
|
|
|
|
Magenta = 5;
|
2024-02-04 19:17:30 +03:00
|
|
|
|
/// Коричневый цвет
|
2015-05-14 22:35:07 +03:00
|
|
|
|
Brown = 6;
|
2024-02-04 19:17:30 +03:00
|
|
|
|
/// Светло-серый цвет
|
2015-05-14 22:35:07 +03:00
|
|
|
|
LightGray = 7;
|
2024-02-04 19:17:30 +03:00
|
|
|
|
/// Темно-серый цвет
|
2015-05-14 22:35:07 +03:00
|
|
|
|
DarkGray = 8;
|
2024-02-04 19:17:30 +03:00
|
|
|
|
/// Светло-голубой цвет
|
2015-05-14 22:35:07 +03:00
|
|
|
|
LightBlue = 9;
|
2024-02-04 19:17:30 +03:00
|
|
|
|
/// Светло-зеленый цвет
|
2015-05-14 22:35:07 +03:00
|
|
|
|
LightGreen = 10;
|
2024-02-04 19:17:30 +03:00
|
|
|
|
/// Светло-бирюзовый цвет
|
2015-05-14 22:35:07 +03:00
|
|
|
|
LightCyan = 11;
|
2024-02-04 19:17:30 +03:00
|
|
|
|
/// цвет
|
2015-05-14 22:35:07 +03:00
|
|
|
|
LightRed = 12;
|
2024-02-04 19:17:30 +03:00
|
|
|
|
/// Светло-малиновый цвет
|
2015-05-14 22:35:07 +03:00
|
|
|
|
LightMagenta = 13;
|
2024-02-04 19:17:30 +03:00
|
|
|
|
/// Желтый цвет
|
2015-05-14 22:35:07 +03:00
|
|
|
|
Yellow = 14;
|
2024-02-04 19:17:30 +03:00
|
|
|
|
/// Белый цвет
|
2015-05-14 22:35:07 +03:00
|
|
|
|
White = 15;
|
2024-02-04 19:17:30 +03:00
|
|
|
|
//{{{--doc: Конец секции 0 }}}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//{{{doc: Начало секции 1 }}}
|
|
|
|
|
|
|
|
|
|
|
|
// -----------------------------------------------------
|
|
|
|
|
|
//>> Подпрограммы модуля CRT # CRT functions
|
|
|
|
|
|
// -----------------------------------------------------
|
2015-05-14 22:35:07 +03:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2015-12-28 14:25:15 +03:00
|
|
|
|
/// Задает заголовок консольного окна
|
2015-05-14 22:35:07 +03:00
|
|
|
|
/// </summary>
|
2015-12-28 14:25:15 +03:00
|
|
|
|
/// <param name="s">Заголовок консольного окна</param>
|
2015-05-14 22:35:07 +03:00
|
|
|
|
procedure SetWindowTitle(s: string);
|
|
|
|
|
|
/// <summary>
|
2015-12-28 14:25:15 +03:00
|
|
|
|
/// Задает заголовок консольного окна
|
2015-05-14 22:35:07 +03:00
|
|
|
|
/// </summary>
|
2015-12-28 14:25:15 +03:00
|
|
|
|
/// <param name="s">Заголовок консольного окна</param>
|
2015-05-14 22:35:07 +03:00
|
|
|
|
procedure SetWindowCaption(s: string);
|
|
|
|
|
|
/// <summary>
|
2015-12-28 14:25:15 +03:00
|
|
|
|
/// Показывает курсор если он скрыт
|
2015-05-14 22:35:07 +03:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
procedure ShowCursor;
|
|
|
|
|
|
/// <summary>
|
2015-12-28 14:25:15 +03:00
|
|
|
|
/// Скрывает курсор
|
2015-05-14 22:35:07 +03:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
procedure HideCursor;
|
|
|
|
|
|
/// <summary>
|
2015-12-28 14:25:15 +03:00
|
|
|
|
/// Считыват нажатую клавишу
|
2015-05-14 22:35:07 +03:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
function ReadKey: char;
|
|
|
|
|
|
/// <summary>
|
2015-12-28 14:25:15 +03:00
|
|
|
|
/// Возвращает true если была нажата клавиша. Считать символ можно спомощью функции ReadKey
|
2015-05-14 22:35:07 +03:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
function KeyPressed: boolean;
|
|
|
|
|
|
/// <summary>
|
2023-02-16 21:18:26 +03:00
|
|
|
|
/// Возвращает ширину экрана
|
2015-05-14 22:35:07 +03:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
function WindowWidth: integer;
|
|
|
|
|
|
/// <summary>
|
2023-02-16 21:18:26 +03:00
|
|
|
|
/// Возвращает высоту экрана
|
2015-05-14 22:35:07 +03:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
function WindowHeight: integer;
|
|
|
|
|
|
/// <summary>
|
2015-12-28 14:25:15 +03:00
|
|
|
|
/// Возвращает Х-координату курсора
|
2015-05-14 22:35:07 +03:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
function WhereX: integer;
|
|
|
|
|
|
/// <summary>
|
2015-12-28 14:25:15 +03:00
|
|
|
|
/// Возвращает Y-координату курсора
|
2015-05-14 22:35:07 +03:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
function WhereY: integer;
|
|
|
|
|
|
/// <summary>
|
2015-12-28 14:25:15 +03:00
|
|
|
|
/// Переводит курсор в координаты (x,y)
|
2015-05-14 22:35:07 +03:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
procedure GotoXY(x, y: integer);
|
|
|
|
|
|
procedure Window(x, y, w, h: integer);
|
|
|
|
|
|
/// <summary>
|
2015-12-28 14:25:15 +03:00
|
|
|
|
/// Задает размеры консольного окна
|
2015-05-14 22:35:07 +03:00
|
|
|
|
/// </summary>
|
2015-12-28 14:25:15 +03:00
|
|
|
|
/// <param name="w">Ширина</param>
|
|
|
|
|
|
/// <param name="h">Высота</param>
|
2015-05-14 22:35:07 +03:00
|
|
|
|
procedure SetWindowSize(w, h: integer);
|
|
|
|
|
|
procedure SetBufferSize(w, h: integer);
|
|
|
|
|
|
/// <summary>
|
2015-12-28 14:25:15 +03:00
|
|
|
|
/// Очищает экран, заполняя его текущим цветом фона
|
2015-05-14 22:35:07 +03:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
procedure ClrScr;
|
|
|
|
|
|
/// <summary>
|
2015-12-28 14:25:15 +03:00
|
|
|
|
/// Задает цвет фона выводимого текста
|
2015-05-14 22:35:07 +03:00
|
|
|
|
/// </summary>
|
2015-12-28 14:25:15 +03:00
|
|
|
|
/// <param name="c">Цвет фона</param>
|
2015-05-14 22:35:07 +03:00
|
|
|
|
procedure TextBackground(c: integer);
|
|
|
|
|
|
/// <summary>
|
2015-12-28 14:25:15 +03:00
|
|
|
|
/// Задает цвет выводимого текста
|
2015-05-14 22:35:07 +03:00
|
|
|
|
/// </summary>
|
2015-12-28 14:25:15 +03:00
|
|
|
|
/// <param name="c">Цвет текста</param>
|
2015-05-14 22:35:07 +03:00
|
|
|
|
procedure TextColor(c: integer);
|
|
|
|
|
|
/// <summary>
|
2015-12-28 14:25:15 +03:00
|
|
|
|
/// Очищает линию на которой установлен курсор
|
2015-05-14 22:35:07 +03:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
procedure ClearLine;
|
|
|
|
|
|
/// <summary>
|
2015-12-28 14:25:15 +03:00
|
|
|
|
/// Делает паузу на ms миллисекунд
|
2015-05-14 22:35:07 +03:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
procedure Delay(ms: integer);
|
|
|
|
|
|
|
2024-02-04 19:17:30 +03:00
|
|
|
|
//{{{--doc: Конец секции 1 }}}
|
|
|
|
|
|
|
2015-05-14 22:35:07 +03:00
|
|
|
|
///--
|
|
|
|
|
|
procedure __InitModule__;
|
|
|
|
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
|
|
|
|
|
|
|
var
|
|
|
|
|
|
nextkey: char;
|
|
|
|
|
|
BlankString: string;
|
|
|
|
|
|
|
|
|
|
|
|
procedure ClearLine;
|
|
|
|
|
|
begin
|
|
|
|
|
|
Console.CursorLeft := 0;
|
|
|
|
|
|
Console.Write(BlankString);
|
|
|
|
|
|
Console.CursorLeft := 0;
|
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
procedure SetWindowTitle(s: string);
|
|
|
|
|
|
begin
|
|
|
|
|
|
Console.Title := s;
|
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
procedure SetWindowCaption(s: string);
|
|
|
|
|
|
begin
|
|
|
|
|
|
Console.Title := s;
|
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
procedure SetWindowSize(w, h: integer);
|
|
|
|
|
|
begin
|
|
|
|
|
|
Console.SetWindowSize(w, h);
|
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
procedure ShowCursor;
|
|
|
|
|
|
begin
|
|
|
|
|
|
Console.CursorVisible := True;
|
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
procedure HideCursor;
|
|
|
|
|
|
begin
|
|
|
|
|
|
Console.CursorVisible := False;
|
|
|
|
|
|
end;
|
|
|
|
|
|
|
2015-12-28 14:25:15 +03:00
|
|
|
|
function ReadKey: char;// TODO продумать это
|
2015-05-14 22:35:07 +03:00
|
|
|
|
var
|
|
|
|
|
|
KeyInfo: ConsoleKeyInfo;
|
|
|
|
|
|
begin
|
2024-02-09 00:03:16 +03:00
|
|
|
|
if NextKey <> #0 then
|
2015-05-14 22:35:07 +03:00
|
|
|
|
begin
|
2024-02-09 00:03:16 +03:00
|
|
|
|
ReadKey := NextKey;
|
2015-05-14 22:35:07 +03:00
|
|
|
|
NextKey := #0;
|
|
|
|
|
|
end
|
|
|
|
|
|
else
|
|
|
|
|
|
begin
|
|
|
|
|
|
KeyInfo := Console.ReadKey(true);
|
|
|
|
|
|
ReadKey := Convert.ToChar(KeyInfo.KeyChar);
|
|
|
|
|
|
if KeyInfo.KeyChar = #0 then
|
|
|
|
|
|
NextKey := Convert.ToChar(KeyInfo.Key);
|
|
|
|
|
|
end;
|
|
|
|
|
|
//ReadKey := Convert.ToChar(Console.ReadKey(true).Key);
|
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
function KeyPressed: boolean;
|
|
|
|
|
|
begin
|
2024-02-09 00:03:16 +03:00
|
|
|
|
KeyPressed := (NextKey <> #0) or Console.KeyAvailable;
|
2015-05-14 22:35:07 +03:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
function WindowWidth: integer;
|
|
|
|
|
|
begin
|
|
|
|
|
|
WindowWidth := Console.WindowWidth;
|
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
function WindowHeight: integer;
|
|
|
|
|
|
begin
|
|
|
|
|
|
WindowHeight := Console.WindowHeight;
|
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
function WhereX: integer;
|
|
|
|
|
|
begin
|
|
|
|
|
|
WhereX := Console.CursorLeft + 1;
|
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
function WhereY: integer;
|
|
|
|
|
|
begin
|
|
|
|
|
|
WhereY := Console.CursorTop + 1;
|
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
procedure GotoXY(x, y: integer);
|
|
|
|
|
|
begin
|
|
|
|
|
|
if (x <= Console.WindowWidth) and (y <= Console.WindowHeight) and (x > 0) and (y > 0) then
|
|
|
|
|
|
Console.SetCursorPosition(x - 1, y - 1);
|
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
procedure Window(x, y, w, h: integer);
|
|
|
|
|
|
begin
|
2015-12-28 14:25:15 +03:00
|
|
|
|
writeln('Функция CRT.Window не реализована');
|
2015-05-14 22:35:07 +03:00
|
|
|
|
{Console.WindowLeft:=x;
|
|
|
|
|
|
Console.WindowTop:=y;
|
|
|
|
|
|
Console.WindowWidth:=w;
|
|
|
|
|
|
Console.WindowHeight:=h;}
|
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
procedure SetBufferSize(w, h: integer);
|
|
|
|
|
|
begin
|
|
|
|
|
|
Console.SetBufferSize(w, h);
|
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
procedure ClrScr;
|
|
|
|
|
|
begin
|
|
|
|
|
|
Console.Clear;
|
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
function IntToConsoleColor(c: integer): ConsoleColor;
|
|
|
|
|
|
begin
|
|
|
|
|
|
case c of
|
|
|
|
|
|
Black: IntToConsoleColor := ConsoleColor.Black;
|
|
|
|
|
|
Blue: IntToConsoleColor := ConsoleColor.DarkBlue;
|
|
|
|
|
|
Green: IntToConsoleColor := ConsoleColor.DarkGreen;
|
|
|
|
|
|
Cyan: IntToConsoleColor := ConsoleColor.DarkCyan;
|
|
|
|
|
|
Red: IntToConsoleColor := ConsoleColor.DarkRed;
|
|
|
|
|
|
Magenta: IntToConsoleColor := ConsoleColor.DarkMagenta;
|
|
|
|
|
|
Brown: IntToConsoleColor := ConsoleColor.DarkYellow;
|
|
|
|
|
|
LightGray: IntToConsoleColor := ConsoleColor.Gray;
|
|
|
|
|
|
DarkGray: IntToConsoleColor := ConsoleColor.DarkGray;
|
|
|
|
|
|
LightBlue: IntToConsoleColor := ConsoleColor.Blue;
|
|
|
|
|
|
LightGreen: IntToConsoleColor := ConsoleColor.Green;
|
|
|
|
|
|
LightCyan: IntToConsoleColor := ConsoleColor.Cyan;
|
|
|
|
|
|
LightRed: IntToConsoleColor := ConsoleColor.Red;
|
|
|
|
|
|
LightMagenta: IntToConsoleColor := ConsoleColor.Magenta;
|
|
|
|
|
|
Yellow: IntToConsoleColor := ConsoleColor.Yellow;
|
|
|
|
|
|
White: IntToConsoleColor := ConsoleColor.White;
|
|
|
|
|
|
else
|
|
|
|
|
|
raise new System.ArgumentOutOfRangeException('c');
|
|
|
|
|
|
end;
|
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
procedure TextBackground(c: integer);
|
|
|
|
|
|
begin
|
|
|
|
|
|
Console.BackgroundColor := IntToConsoleColor(c);
|
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
procedure TextColor(c: integer);
|
|
|
|
|
|
begin
|
|
|
|
|
|
Console.ForegroundColor := IntToConsoleColor(c);
|
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
procedure Delay(ms: integer);
|
|
|
|
|
|
begin
|
|
|
|
|
|
Sleep(ms);
|
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
function RedirectIOUnitUsed: boolean;
|
|
|
|
|
|
var
|
|
|
|
|
|
t: &Type;
|
|
|
|
|
|
begin
|
|
|
|
|
|
t := System.Reflection.Assembly.GetExecutingAssembly.GetType('__RedirectIOMode.__RedirectIOMode');
|
|
|
|
|
|
Result := t <> nil;
|
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
var
|
|
|
|
|
|
i: integer;
|
|
|
|
|
|
|
|
|
|
|
|
var __initialized := false;
|
|
|
|
|
|
|
|
|
|
|
|
procedure __InitModule;
|
|
|
|
|
|
begin
|
|
|
|
|
|
if (not RedirectIOUnitUsed) {and IsConsoleApplication} then
|
|
|
|
|
|
begin
|
|
|
|
|
|
Console.CursorSize := 15;
|
|
|
|
|
|
BlankString := new string(' ', Console.BufferWidth - 1);
|
|
|
|
|
|
end
|
|
|
|
|
|
else
|
|
|
|
|
|
begin
|
2015-12-28 14:25:15 +03:00
|
|
|
|
Console.WriteLine('Программу с подключенным модулем CRT нельзя запускать по F9.');
|
|
|
|
|
|
Console.WriteLine('Запустите программу, используя Shift-F9');
|
2015-05-14 22:35:07 +03:00
|
|
|
|
Halt;
|
|
|
|
|
|
end;
|
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
procedure __InitModule__;
|
|
|
|
|
|
begin
|
|
|
|
|
|
if not __initialized then
|
|
|
|
|
|
begin
|
|
|
|
|
|
__initialized := true;
|
|
|
|
|
|
PABCSystem.__InitModule__;
|
|
|
|
|
|
__InitModule;
|
|
|
|
|
|
end;
|
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
begin
|
|
|
|
|
|
//zdes oshibka ISConsoleApplication u nas nikogda ne inicializiruetsja
|
|
|
|
|
|
__InitModule;
|
2024-02-09 00:03:16 +03:00
|
|
|
|
end.
|