Тема: Управление видеоадаптером и графическим выводом
Прикладная программа не обращается к видеокарте напрямую — этим занимается драйвер. Программе доступны:
Что можно сделать средствами консоли:
Экран консоли — это двумерный массив знакомест. Каждое знакоместо хранит символ и его атрибут: цвет текста и цвет фона.
using System;
var original = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Текст зелёного цвета");
Console.ForegroundColor = ConsoleColor.Yellow;
Console.BackgroundColor = ConsoleColor.DarkBlue;
Console.WriteLine("Жёлтый текст на синем фоне");
Console.ResetColor(); // возврат к стандартным цветам
Console.ForegroundColor = original;
Console.WriteLine("Обычный текст");Выведите все шестнадцать цветов и посмотрите, какие читаются на вашем экране:
using System;
foreach (ConsoleColor color in Enum.GetValues<ConsoleColor>())
{
Console.ForegroundColor = color;
Console.WriteLine($"{(int)color,2}: {color}");
}
Console.ResetColor();using System;
using System.Threading;
Console.WriteLine("Сейчас экран будет очищен...");
Thread.Sleep(1000);
Console.Clear();
Console.WriteLine("Экран очищен!");using System;
using System.Threading;
Console.CursorVisible = false;
int line = Console.CursorTop;
for (int i = 0; i < 20; i++)
{
Console.SetCursorPosition(0, line);
Console.Write(new string(' ', 30)); // стираем прежнее положение
Console.SetCursorPosition(i, line);
Console.Write("Hello");
Thread.Sleep(150);
}
Console.CursorVisible = true;
Console.WriteLine();
Приём простой: вернуть курсор, стереть старое, нарисовать новое. На нём строятся все анимации в консоли.
using System;
using System.Threading;
Console.CursorVisible = false;
int row = Console.CursorTop;
for (int percent = 0; percent <= 100; percent += 5)
{
int filled = percent * 30 / 100;
Console.SetCursorPosition(0, row);
Console.Write($"[{new string('#', filled)}{new string('-', 30 - filled)}] {percent,3}%");
Thread.Sleep(100);
}
Console.CursorVisible = true;
Console.WriteLine();Console.SetCursorPosition?