Вывод текста в консоли в углу, независящего от остального - C#
Формулировка задачи:
Хочу сделать в правом нижнем углу динамический текст, который будет обновляться.
Мне надо сделать так, что бы он отображался всегда, поверх всех надписей.
Как такое сделать?
Решение задачи: «Вывод текста в консоли в углу, независящего от остального»
textual
Листинг программы
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Threading;
namespace ConsoleApplication1 {
class Program {
[DllImport("user32.dll", SetLastError = true)]
static extern bool GetClientRect(IntPtr hwnd, out Rectangle lpRect);
static void Main(string[] args) {
Thread t = new Thread(DrawTime);
t.Start();
while (true) {
ConsoleKeyInfo cki = Console.ReadKey(false);
if (cki.Key == ConsoleKey.Escape) break;
}
t.Abort();
}
static void DrawTime() {
while (true) {
Thread.Sleep(100);//Небольшая задержка, чтобы не мерцало
Rectangle clientArea;
IntPtr hwnd = System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle;
GetClientRect(hwnd, out clientArea);
string time = DateTime.Now.ToLongTimeString();
using (Graphics g = Graphics.FromHwnd(hwnd)) {
using (Font f = new Font("Courier New", 25)) {
SizeF sf = g.MeasureString(time, f);
float x = clientArea.Right - sf.Width, y = clientArea.Bottom - sf.Height;
g.Clip = new Region(new RectangleF(x, y, sf.Width, sf.Height));
g.Clear(Color.Black);
g.DrawString(time, f, Brushes.LightGreen, new PointF(x, y));
}
}
}
}
}
}