Advertisement
Guest User

Untitled

a guest
May 24th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.82 KB | None | 0 0
  1. using System;
  2.  
  3. class Programm
  4. {
  5.     static void Main(string[] args)
  6.     {
  7.         Window window = new Window(20, 15);
  8.         window.SetTitle("title");
  9.         window.SetTextColor(ConsoleColor.Green);
  10.         window.SetBorderColor(ConsoleColor.Blue);
  11.         window.SetPosition(5, 7);
  12.         window.DrawWindow();
  13.         window.PrintTitle();
  14.         window.PrintInnerText();
  15.  
  16.  
  17.     }
  18. }
  19.  
  20. /*
  21.  * Создание окна WxH
  22.  * Задание заголовка окна (не больше 15 символов)
  23.  * Задание цвета текста окна
  24.  * Задание цвета обводки окна
  25.  * Задание позиции окна (в пределах консоли)
  26.  * Отрисовка
  27.  */
  28. class Window
  29. {
  30.     private string _title;
  31.     private string _innerText;
  32.     private int _width;
  33.     private int _height;
  34.     private ConsoleColor _border;
  35.     private ConsoleColor _textColor;
  36.     private int _positionX;
  37.     private int _positionY;
  38.  
  39.  
  40.     public Window(int width, int height)
  41.     {
  42.         if (width > Console.WindowWidth)
  43.         {
  44.             throw new Exception("Ширина окна превышает максимальную");
  45.         }
  46.         else
  47.         {
  48.             _width = width;
  49.         }
  50.         if (height > Console.WindowHeight)
  51.         {
  52.             throw new Exception("Высота окна превышает максимальную");
  53.         }
  54.         else
  55.         {
  56.             _height = height;
  57.         }
  58.        
  59.        
  60.     }
  61.     public void PrintTitle()
  62.     {      
  63.         for (int i = 0; i < _title.Length; i++)
  64.         {
  65.             Console.SetCursorPosition(_positionX + (_width - _title.Length) / 2 + i, 3 + _positionY);
  66.             Console.ForegroundColor = _textColor;
  67.             Console.Write(_title[i]);
  68.             ResetCoursorPosition();
  69.         }        
  70.     }
  71.     public void PrintInnerText()
  72.     {
  73.         Console.WriteLine("Введите текст");
  74.         _innerText = Console.ReadLine();
  75.         if (_innerText.Length < _width - 2)
  76.         {
  77.             for (int i = 0; i < _innerText.Length; i++)
  78.             {
  79.                 Console.SetCursorPosition(_positionX + (_width - _innerText.Length)/2 + i, _height/2 + _positionY + 4);
  80.                 Console.ForegroundColor = _textColor;
  81.                 Console.Write(_innerText[i]);
  82.                 ResetCoursorPosition();
  83.             }
  84.         }
  85.     }
  86.     public void SetTitle(string title)
  87.     {
  88.         if (title.Length <= 15)
  89.         {
  90.             _title = title;
  91.         }
  92.     }
  93.  
  94.     public void ResetCoursorPosition()
  95.     {
  96.         Console.SetCursorPosition(0, 4 + _height + _positionY);
  97.     }
  98.  
  99.     public void SetTextColor(ConsoleColor color)
  100.     {
  101.         _textColor = color;
  102.     }
  103.  
  104.     public void SetBorderColor(ConsoleColor color)
  105.     {
  106.         _border = color;
  107.     }
  108.  
  109.     public void SetPosition(int positionX, int positionY)
  110.     {
  111.         if (positionX + _width < Console.WindowWidth)
  112.         {
  113.             _positionX = positionX;
  114.         }
  115.         if (positionY + _height < Console.WindowHeight)
  116.         {
  117.             _positionY = positionY;
  118.         }
  119.     }
  120.  
  121.    
  122.  
  123.     public void DrawWindow()
  124.     {
  125.         for (int i = 0; i < _width; i++)
  126.         {
  127.             for (int j = 0; j < _height +2; j++)
  128.             {
  129.                 if (j == 0 || i == 0 || j == _height + 1 || i == _width - 1 || j == 2)
  130.                 {
  131.                     Console.SetCursorPosition(i+ _positionX, j + _positionY + 2);
  132.                     Console.ForegroundColor = _textColor;
  133.                     Console.Write('#');
  134.                     ResetCoursorPosition();
  135.                 }
  136.             }
  137.         }
  138.     }
  139.  
  140. }
  141.  
  142. /*
  143. H - 10
  144. W - 5
  145.  
  146. ##########
  147. #Title   #
  148. ##########
  149. #        #
  150. #Main    #
  151. #        #
  152. #        #
  153. ##########
  154. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement