pascalabcnet/TestSuite/CompilationSamples/CRT.pas

333 lines
8.3 KiB
ObjectPascal
Raw Normal View History

// 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-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-05-14 22:35:07 +03:00
/// </summary>
/// <param name="s">Заголовок консольного окна</param>
2015-05-14 22:35:07 +03:00
procedure SetWindowTitle(s: string);
/// <summary>
/// Задает заголовок консольного окна
2015-05-14 22:35:07 +03:00
/// </summary>
/// <param name="s">Заголовок консольного окна</param>
2015-05-14 22:35:07 +03:00
procedure SetWindowCaption(s: string);
/// <summary>
/// Показывает курсор если он скрыт
2015-05-14 22:35:07 +03:00
/// </summary>
procedure ShowCursor;
/// <summary>
/// Скрывает курсор
2015-05-14 22:35:07 +03:00
/// </summary>
procedure HideCursor;
/// <summary>
/// Считыват нажатую клавишу
2015-05-14 22:35:07 +03:00
/// </summary>
function ReadKey: char;
/// <summary>
/// Возвращает true если была нажата клавиша. Считать символ можно спомощью функции ReadKey
2015-05-14 22:35:07 +03:00
/// </summary>
function KeyPressed: boolean;
/// <summary>
/// Возвращает ширину экрана
2015-05-14 22:35:07 +03:00
/// </summary>
function WindowWidth: integer;
/// <summary>
/// Возвращает высоту экрана
2015-05-14 22:35:07 +03:00
/// </summary>
function WindowHeight: integer;
/// <summary>
/// Возвращает Х-координату курсора
2015-05-14 22:35:07 +03:00
/// </summary>
function WhereX: integer;
/// <summary>
/// Возвращает Y-координату курсора
2015-05-14 22:35:07 +03:00
/// </summary>
function WhereY: integer;
/// <summary>
/// Переводит курсор в координаты (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-05-14 22:35:07 +03:00
/// </summary>
/// <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-05-14 22:35:07 +03:00
/// </summary>
procedure ClrScr;
/// <summary>
/// Задает цвет фона выводимого текста
2015-05-14 22:35:07 +03:00
/// </summary>
/// <param name="c">Цвет фона</param>
2015-05-14 22:35:07 +03:00
procedure TextBackground(c: integer);
/// <summary>
/// Задает цвет выводимого текста
2015-05-14 22:35:07 +03:00
/// </summary>
/// <param name="c">Цвет текста</param>
2015-05-14 22:35:07 +03:00
procedure TextColor(c: integer);
/// <summary>
/// Очищает линию на которой установлен курсор
2015-05-14 22:35:07 +03:00
/// </summary>
procedure ClearLine;
/// <summary>
/// Делает паузу на 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;
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
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
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.