Last active
October 29, 2025 20:37
-
-
Save sunmeat/7369e98cb742be3e63cbca7ef228ea47 to your computer and use it in GitHub Desktop.
введення даних в консоль
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.Runtime.InteropServices; // DllImport | |
| using System.Text; // Console.InputEncoding = Encoding.UTF8; | |
| namespace ConsoleIO | |
| { | |
| [StructLayout(LayoutKind.Sequential, Pack = 1)] | |
| public struct ConsoleFont | |
| { | |
| public uint Index; | |
| public short SizeX, SizeY; | |
| } | |
| public static class ConsoleHelper | |
| { | |
| [DllImport("kernel32")] | |
| private extern static bool SetConsoleFont(IntPtr hOutput, uint index); | |
| private enum StdHandle | |
| { | |
| OutputHandle = -11 | |
| } | |
| [DllImport("kernel32")] | |
| private static extern IntPtr GetStdHandle(StdHandle index); | |
| public static bool SetConsoleFont(uint index) | |
| { | |
| return SetConsoleFont(GetStdHandle(StdHandle.OutputHandle), index); | |
| } | |
| [DllImport("kernel32")] | |
| private static extern bool GetConsoleFontInfo(IntPtr hOutput, [MarshalAs(UnmanagedType.Bool)] bool bMaximize, | |
| uint count, [MarshalAs(UnmanagedType.LPArray), Out] ConsoleFont[] fonts); | |
| [DllImport("kernel32")] | |
| private static extern uint GetNumberOfConsoleFonts(); | |
| public static uint ConsoleFontsCount | |
| { | |
| get | |
| { | |
| return GetNumberOfConsoleFonts(); | |
| } | |
| } | |
| public static ConsoleFont[] ConsoleFonts | |
| { | |
| get | |
| { | |
| ConsoleFont[] fonts = new ConsoleFont[GetNumberOfConsoleFonts()]; | |
| if (fonts.Length > 0) | |
| GetConsoleFontInfo(GetStdHandle(StdHandle.OutputHandle), false, (uint)fonts.Length, fonts); | |
| return fonts; | |
| } | |
| } | |
| } | |
| static class Input | |
| { | |
| static int number; | |
| public static int Number() | |
| { | |
| while (true) | |
| { | |
| try | |
| { | |
| number = Convert.ToInt32(Console.ReadLine()); | |
| } | |
| catch (System.FormatException) | |
| { | |
| Console.WriteLine("потрібно було ввести число, а не рядок!"); | |
| continue; | |
| } | |
| catch (System.OverflowException) | |
| { | |
| Console.WriteLine("занадто велике число!"); | |
| continue; | |
| } | |
| break; | |
| } | |
| return number; | |
| } | |
| } | |
| class Program | |
| { | |
| static void Main() | |
| { | |
| Console.InputEncoding = Encoding.UTF8; | |
| Console.OutputEncoding = Encoding.UTF8; | |
| // зміна заголовка вікна консолі | |
| Console.Title = "Введення та виведення"; | |
| // зміна кольору фону | |
| Console.BackgroundColor = ConsoleColor.White; | |
| // зміна кольору тексту | |
| Console.ForegroundColor = ConsoleColor.DarkGreen; | |
| // отримання розміру найдовшого повідомлення в програмі | |
| int length = ("кодування вводу: " + Console.InputEncoding.ToString()).Length + 15; | |
| // Thread.Sleep(5000); | |
| // встановлення розміру вікна консолі | |
| Console.SetWindowSize(length, 8); // os windows only! | |
| // встановлення розміру буфера консолі (розмір вікна має відповідати і встановлюється перед буфером) | |
| Console.SetBufferSize(length, 8); | |
| // вивід інформації про кодування потоку вводу | |
| Console.WriteLine("кодування вводу: " + Console.InputEncoding.ToString()); | |
| // вивід інформації про кодування потоку виводу | |
| Console.WriteLine("кодування виводу: " + Console.OutputEncoding.ToString()); | |
| // скидання кольорів фону та тексту до значень за замовчуванням | |
| Console.ResetColor(); | |
| // вивід інформації про те, чи увімкнено клавішу num lock | |
| Console.WriteLine("чи увімкнено num lock: " + Console.NumberLock.ToString()); | |
| // вивід інформації про те, чи увімкнено клавішу caps lock | |
| Console.WriteLine("чи увімкнено caps lock: " + Console.CapsLock.ToString()); | |
| // вивід повідомлення користувачу про очікування вводу інформації | |
| Console.Write("введіть просте повідомлення: "); | |
| // отримання від користувача текстового повідомлення | |
| string message = Console.ReadLine(); | |
| // вивід введеного користувачем повідомлення | |
| Console.WriteLine("ваше повідомлення: " + message); | |
| // демонстрація зміни шрифтів консолі | |
| for (uint i = 0; i < 10; i++) | |
| { | |
| ConsoleHelper.SetConsoleFont(i); | |
| Console.WriteLine("зразок тексту!"); | |
| Thread.Sleep(500); | |
| } | |
| // читання клавіш до натискання enter | |
| string str = ""; | |
| while (true) | |
| { | |
| ConsoleKeyInfo k = Console.ReadKey(); | |
| if (k.Key == ConsoleKey.Enter) | |
| { | |
| Console.WriteLine(); | |
| break; | |
| } | |
| str += k.KeyChar; | |
| } | |
| Console.WriteLine(str); | |
| /* // _kbhit alternative | |
| while (true) | |
| { | |
| if (Console.KeyAvailable == true) | |
| { | |
| ConsoleKeyInfo k = Console.ReadKey(true); | |
| Console.Write(k.KeyChar); | |
| } | |
| else | |
| { | |
| Console.Write("_"); | |
| } | |
| Thread.Sleep(15); | |
| } | |
| */ | |
| // вивід запрошення для вводу числа | |
| Console.WriteLine("введіть число: "); | |
| int x = Input.Number(); | |
| // читання клавіші з модифікаторами | |
| ConsoleKeyInfo cki = Console.ReadKey(true); | |
| if (cki.Modifiers != 0) | |
| { | |
| if ((cki.Modifiers & ConsoleModifiers.Alt) != 0) | |
| Console.Write("alt+"); | |
| if ((cki.Modifiers & ConsoleModifiers.Shift) != 0) | |
| Console.Write("shift+"); | |
| if ((cki.Modifiers & ConsoleModifiers.Control) != 0) | |
| Console.Write("ctrl+"); | |
| } | |
| Console.WriteLine(cki.Key.ToString()); | |
| // екстрене завершення програми (за потреби) | |
| Environment.Exit(0); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment